// Path: src/main/java/bbejeck/nio/channels/AsyncServerSocket.java
// public class AsyncServerSocket {
// 
//     private int count = 0;
//     private CountDownLatch latch = new CountDownLatch(1);
//     private int port;
//     private String host;
//     private AsynchronousChannelGroup channelGroup;
//     private AsynchronousServerSocketChannel server;
// 
//     @Inject
//     public AsyncServerSocket(@Port int port, @Host String host) throws Exception {
//         this.port = port;
//         this.host = host;
//     }
// 
//     public void startServer() throws Exception {
//         ExecutorService executorService = Executors.newCachedThreadPool();
//         channelGroup = AsynchronousChannelGroup.withThreadPool(executorService);
//         server = AsynchronousServerSocketChannel.open(channelGroup);
//         server.bind(new InetSocketAddress(host, port));
//         server.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() {
//             @Override
//             public void completed(AsynchronousSocketChannel result, Object attachment) {
//                 server.accept(null, this);
//                 processConnection(result);
//             }
// 
//             @Override
//             public void failed(Throwable exc, Object attachment) {
//                 exc.printStackTrace();
//             }
//         });
//         latch.await();
//         channelGroup.shutdown();
//         executorService.shutdownNow();
//         System.out.println("AsyncServer shutting down");
//     }
// 
//     private void processConnection(AsynchronousSocketChannel socket) {
//         ByteBuffer byteBuffer = ByteBuffer.allocate(1000);
//         Future<Integer> read = socket.read(byteBuffer);
//         try {
//             int totalBytes = read.get();
//             String message = getMessageFromBuffer(byteBuffer, totalBytes);
// //            System.out.println("Message[" + message + "] received @" + new Date());
//             sendEchoResponse(byteBuffer, socket, message);
//             if (message.equals("quit")) {
//                 quit();
//             }
//         } catch (InterruptedException | ExecutionException e) {
//             quit();
//             throw new RuntimeException(e);
//         }
//     }
// 
//     private void sendEchoResponse(ByteBuffer byteBuffer, AsynchronousSocketChannel socket, String originalMessage) {
//         StringBuilder builder = new StringBuilder("You Said >> \"");
//         builder.append(originalMessage).append("\"");
//         byteBuffer.clear();
//         byteBuffer.put(builder.toString().getBytes(Charset.forName("UTF-8")));
//         byteBuffer.flip();
//         socket.write(byteBuffer);
//     }
// 
// 
//     private String getMessageFromBuffer(ByteBuffer byteBuffer, int size) {
//         byte[] bytes = new byte[size];
//         byteBuffer.flip();
//         byteBuffer.get(bytes, 0, size);
//         return new String(bytes, Charset.forName("UTF-8"));
//     }
// 
//     private void quit() {
//         latch.countDown();
//     }
// 
// }
// 
// Path: src/main/java/bbejeck/nio/channels/AsyncServerTestModule.java
// public class AsyncServerTestModule extends AbstractModule {
// 
//     @Override
//     protected void configure() {
//         bind(AsyncServerSocket.class);
//     }
// }
// 
// Path: src/main/java/bbejeck/nio/sockets/PlainSocketMessageSender.java
// public class PlainSocketMessageSender {
// 
//     private Socket socket;
//     private int port;
//     private InetSocketAddress socketAddress;
//     private int messageCount;
// 
//     @Inject
//     public PlainSocketMessageSender(@Port int port, @MessageCount int messageCount, @Host String host) {
//         this.port = port;
//         socketAddress = new InetSocketAddress(host, port);
//         this.messageCount = messageCount;
//     }
// 
//     public void sendMessages() throws Exception {
//         for (int i = 0; i < messageCount; i++) {
//             sendTextMessage("Plain text message from socket");
//         }
//         sendTextMessage("quit");
//     }
// 
//     private void sendTextMessage(String message) throws Exception {
//         socket = new Socket();
//         socket.connect(socketAddress);
//         BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//         bufferedWriter.write(message);
//         bufferedWriter.flush();
//         BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//         CharBuffer charBuffer = CharBuffer.allocate(1000);
//         reader.read(charBuffer);
//         charBuffer.flip();
// //        System.out.println("Reply from server "+charBuffer.toString());
//         reader.close();
//         bufferedWriter.close();
//         socket.close();
//     }
// }
// 
// Path: src/main/java/bbejeck/nio/sockets/PlainSocketModule.java
// public class PlainSocketModule extends AbstractModule {
// 
//     @Override
//     protected void configure() {
//         bind(PlainServerSocket.class);
//         bind(PlainSocketMessageSender.class);
//     }
// }


import bbejeck.nio.channels.AsyncServerSocket;
import bbejeck.nio.channels.AsyncServerTestModule;
import bbejeck.nio.sockets.PlainSocketMessageSender;
import bbejeck.nio.sockets.PlainSocketModule;
import com.google.common.base.Stopwatch;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
package bbejeck.nio;




/**
 * Created by IntelliJ IDEA.
 * User: bbejeck
 * Date: 3/14/12
 * Time: 9:48 PM
 */
public class AsyncSocketTestDriver {


    public static void main(String[] args) throws Exception {
// Path: src/main/java/bbejeck/nio/channels/AsyncServerSocket.java
// public class AsyncServerSocket {
// 
//     private int count = 0;
//     private CountDownLatch latch = new CountDownLatch(1);
//     private int port;
//     private String host;
//     private AsynchronousChannelGroup channelGroup;
//     private AsynchronousServerSocketChannel server;
// 
//     @Inject
//     public AsyncServerSocket(@Port int port, @Host String host) throws Exception {
//         this.port = port;
//         this.host = host;
//     }
// 
//     public void startServer() throws Exception {
//         ExecutorService executorService = Executors.newCachedThreadPool();
//         channelGroup = AsynchronousChannelGroup.withThreadPool(executorService);
//         server = AsynchronousServerSocketChannel.open(channelGroup);
//         server.bind(new InetSocketAddress(host, port));
//         server.accept(null, new CompletionHandler<AsynchronousSocketChannel, Object>() {
//             @Override
//             public void completed(AsynchronousSocketChannel result, Object attachment) {
//                 server.accept(null, this);
//                 processConnection(result);
//             }
// 
//             @Override
//             public void failed(Throwable exc, Object attachment) {
//                 exc.printStackTrace();
//             }
//         });
//         latch.await();
//         channelGroup.shutdown();
//         executorService.shutdownNow();
//         System.out.println("AsyncServer shutting down");
//     }
// 
//     private void processConnection(AsynchronousSocketChannel socket) {
//         ByteBuffer byteBuffer = ByteBuffer.allocate(1000);
//         Future<Integer> read = socket.read(byteBuffer);
//         try {
//             int totalBytes = read.get();
//             String message = getMessageFromBuffer(byteBuffer, totalBytes);
// //            System.out.println("Message[" + message + "] received @" + new Date());
//             sendEchoResponse(byteBuffer, socket, message);
//             if (message.equals("quit")) {
//                 quit();
//             }
//         } catch (InterruptedException | ExecutionException e) {
//             quit();
//             throw new RuntimeException(e);
//         }
//     }
// 
//     private void sendEchoResponse(ByteBuffer byteBuffer, AsynchronousSocketChannel socket, String originalMessage) {
//         StringBuilder builder = new StringBuilder("You Said >> \"");
//         builder.append(originalMessage).append("\"");
//         byteBuffer.clear();
//         byteBuffer.put(builder.toString().getBytes(Charset.forName("UTF-8")));
//         byteBuffer.flip();
//         socket.write(byteBuffer);
//     }
// 
// 
//     private String getMessageFromBuffer(ByteBuffer byteBuffer, int size) {
//         byte[] bytes = new byte[size];
//         byteBuffer.flip();
//         byteBuffer.get(bytes, 0, size);
//         return new String(bytes, Charset.forName("UTF-8"));
//     }
// 
//     private void quit() {
//         latch.countDown();
//     }
// 
// }
// 
// Path: src/main/java/bbejeck/nio/channels/AsyncServerTestModule.java
// public class AsyncServerTestModule extends AbstractModule {
// 
//     @Override
//     protected void configure() {
//         bind(AsyncServerSocket.class);
//     }
// }
// 
// Path: src/main/java/bbejeck/nio/sockets/PlainSocketMessageSender.java
// public class PlainSocketMessageSender {
// 
//     private Socket socket;
//     private int port;
//     private InetSocketAddress socketAddress;
//     private int messageCount;
// 
//     @Inject
//     public PlainSocketMessageSender(@Port int port, @MessageCount int messageCount, @Host String host) {
//         this.port = port;
//         socketAddress = new InetSocketAddress(host, port);
//         this.messageCount = messageCount;
//     }
// 
//     public void sendMessages() throws Exception {
//         for (int i = 0; i < messageCount; i++) {
//             sendTextMessage("Plain text message from socket");
//         }
//         sendTextMessage("quit");
//     }
// 
//     private void sendTextMessage(String message) throws Exception {
//         socket = new Socket();
//         socket.connect(socketAddress);
//         BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//         bufferedWriter.write(message);
//         bufferedWriter.flush();
//         BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//         CharBuffer charBuffer = CharBuffer.allocate(1000);
//         reader.read(charBuffer);
//         charBuffer.flip();
// //        System.out.println("Reply from server "+charBuffer.toString());
//         reader.close();
//         bufferedWriter.close();
//         socket.close();
//     }
// }
// 
// Path: src/main/java/bbejeck/nio/sockets/PlainSocketModule.java
// public class PlainSocketModule extends AbstractModule {
// 
//     @Override
//     protected void configure() {
//         bind(PlainServerSocket.class);
//         bind(PlainSocketMessageSender.class);
//     }
// }


// Path: src/main/java/bbejeck/nio/AsyncSocketTestDriver.java
import bbejeck.nio.channels.AsyncServerSocket;
import bbejeck.nio.channels.AsyncServerTestModule;
import bbejeck.nio.sockets.PlainSocketMessageSender;
import bbejeck.nio.sockets.PlainSocketModule;
import com.google.common.base.Stopwatch;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

package bbejeck.nio;




/**
 * Created by IntelliJ IDEA.
 * User: bbejeck
 * Date: 3/14/12
 * Time: 9:48 PM
 */
public class AsyncSocketTestDriver {


    public static void main(String[] args) throws Exception {
        Injector injector = Guice.createInjector(new SocketModule(), new PlainSocketModule(), new AsyncServerTestModule());