package pl.kungfoo.grizzly.http.client; import com.sun.grizzly.*; import com.sun.grizzly.util.WorkerThreadImpl; import com.sun.grizzly.filter.ReadFilter; import com.sun.grizzly.filter.LogFilter; import java.net.InetSocketAddress; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.util.concurrent.CountDownLatch; import java.util.logging.Level; /** * Hello world! */ public class App { public static void main(String[] args) throws IOException { Controller controller = new Controller(); ProtocolChain protocolChain = controller.getProtocolChainInstanceHandler().poll(); protocolChain.addFilter(new ReadFilter()); protocolChain.addFilter(new LogFilter()); ProtocolChainInstanceHandler pciHandler = new ProtocolChainInstanceHandler() { final private ProtocolChain protocolChain = new DefaultProtocolChain(); public ProtocolChain poll() { return protocolChain; } public boolean offer(ProtocolChain instance) { return true; } }; controller.setProtocolChainInstanceHandler(pciHandler); TCPSelectorHandler selectorHandler = new TCPSelectorHandler(true); selectorHandler.setSelectionKeyHandler(new DefaultSelectionKeyHandler()); controller.addSelectorHandler(selectorHandler); startController(controller); final TCPConnectorHandler tcpConnectorHandler = (TCPConnectorHandler) controller.acquireConnectorHandler( Controller.Protocol.TCP); final CountDownLatch anotherLatch = new CountDownLatch(1); CallbackHandler callbackHandler = new CallbackHandler() { public void onConnect(IOEvent ioEvent) { System.out.println("onConnect"); SelectionKey selectionKey = ioEvent.attachment().getSelectionKey(); try { tcpConnectorHandler.finishConnect(selectionKey); System.out.println("Done finishconnect"); } catch (IOException e) { System.out.println("onConnect: " + e.getMessage()); e.printStackTrace(System.out); } anotherLatch.countDown(); ioEvent.attachment().getSelectorHandler().register(selectionKey, SelectionKey.OP_READ); } public void onRead(IOEvent ioEvent) { Context context = ioEvent.attachment(); try { context.getProtocolChain().execute(context); } catch (Exception e) { e.printStackTrace(); } } /** {@inheritDoc} */ public void onWrite(IOEvent ioEvent) { } }; tcpConnectorHandler .connect(new InetSocketAddress("localhost", 8080), new DefaultCallbackHandler(tcpConnectorHandler)); /* try { anotherLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); }*/ byte[] bytes = "GET \"/index.html\" HTTP/1.0\n\n".getBytes(); long written = tcpConnectorHandler.write(ByteBuffer.wrap(bytes), true); if (written != bytes.length) { System.out.println("Written " + written + ", supposed to: " + bytes.length); } ByteBuffer buffer = ByteBuffer.allocateDirect(2048); long l = tcpConnectorHandler.read(buffer, true); System.out.println(l); System.out.println(buffer.toString()); tcpConnectorHandler.close(); controller.stop(); } private static void startController(Controller controller) { final CountDownLatch latch = new CountDownLatch(1); controller.addStateListener(new ControllerStateListenerAdapter() { @Override public void onReady() { System.out.println("Ready."); latch.countDown(); } @Override public void onException(Throwable e) { if (latch.getCount() > 0) { Controller.logger().log(Level.SEVERE, "Exception during " + "starting the controller", e); latch.countDown(); } else { Controller.logger().log(Level.SEVERE, "Exception during " + "controller processing", e); } } }); new WorkerThreadImpl("ControllerWorker", controller).start(); try { latch.await(); System.out.println("Controller started."); } catch (InterruptedException ex) { } if (!controller.isStarted()) { throw new IllegalStateException("Controller is not started!"); } } }