add FCPPluginReply
[jSite2.git] / src / net / pterodactylus / util / fcp / FcpConnectionHandler.java
index 2693c1f..8594c79 100644 (file)
@@ -25,26 +25,50 @@ import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 
 /**
- * TODO
+ * Handles an FCP connection to a node.
  * 
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  * @version $Id$
  */
 public class FcpConnectionHandler implements Runnable {
 
+       /** The underlying connection. */
        private final FcpConnection fcpConnection;
+
+       /** The input stream from the node. */
        private final InputStream remoteInputStream;
-       
+
+       /** Whether to stop the connection handler. */
+       private boolean shouldStop;
+
+       /** Whether the next read line feed should be ignored. */
        private boolean ignoreNextLinefeed;
 
+       /**
+        * Creates a new connection handler that operates on the given connection
+        * and input stream.
+        * 
+        * @param fcpConnection
+        *            The underlying FCP connection
+        * @param remoteInputStream
+        *            The input stream from the node
+        */
        public FcpConnectionHandler(FcpConnection fcpConnection, InputStream remoteInputStream) {
                this.fcpConnection = fcpConnection;
                this.remoteInputStream = remoteInputStream;
        }
 
+       /**
+        * {@inheritDoc}
+        */
        public void run() {
                FcpMessage fcpMessage = null;
                while (true) {
+                       synchronized (this) {
+                               if (shouldStop) {
+                                       break;
+                               }
+                       }
                        try {
                                String line = readLine();
                                System.out.println("read line: " + line);
@@ -70,13 +94,29 @@ public class FcpConnectionHandler implements Runnable {
                                }
                                String field = line.substring(0, equalSign);
                                String value = line.substring(equalSign + 1);
+                               assert fcpMessage != null : "fcp message is null";
                                fcpMessage.setField(field, value);
                        } catch (IOException e) {
+                               break;
                        }
                }
+               fcpConnection.handleDisconnect();
        }
 
        /**
+        * Stops the connection handler.
+        */
+       public void stop() {
+               synchronized (this) {
+                       shouldStop = true;
+               }
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
         * Reads bytes from {@link #remoteInputStream} until ‘\r’ or ‘\n’ are
         * encountered and decodes the read bytes using UTF-8.
         *