public void run() { String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); String host = uri.getHost() == null ? "localhost" : uri.getHost(); int port = uri.getPort(); if (port == -1) { if (scheme.equalsIgnoreCase("http")) { port = 80; } else if (scheme.equalsIgnoreCase("https")) { port = 443; } } if (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")) { logger.error("Only HTTP(S) is supported."); return; } boolean ssl = scheme.equalsIgnoreCase("https"); // Configure the client. ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool())); // Set up the event pipeline factory. bootstrap.setPipelineFactory(new HttpSnoopClientPipelineFactory(ssl)); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection attempt succeeds or fails. Channel channel = future.awaitUninterruptibly().getChannel(); if (!future.isSuccess()) { future.getCause().printStackTrace(); bootstrap.releaseExternalResources(); return; } // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath()); request.setHeader(HttpHeaders.Names.HOST, host); request.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); request.setHeader(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); // Set some example cookies. CookieEncoder httpCookieEncoder = new CookieEncoder(false); httpCookieEncoder.addCookie("my-cookie", "foo"); httpCookieEncoder.addCookie("another-cookie", "bar"); request.setHeader(HttpHeaders.Names.COOKIE, httpCookieEncoder.encode()); // Send the HTTP request. channel.write(request); // Wait for the server to close the connection. channel.getCloseFuture().awaitUninterruptibly(); // Shut down executor threads to exit. bootstrap.releaseExternalResources(); }
private void testSpdyEcho(int version) throws Throwable { ServerBootstrap sb = new ServerBootstrap(newServerSocketChannelFactory(executor)); ClientBootstrap cb = new ClientBootstrap(newClientSocketChannelFactory(executor)); ChannelBuffer frames = createFrames(version); EchoHandler sh = new EchoHandler(frames, true); EchoHandler ch = new EchoHandler(frames, false); sb.getPipeline().addLast("decoder", new SpdyFrameDecoder(version)); sb.getPipeline().addLast("encoder", new SpdyFrameEncoder(version)); sb.getPipeline().addLast("handler", sh); cb.getPipeline().addLast("handler", ch); Channel sc = sb.bind(new InetSocketAddress(0)); int port = ((InetSocketAddress) sc.getLocalAddress()).getPort(); ChannelFuture ccf = cb.connect(new InetSocketAddress(InetAddress.getLocalHost(), port)); assertTrue(ccf.awaitUninterruptibly().isSuccess()); Channel cc = ccf.getChannel(); cc.write(frames); while (ch.counter < frames.writerIndex() - ignoredBytes) { if (sh.exception.get() != null) { break; } if (ch.exception.get() != null) { break; } try { Thread.sleep(1); } catch (InterruptedException e) { // Ignore. } } sh.channel.close().awaitUninterruptibly(); ch.channel.close().awaitUninterruptibly(); sc.close().awaitUninterruptibly(); if (sh.exception.get() != null && !(sh.exception.get() instanceof IOException)) { throw sh.exception.get(); } if (ch.exception.get() != null && !(ch.exception.get() instanceof IOException)) { throw ch.exception.get(); } if (sh.exception.get() != null) { throw sh.exception.get(); } if (ch.exception.get() != null) { throw ch.exception.get(); } }
@Test public void testShutdownTime() throws Throwable { ServerSocketChannel serverSocket = ServerSocketChannel.open(); serverSocket.socket().bind(new InetSocketAddress(0)); ClientBootstrap b = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); b.getPipeline().addLast("handler", new DummyHandler()); long startTime; long stopTime; try { serverSocket.configureBlocking(false); ChannelFuture f = b.connect( new InetSocketAddress( SocketAddresses.LOCALHOST, serverSocket.socket().getLocalPort())); serverSocket.accept(); f.awaitUninterruptibly(); if (f.getCause() != null) { throw f.getCause(); } assertTrue(f.isSuccess()); startTime = System.currentTimeMillis(); f.getChannel().close().awaitUninterruptibly(); } finally { b.getFactory().releaseExternalResources(); stopTime = System.currentTimeMillis(); try { serverSocket.close(); } catch (IOException ex) { // Ignore. } } long shutdownTime = stopTime - startTime; assertTrue("Shutdown takes too long: " + shutdownTime + " ms", shutdownTime < 500); }