Use logging for debug output.
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / FcpConnectionHandler.java
1 /*
2  * jFCPlib - FcpConnectionHandler.java - Copyright © 2008 David Roden
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 package net.pterodactylus.fcp;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.nio.ByteBuffer;
24 import java.nio.charset.Charset;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 import net.pterodactylus.util.logging.Logging;
29
30 /**
31  * Handles an FCP connection to a node.
32  *
33  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34  */
35 class FcpConnectionHandler implements Runnable {
36
37         /** The logger. */
38         private static final Logger logger = Logging.getLogger(FcpConnectionHandler.class.getName());
39
40         /** The underlying connection. */
41         private final FcpConnection fcpConnection;
42
43         /** The input stream from the node. */
44         private final InputStream remoteInputStream;
45
46         /** Whether to stop the connection handler. */
47         private boolean shouldStop;
48
49         /** Whether the next read line feed should be ignored. */
50         private boolean ignoreNextLinefeed;
51
52         /**
53          * Creates a new connection handler that operates on the given connection
54          * and input stream.
55          *
56          * @param fcpConnection
57          *            The underlying FCP connection
58          * @param remoteInputStream
59          *            The input stream from the node
60          */
61         public FcpConnectionHandler(FcpConnection fcpConnection, InputStream remoteInputStream) {
62                 this.fcpConnection = fcpConnection;
63                 this.remoteInputStream = remoteInputStream;
64         }
65
66         /**
67          * {@inheritDoc}
68          */
69         public void run() {
70                 FcpMessage fcpMessage = null;
71                 Throwable throwable = null;
72                 while (true) {
73                         synchronized (this) {
74                                 if (shouldStop) {
75                                         break;
76                                 }
77                         }
78                         try {
79                                 String line = readLine();
80                                 logger.log(Level.FINEST, "read line: %1$s", line);
81                                 if (line == null) {
82                                         break;
83                                 }
84                                 if (line.length() == 0) {
85                                         continue;
86                                 }
87                                 line = line.trim();
88                                 if (fcpMessage == null) {
89                                         fcpMessage = new FcpMessage(line);
90                                         continue;
91                                 }
92                                 if ("EndMessage".equalsIgnoreCase(line) || "Data".equalsIgnoreCase(line)) {
93                                         fcpConnection.handleMessage(fcpMessage);
94                                         fcpMessage = null;
95                                 }
96                                 int equalSign = line.indexOf('=');
97                                 if (equalSign == -1) {
98                                         /* something's fishy! */
99                                         continue;
100                                 }
101                                 String field = line.substring(0, equalSign);
102                                 String value = line.substring(equalSign + 1);
103                                 assert fcpMessage != null: "fcp message is null";
104                                 fcpMessage.setField(field, value);
105                         } catch (IOException ioe1) {
106                                 throwable = ioe1;
107                                 break;
108                         }
109                 }
110                 fcpConnection.handleDisconnect(throwable);
111         }
112
113         /**
114          * Stops the connection handler.
115          */
116         public void stop() {
117                 synchronized (this) {
118                         shouldStop = true;
119                 }
120         }
121
122         //
123         // PRIVATE METHODS
124         //
125
126         /**
127          * Reads bytes from {@link #remoteInputStream} until ‘\r’ or ‘\n’ are
128          * encountered and decodes the read bytes using UTF-8.
129          *
130          * @return The decoded line
131          * @throws IOException
132          *             if an I/O error occurs
133          */
134         private String readLine() throws IOException {
135                 byte[] readBytes = new byte[512];
136                 int readIndex = 0;
137                 while (true) {
138                         int nextByte = remoteInputStream.read();
139                         if (nextByte == -1) {
140                                 if (readIndex == 0) {
141                                         return null;
142                                 }
143                                 break;
144                         }
145                         if (nextByte == 10) {
146                                 if (!ignoreNextLinefeed) {
147                                         break;
148                                 }
149                         }
150                         ignoreNextLinefeed = false;
151                         if (nextByte == 13) {
152                                 ignoreNextLinefeed = true;
153                                 break;
154                         }
155                         if (readIndex == readBytes.length) {
156                                 /* recopy & enlarge array */
157                                 byte[] newReadBytes = new byte[readBytes.length * 2];
158                                 System.arraycopy(readBytes, 0, newReadBytes, 0, readBytes.length);
159                                 readBytes = newReadBytes;
160                         }
161                         readBytes[readIndex++] = (byte) nextByte;
162                 }
163                 ByteBuffer byteBuffer = ByteBuffer.wrap(readBytes, 0, readIndex);
164                 return Charset.forName("UTF-8").decode(byteBuffer).toString();
165         }
166
167 }