// Path: src/main/java/com/yammer/httptunnel/client/HttpTunnelClientChannelFactory.java
// public class HttpTunnelClientChannelFactory implements ClientSocketChannelFactory {
// 
// 	private final ClientSocketChannelFactory factory;
// 	private final ChannelGroup realConnections;
// 
// 	public HttpTunnelClientChannelFactory(ClientSocketChannelFactory factory) {
// 		this.factory = factory;
// 
// 		realConnections = new DefaultChannelGroup();
// 	}
// 
// 	@Override
// 	public HttpTunnelClientChannel newChannel(ChannelPipeline pipeline) {
// 		return new HttpTunnelClientChannel(this, pipeline, new HttpTunnelClientChannelSink(), factory, realConnections);
// 	}
// 
// 	@Override
// 	public void releaseExternalResources() {
// 		factory.releaseExternalResources();
// 	}
// }
// 
// Path: src/main/java/com/yammer/httptunnel/server/HttpTunnelServerChannelFactory.java
// public class HttpTunnelServerChannelFactory implements ServerSocketChannelFactory {
// 
// 	private final ServerSocketChannelFactory factory;
// 	private final ChannelGroup realConnections;
// 
// 	public HttpTunnelServerChannelFactory(ServerSocketChannelFactory factory) {
// 		this.factory = factory;
// 
// 		realConnections = new DefaultChannelGroup();
// 	}
// 
// 	@Override
// 	public HttpTunnelServerChannel newChannel(ChannelPipeline pipeline) {
// 		return new HttpTunnelServerChannel(this, pipeline, new HttpTunnelServerChannelSink(), factory, realConnections);
// 	}
// 
// 	@Override
// 	public void releaseExternalResources() {
// 		factory.releaseExternalResources();
// 	}
// }


import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelState;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.DownstreamMessageEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.UpstreamMessageEvent;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.ServerSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import com.yammer.httptunnel.client.HttpTunnelClientChannelFactory;
import com.yammer.httptunnel.server.HttpTunnelServerChannelFactory;
			return new InetSocketAddress(InetAddress.getByAddress(addr), port);
		}
		catch (UnknownHostException e) {
			throw new RuntimeException("Bad address in test");
		}
	}

	public static Throwable checkIsExceptionEvent(ChannelEvent ev) {
		assertTrue(ev instanceof ExceptionEvent);
		ExceptionEvent exceptionEv = (ExceptionEvent) ev;
		return exceptionEv.getCause();
	}

	public static ChannelStateEvent checkIsStateEvent(ChannelEvent event, ChannelState expectedState, Object expectedValue) {
		assertTrue(event instanceof ChannelStateEvent);
		ChannelStateEvent stateEvent = (ChannelStateEvent) event;
		Assert.assertEquals(expectedState, stateEvent.getState());
		Assert.assertEquals(expectedValue, stateEvent.getValue());
		return stateEvent;
	}

    public static InetAddress getLocalHost() throws UnknownHostException {
        return InetAddress.getLocalHost();
    }

	public static Channel createServerChannel(InetSocketAddress addr, ChannelPipelineFactory pipelineFactory) {
		// TCP socket factory
		ServerSocketChannelFactory socketFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

		// HTTP socket factory
// Path: src/main/java/com/yammer/httptunnel/client/HttpTunnelClientChannelFactory.java
// public class HttpTunnelClientChannelFactory implements ClientSocketChannelFactory {
// 
// 	private final ClientSocketChannelFactory factory;
// 	private final ChannelGroup realConnections;
// 
// 	public HttpTunnelClientChannelFactory(ClientSocketChannelFactory factory) {
// 		this.factory = factory;
// 
// 		realConnections = new DefaultChannelGroup();
// 	}
// 
// 	@Override
// 	public HttpTunnelClientChannel newChannel(ChannelPipeline pipeline) {
// 		return new HttpTunnelClientChannel(this, pipeline, new HttpTunnelClientChannelSink(), factory, realConnections);
// 	}
// 
// 	@Override
// 	public void releaseExternalResources() {
// 		factory.releaseExternalResources();
// 	}
// }
// 
// Path: src/main/java/com/yammer/httptunnel/server/HttpTunnelServerChannelFactory.java
// public class HttpTunnelServerChannelFactory implements ServerSocketChannelFactory {
// 
// 	private final ServerSocketChannelFactory factory;
// 	private final ChannelGroup realConnections;
// 
// 	public HttpTunnelServerChannelFactory(ServerSocketChannelFactory factory) {
// 		this.factory = factory;
// 
// 		realConnections = new DefaultChannelGroup();
// 	}
// 
// 	@Override
// 	public HttpTunnelServerChannel newChannel(ChannelPipeline pipeline) {
// 		return new HttpTunnelServerChannel(this, pipeline, new HttpTunnelServerChannelSink(), factory, realConnections);
// 	}
// 
// 	@Override
// 	public void releaseExternalResources() {
// 		factory.releaseExternalResources();
// 	}
// }


// Path: src/test/java/com/yammer/httptunnel/util/NettyTestUtils.java
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import junit.framework.Assert;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelEvent;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelState;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.DownstreamMessageEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.UpstreamMessageEvent;
import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
import org.jboss.netty.channel.socket.ServerSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import com.yammer.httptunnel.client.HttpTunnelClientChannelFactory;
import com.yammer.httptunnel.server.HttpTunnelServerChannelFactory;

			return new InetSocketAddress(InetAddress.getByAddress(addr), port);
		}
		catch (UnknownHostException e) {
			throw new RuntimeException("Bad address in test");
		}
	}

	public static Throwable checkIsExceptionEvent(ChannelEvent ev) {
		assertTrue(ev instanceof ExceptionEvent);
		ExceptionEvent exceptionEv = (ExceptionEvent) ev;
		return exceptionEv.getCause();
	}

	public static ChannelStateEvent checkIsStateEvent(ChannelEvent event, ChannelState expectedState, Object expectedValue) {
		assertTrue(event instanceof ChannelStateEvent);
		ChannelStateEvent stateEvent = (ChannelStateEvent) event;
		Assert.assertEquals(expectedState, stateEvent.getState());
		Assert.assertEquals(expectedValue, stateEvent.getValue());
		return stateEvent;
	}

    public static InetAddress getLocalHost() throws UnknownHostException {
        return InetAddress.getLocalHost();
    }

	public static Channel createServerChannel(InetSocketAddress addr, ChannelPipelineFactory pipelineFactory) {
		// TCP socket factory
		ServerSocketChannelFactory socketFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

		// HTTP socket factory
		socketFactory = new HttpTunnelServerChannelFactory(socketFactory);