X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Futil%2Ffcp%2FFcpConnectionHandler.java;h=67dd65ea3d52e65e84e69a5d10d541f88cd638f5;hb=474a0f39c87d520b0ce8c23ea8e8046441e13ea7;hp=2693c1f1e03d5cf5422ae166e1ec4aaa729bc6aa;hpb=90f3a1c8e0c85f19c82130e49d7a605cb9e55286;p=jSite2.git diff --git a/src/net/pterodactylus/util/fcp/FcpConnectionHandler.java b/src/net/pterodactylus/util/fcp/FcpConnectionHandler.java index 2693c1f..67dd65e 100644 --- a/src/net/pterodactylus/util/fcp/FcpConnectionHandler.java +++ b/src/net/pterodactylus/util/fcp/FcpConnectionHandler.java @@ -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,28 @@ 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; } } } /** + * 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. *