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