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