ad3e3f3a876efa709e349237ca4aac1bfb75638f
[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 /**
29  * Handles an FCP connection to a node.
30  *
31  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32  */
33 class FcpConnectionHandler implements Runnable {
34
35         /** The logger. */
36         private static final Logger logger = Logger.getLogger(FcpConnectionHandler.class.getName());
37
38         /** The underlying connection. */
39         private final FcpConnection fcpConnection;
40
41         /** The input stream from the node. */
42         private final InputStream remoteInputStream;
43
44         /** Whether to stop the connection handler. */
45         private boolean shouldStop;
46
47         /** Whether the next read line feed should be ignored. */
48         private boolean ignoreNextLinefeed;
49
50         /**
51          * Creates a new connection handler that operates on the given connection
52          * and input stream.
53          *
54          * @param fcpConnection
55          *            The underlying FCP connection
56          * @param remoteInputStream
57          *            The input stream from the node
58          */
59         public FcpConnectionHandler(FcpConnection fcpConnection, InputStream remoteInputStream) {
60                 this.fcpConnection = fcpConnection;
61                 this.remoteInputStream = remoteInputStream;
62         }
63
64         /**
65          * {@inheritDoc}
66          */
67         @Override
68         public void run() {
69                 FcpMessage fcpMessage = null;
70                 Throwable throwable = null;
71                 while (true) {
72                         synchronized (this) {
73                                 if (shouldStop) {
74                                         break;
75                                 }
76                         }
77                         try {
78                                 String line = readLine();
79                                 logger.log(Level.FINEST, String.format("read line: %1$s", line));
80                                 if (line == null) {
81                                         break;
82                                 }
83                                 if (line.length() == 0) {
84                                         continue;
85                                 }
86                                 line = line.trim();
87                                 if (fcpMessage == null) {
88                                         fcpMessage = new FcpMessage(line);
89                                         continue;
90                                 }
91                                 if ("EndMessage".equalsIgnoreCase(line) || "Data".equalsIgnoreCase(line)) {
92                                         fcpConnection.handleMessage(fcpMessage);
93                                         fcpMessage = null;
94                                 }
95                                 int equalSign = line.indexOf('=');
96                                 if (equalSign == -1) {
97                                         /* something's fishy! */
98                                         continue;
99                                 }
100                                 String field = line.substring(0, equalSign);
101                                 String value = line.substring(equalSign + 1);
102                                 assert fcpMessage != null: "fcp message is null";
103                                 fcpMessage.setField(field, value);
104                         } catch (IOException ioe1) {
105                                 throwable = ioe1;
106                                 break;
107                         }
108                 }
109                 fcpConnection.handleDisconnect(throwable);
110         }
111
112         /**
113          * Stops the connection handler.
114          */
115         public void stop() {
116                 synchronized (this) {
117                         shouldStop = true;
118                 }
119         }
120
121         //
122         // PRIVATE METHODS
123         //
124
125         /**
126          * Reads bytes from {@link #remoteInputStream} until ‘\r’ or ‘\n’ are
127          * encountered and decodes the read bytes using UTF-8.
128          *
129          * @return The decoded line
130          * @throws IOException
131          *             if an I/O error occurs
132          */
133         private String readLine() throws IOException {
134                 byte[] readBytes = new byte[512];
135                 int readIndex = 0;
136                 while (true) {
137                         int nextByte = remoteInputStream.read();
138                         if (nextByte == -1) {
139                                 if (readIndex == 0) {
140                                         return null;
141                                 }
142                                 break;
143                         }
144                         if (nextByte == 10) {
145                                 if (!ignoreNextLinefeed) {
146                                         break;
147                                 }
148                         }
149                         ignoreNextLinefeed = false;
150                         if (nextByte == 13) {
151                                 ignoreNextLinefeed = true;
152                                 break;
153                         }
154                         if (readIndex == readBytes.length) {
155                                 /* recopy & enlarge array */
156                                 byte[] newReadBytes = new byte[readBytes.length * 2];
157                                 System.arraycopy(readBytes, 0, newReadBytes, 0, readBytes.length);
158                                 readBytes = newReadBytes;
159                         }
160                         readBytes[readIndex++] = (byte) nextByte;
161                 }
162                 ByteBuffer byteBuffer = ByteBuffer.wrap(readBytes, 0, readIndex);
163                 return Charset.forName("UTF-8").decode(byteBuffer).toString();
164         }
165
166 }