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