// Path: src/main/java/com/yammer/httptunnel/state/BindState.java
// public enum BindState {
// 	/**
// 	 * The channel is currently not bound to any address.
// 	 */
// 	UNBOUND,
// 
// 	/**
// 	 * The channel is currently in the process of binding to a local address.
// 	 */
// 	BINDING,
// 
// 	/**
// 	 * The channel is currently bound to a local address.
// 	 */
// 	BOUND,
// 
// 	/**
// 	 * The channel is currently in the process of unbinding from a local
// 	 * address.
// 	 */
// 	UNBINDING;
// }
// 
// Path: src/main/java/com/yammer/httptunnel/util/TunnelIdGenerator.java
// public interface TunnelIdGenerator {
// 
// 	/**
// 	 * Generates the next tunnel ID to be used, which must be unique (i.e.
// 	 * ensure with high probability that it will not clash with an existing
// 	 * tunnel ID). This method must be thread safe, and preferably lock free.
// 	 */
// 	public String generateId();
// }


import java.net.InetSocketAddress;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.jboss.netty.channel.AbstractServerChannel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineException;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelSink;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.socket.ServerSocketChannel;
import org.jboss.netty.channel.socket.ServerSocketChannelFactory;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import com.yammer.httptunnel.state.BindState;
import com.yammer.httptunnel.util.TunnelIdGenerator;
/*
 * Copyright 2011 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.yammer.httptunnel.server;




/**
 * The server end of an HTTP tunnel, created by an
 * {@link HttpTunnelServerChannelFactory}. Channels of this type are designed to
 * emulate a normal TCP based server socket channel as far as is feasible within
 * the limitations of the HTTP 1.0 protocol, and the usage patterns permitted by
 * commonly used HTTP proxies and firewalls.
 *
 * @author The Netty Project (netty-dev@lists.jboss.org)
 * @author Iain McGinniss (iain.mcginniss@onedrum.com)
 * @author Jamie Furness (jamie@onedrum.com)
 * @author OneDrum Ltd.
 */
public class HttpTunnelServerChannel extends AbstractServerChannel implements ServerSocketChannel {

	private static final InternalLogger LOG = InternalLoggerFactory.getInstance(HttpTunnelServerChannel.class);

	private static final Random random;

	static {
		random = new Random();
	}

	private final String tunnelIdPrefix;
	private final ConcurrentHashMap<String, HttpTunnelAcceptedChannel> tunnels;
	private final ServerSocketChannel realChannel;
	private final HttpTunnelServerChannelConfig config;

	private final AtomicBoolean opened;
// Path: src/main/java/com/yammer/httptunnel/state/BindState.java
// public enum BindState {
// 	/**
// 	 * The channel is currently not bound to any address.
// 	 */
// 	UNBOUND,
// 
// 	/**
// 	 * The channel is currently in the process of binding to a local address.
// 	 */
// 	BINDING,
// 
// 	/**
// 	 * The channel is currently bound to a local address.
// 	 */
// 	BOUND,
// 
// 	/**
// 	 * The channel is currently in the process of unbinding from a local
// 	 * address.
// 	 */
// 	UNBINDING;
// }
// 
// Path: src/main/java/com/yammer/httptunnel/util/TunnelIdGenerator.java
// public interface TunnelIdGenerator {
// 
// 	/**
// 	 * Generates the next tunnel ID to be used, which must be unique (i.e.
// 	 * ensure with high probability that it will not clash with an existing
// 	 * tunnel ID). This method must be thread safe, and preferably lock free.
// 	 */
// 	public String generateId();
// }


// Path: src/main/java/com/yammer/httptunnel/server/HttpTunnelServerChannel.java
import java.net.InetSocketAddress;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.jboss.netty.channel.AbstractServerChannel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineException;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelSink;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.socket.ServerSocketChannel;
import org.jboss.netty.channel.socket.ServerSocketChannelFactory;
import org.jboss.netty.logging.InternalLogger;
import org.jboss.netty.logging.InternalLoggerFactory;
import com.yammer.httptunnel.state.BindState;
import com.yammer.httptunnel.util.TunnelIdGenerator;

/*
 * Copyright 2011 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.yammer.httptunnel.server;




/**
 * The server end of an HTTP tunnel, created by an
 * {@link HttpTunnelServerChannelFactory}. Channels of this type are designed to
 * emulate a normal TCP based server socket channel as far as is feasible within
 * the limitations of the HTTP 1.0 protocol, and the usage patterns permitted by
 * commonly used HTTP proxies and firewalls.
 *
 * @author The Netty Project (netty-dev@lists.jboss.org)
 * @author Iain McGinniss (iain.mcginniss@onedrum.com)
 * @author Jamie Furness (jamie@onedrum.com)
 * @author OneDrum Ltd.
 */
public class HttpTunnelServerChannel extends AbstractServerChannel implements ServerSocketChannel {

	private static final InternalLogger LOG = InternalLoggerFactory.getInstance(HttpTunnelServerChannel.class);

	private static final Random random;

	static {
		random = new Random();
	}

	private final String tunnelIdPrefix;
	private final ConcurrentHashMap<String, HttpTunnelAcceptedChannel> tunnels;
	private final ServerSocketChannel realChannel;
	private final HttpTunnelServerChannelConfig config;

	private final AtomicBoolean opened;
	private final AtomicReference<BindState> bindState;