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);
}
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;
}
}
}
/**
+ * 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.
*