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