2 * jSite2 - FcpConnectionHandler.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.util.fcp;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.nio.ByteBuffer;
25 import java.nio.charset.Charset;
28 * Handles an FCP connection to a node.
30 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
33 public class FcpConnectionHandler implements Runnable {
35 /** The underlying connection. */
36 private final FcpConnection fcpConnection;
38 /** The input stream from the node. */
39 private final InputStream remoteInputStream;
41 /** Whether to stop the connection handler. */
42 private boolean shouldStop;
44 /** Whether the next read line feed should be ignored. */
45 private boolean ignoreNextLinefeed;
48 * Creates a new connection handler that operates on the given connection
51 * @param fcpConnection
52 * The underlying FCP connection
53 * @param remoteInputStream
54 * The input stream from the node
56 public FcpConnectionHandler(FcpConnection fcpConnection, InputStream remoteInputStream) {
57 this.fcpConnection = fcpConnection;
58 this.remoteInputStream = remoteInputStream;
65 FcpMessage fcpMessage = null;
73 String line = readLine();
74 System.out.println("read line: " + line);
78 if (line.length() == 0) {
82 if (fcpMessage == null) {
83 fcpMessage = new FcpMessage(line);
86 if ("EndMessage".equals(line)) {
87 fcpConnection.handleMessage(fcpMessage);
90 int equalSign = line.indexOf('=');
91 if (equalSign == -1) {
92 /* something's fishy! */
95 String field = line.substring(0, equalSign);
96 String value = line.substring(equalSign + 1);
97 assert fcpMessage != null : "fcp message is null";
98 fcpMessage.setField(field, value);
99 } catch (IOException e) {
103 fcpConnection.handleDisconnect();
107 * Stops the connection handler.
110 synchronized (this) {
120 * Reads bytes from {@link #remoteInputStream} until ‘\r’ or ‘\n’ are
121 * encountered and decodes the read bytes using UTF-8.
123 * @return The decoded line
124 * @throws IOException
125 * if an I/O error occurs
127 private String readLine() throws IOException {
128 byte[] readBytes = new byte[512];
131 int nextByte = remoteInputStream.read();
132 if (nextByte == -1) {
133 if (readIndex == 0) {
138 if (nextByte == 10) {
139 if (!ignoreNextLinefeed) {
143 ignoreNextLinefeed = false;
144 if (nextByte == 13) {
145 ignoreNextLinefeed = true;
148 if (readIndex == readBytes.length) {
149 /* recopy & enlarge array */
150 byte[] newReadBytes = new byte[readBytes.length * 2];
151 System.arraycopy(readBytes, 0, newReadBytes, 0, readBytes.length);
152 readBytes = newReadBytes;
154 readBytes[readIndex++] = (byte) nextByte;
156 ByteBuffer byteBuffer = ByteBuffer.wrap(readBytes, 0, readIndex);
157 return Charset.forName("UTF-8").decode(byteBuffer).toString();