package com.orbekk.protobuf;

import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;

public class ConnectionHandler {
    private final Socket connection;
    private final BlockingQueue<Data.Response> dispatcherOutput;
    private final RequestDispatcher dispatcher;

    private class IncomingHandler implements Runnable {
        @Override public void run() {
            dispatcher.start();
            while (!connection.isClosed()) {
                try {
                    Data.Request r = Data.Request.parseDelimitedFrom(
                            connection.getInputStream());
                    if (r == null) {
                        tryCloseConnection();
                    } else {
                        try {
                            dispatcher.handleRequest(r);
                        } catch (InterruptedException e) {
                            tryCloseConnection();
                            return;
                        }
                    }
                } catch (IOException e) {
                    tryCloseConnection();
                }
            }
            dispatcher.interrupt();
        }
    }

    private class OutgoingHandler implements Runnable {
        @Override public void run() {
            while (!connection.isClosed()) {
                try {
                    Data.Response response = dispatcherOutput.take();
                    try {
                        response.writeDelimitedTo(connection.getOutputStream());
                    } catch (IOException e) {
                        tryCloseConnection();
                    }
                } catch (InterruptedException e) {
                    tryCloseConnection();
                }
            }
            dispatcher.interrupt();
        }
    }
    
    public static ConnectionHandler create(Socket connection,
            ExecutorService requestPool, ServiceHolder services) {
        BlockingQueue<Data.Response> dispatcherOutput =
                new ArrayBlockingQueue(RequestDispatcher.DEFAULT_QUEUE_SIZE);
        RequestDispatcher dispatcher = new RequestDispatcher(
                requestPool, dispatcherOutput, services);
        return new ConnectionHandler(connection, dispatcherOutput,
                dispatcher);
    }
    
    ConnectionHandler(Socket connection,
            BlockingQueue<Data.Response> dispatcherOutput,
            RequestDispatcher dispatcher) {
        this.connection = connection;
        this.dispatcherOutput = dispatcherOutput;
        this.dispatcher = dispatcher;
    }

    public void closeConnection() {
        tryCloseConnection();
    }
    
    private void tryCloseConnection() {
        try {
            connection.close();
        } catch (IOException e) {
            // Assume connection is closed.
        }
    }
    
    public Runnable createIncomingHandler() {
        return new IncomingHandler();
    }
    
    public Runnable createOutgoingHandler() {
        return new OutgoingHandler();
    }
}

@@ -23,16 +23,89 @@
 tobuf;%0A%0A
+import java.io.BufferedInputStream;%0Aimport java.io.BufferedOutputStream;%0A
 import j
@@ -591,24 +591,169 @@
 er.start();%0A
+            try %7B%0A                BufferedInputStream input = new BufferedInputStream(%0A                        connection.getInputStream());%0A    
             
@@ -785,38 +785,16 @@
 ed()) %7B%0A
-                try %7B%0A
         
@@ -887,35 +887,13 @@
     
-connection.getInputStream()
+input
 );%0A 
@@ -1292,32 +1292,46 @@
                %7D
+%0A            %7D
  catch (IOExcept
@@ -1331,36 +1331,32 @@
 OException e) %7B%0A
-    
                 
@@ -1377,34 +1377,16 @@
 tion();%0A
-                %7D%0A
         
@@ -1530,32 +1530,181 @@
 ic void run() %7B%0A
+            try %7B%0A                BufferedOutputStream output = new BufferedOutputStream(%0A                        connection.getOutputStream());%0A    
             whil
@@ -1732,38 +1732,16 @@
 ed()) %7B%0A
-                try %7B%0A
         
@@ -2032,32 +2032,46 @@
                %7D
+%0A            %7D
  catch (Interrup
@@ -2080,36 +2080,32 @@
 dException e) %7B%0A
-    
                 
@@ -2130,37 +2130,95 @@
 ();%0A            
-    %7D
+%7D catch (IOException e) %7B%0A                tryCloseConnection();
 %0A            %7D%0A 

Use buffered readers on server side.