2f407858a2591b549d8ede2890b769f32c2e2a6e
[jFCPlib.git] / src / main / java / net / pterodactylus / fcp / FcpConnection.java
1 /*
2  * jFCPlib - FpcConnection.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.Closeable;
22 import java.io.FilterInputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.net.InetAddress;
27 import java.net.Socket;
28 import java.net.UnknownHostException;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.logging.Logger;
33
34 /**
35  * An FCP connection to a Freenet node.
36  *
37  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
38  */
39 public class FcpConnection implements Closeable {
40
41         /** Logger. */
42         private static final Logger logger = Logger.getLogger(FcpConnection.class.getName());
43
44         /** The default port for FCP v2. */
45         public static final int DEFAULT_PORT = 9481;
46
47         /** Listener management. */
48         private final FcpListenerManager fcpListenerManager = new FcpListenerManager(this);
49
50         /** The address of the node. */
51         private final InetAddress address;
52
53         /** The port number of the node’s FCP port. */
54         private final int port;
55
56         /** The remote socket. */
57         private Socket remoteSocket;
58
59         /** The input stream from the node. */
60         private InputStream remoteInputStream;
61
62         /** The output stream to the node. */
63         private OutputStream remoteOutputStream;
64
65         /** The connection handler. */
66         private FcpConnectionHandler connectionHandler;
67
68         /** Incoming message statistics. */
69         private Map<String, Integer> incomingMessageStatistics = Collections.synchronizedMap(new HashMap<String, Integer>());
70
71         /**
72          * Creates a new FCP connection to the freenet node running on localhost,
73          * using the default port.
74          *
75          * @throws UnknownHostException
76          *             if the hostname can not be resolved
77          */
78         public FcpConnection() throws UnknownHostException {
79                 this(InetAddress.getLocalHost());
80         }
81
82         /**
83          * Creates a new FCP connection to the Freenet node running on the given
84          * host, listening on the default port.
85          *
86          * @param host
87          *            The hostname of the Freenet node
88          * @throws UnknownHostException
89          *             if <code>host</code> can not be resolved
90          */
91         public FcpConnection(String host) throws UnknownHostException {
92                 this(host, DEFAULT_PORT);
93         }
94
95         /**
96          * Creates a new FCP connection to the Freenet node running on the given
97          * host, listening on the given port.
98          *
99          * @param host
100          *            The hostname of the Freenet node
101          * @param port
102          *            The port number of the node’s FCP port
103          * @throws UnknownHostException
104          *             if <code>host</code> can not be resolved
105          */
106         public FcpConnection(String host, int port) throws UnknownHostException {
107                 this(InetAddress.getByName(host), port);
108         }
109
110         /**
111          * Creates a new FCP connection to the Freenet node running at the given
112          * address, listening on the default port.
113          *
114          * @param address
115          *            The address of the Freenet node
116          */
117         public FcpConnection(InetAddress address) {
118                 this(address, DEFAULT_PORT);
119         }
120
121         /**
122          * Creates a new FCP connection to the Freenet node running at the given
123          * address, listening on the given port.
124          *
125          * @param address
126          *            The address of the Freenet node
127          * @param port
128          *            The port number of the node’s FCP port
129          */
130         public FcpConnection(InetAddress address, int port) {
131                 this.address = address;
132                 this.port = port;
133         }
134
135         //
136         // LISTENER MANAGEMENT
137         //
138
139         /**
140          * Adds the given listener to the list of listeners.
141          *
142          * @param fcpListener
143          *            The listener to add
144          */
145         public void addFcpListener(FcpListener fcpListener) {
146                 fcpListenerManager.addListener(fcpListener);
147         }
148
149         /**
150          * Removes the given listener from the list of listeners.
151          *
152          * @param fcpListener
153          *            The listener to remove
154          */
155         public void removeFcpListener(FcpListener fcpListener) {
156                 fcpListenerManager.removeListener(fcpListener);
157         }
158
159         //
160         // ACTIONS
161         //
162
163         /**
164          * Connects to the node.
165          *
166          * @throws IOException
167          *             if an I/O error occurs
168          * @throws IllegalStateException
169          *             if there is already a connection to the node
170          */
171         public synchronized void connect() throws IOException, IllegalStateException {
172                 if (connectionHandler != null) {
173                         throw new IllegalStateException("already connected, disconnect first");
174                 }
175                 logger.info("connecting to " + address + ":" + port + "…");
176                 remoteSocket = new Socket(address, port);
177                 remoteInputStream = remoteSocket.getInputStream();
178                 remoteOutputStream = remoteSocket.getOutputStream();
179                 new Thread(connectionHandler = new FcpConnectionHandler(this, remoteInputStream)).start();
180         }
181
182         /**
183          * Disconnects from the node. If there is no connection to the node, this
184          * method does nothing.
185          *
186          * @deprecated Use {@link #close()} instead
187          */
188         @Deprecated
189         public synchronized void disconnect() {
190                 close();
191         }
192
193         /**
194          * Closes the connection. If there is no connection to the node, this method
195          * does nothing.
196          */
197         public void close() {
198                 handleDisconnect(null);
199         }
200
201         /**
202          * Sends the given FCP message.
203          *
204          * @param fcpMessage
205          *            The FCP message to send
206          * @throws IOException
207          *             if an I/O error occurs
208          */
209         public synchronized void sendMessage(FcpMessage fcpMessage) throws IOException {
210                 logger.fine("sending message: " + fcpMessage.getName());
211                 fcpMessage.write(remoteOutputStream);
212         }
213
214         //
215         // PACKAGE-PRIVATE METHODS
216         //
217
218         /**
219          * Handles the given message, notifying listeners. This message should only
220          * be called by {@link FcpConnectionHandler}.
221          *
222          * @param fcpMessage
223          *            The received message
224          */
225         void handleMessage(FcpMessage fcpMessage) {
226                 logger.fine("received message: " + fcpMessage.getName());
227                 String messageName = fcpMessage.getName();
228                 countMessage(messageName);
229                 if ("SimpleProgress".equals(messageName)) {
230                         fcpListenerManager.fireReceivedSimpleProgress(new SimpleProgress(fcpMessage));
231                 } else if ("ProtocolError".equals(messageName)) {
232                         fcpListenerManager.fireReceivedProtocolError(new ProtocolError(fcpMessage));
233                 } else if ("PersistentGet".equals(messageName)) {
234                         fcpListenerManager.fireReceivedPersistentGet(new PersistentGet(fcpMessage));
235                 } else if ("PersistentPut".equals(messageName)) {
236                         fcpListenerManager.fireReceivedPersistentPut(new PersistentPut(fcpMessage));
237                 } else if ("PersistentPutDir".equals(messageName)) {
238                         fcpListenerManager.fireReceivedPersistentPutDir(new PersistentPutDir(fcpMessage));
239                 } else if ("URIGenerated".equals(messageName)) {
240                         fcpListenerManager.fireReceivedURIGenerated(new URIGenerated(fcpMessage));
241                 } else if ("EndListPersistentRequests".equals(messageName)) {
242                         fcpListenerManager.fireReceivedEndListPersistentRequests(new EndListPersistentRequests(fcpMessage));
243                 } else if ("Peer".equals(messageName)) {
244                         fcpListenerManager.fireReceivedPeer(new Peer(fcpMessage));
245                 } else if ("PeerNote".equals(messageName)) {
246                         fcpListenerManager.fireReceivedPeerNote(new PeerNote(fcpMessage));
247                 } else if ("StartedCompression".equals(messageName)) {
248                         fcpListenerManager.fireReceivedStartedCompression(new StartedCompression(fcpMessage));
249                 } else if ("FinishedCompression".equals(messageName)) {
250                         fcpListenerManager.fireReceivedFinishedCompression(new FinishedCompression(fcpMessage));
251                 } else if ("GetFailed".equals(messageName)) {
252                         fcpListenerManager.fireReceivedGetFailed(new GetFailed(fcpMessage));
253                 } else if ("PutFetchable".equals(messageName)) {
254                         fcpListenerManager.fireReceivedPutFetchable(new PutFetchable(fcpMessage));
255                 } else if ("PutSuccessful".equals(messageName)) {
256                         fcpListenerManager.fireReceivedPutSuccessful(new PutSuccessful(fcpMessage));
257                 } else if ("PutFailed".equals(messageName)) {
258                         fcpListenerManager.fireReceivedPutFailed(new PutFailed(fcpMessage));
259                 } else if ("DataFound".equals(messageName)) {
260                         fcpListenerManager.fireReceivedDataFound(new DataFound(fcpMessage));
261                 } else if ("SubscribedUSKUpdate".equals(messageName)) {
262                         fcpListenerManager.fireReceivedSubscribedUSKUpdate(new SubscribedUSKUpdate(fcpMessage));
263                 } else if ("IdentifierCollision".equals(messageName)) {
264                         fcpListenerManager.fireReceivedIdentifierCollision(new IdentifierCollision(fcpMessage));
265                 } else if ("AllData".equals(messageName)) {
266                         LimitedInputStream payloadInputStream = getInputStream(FcpUtils.safeParseLong(fcpMessage.getField("DataLength")));
267                         fcpListenerManager.fireReceivedAllData(new AllData(fcpMessage, payloadInputStream));
268                         try {
269                                 payloadInputStream.consume();
270                         } catch (IOException ioe1) {
271                                 /* well, ignore. when the connection handler fails, all fails. */
272                         }
273                 } else if ("EndListPeerNotes".equals(messageName)) {
274                         fcpListenerManager.fireReceivedEndListPeerNotes(new EndListPeerNotes(fcpMessage));
275                 } else if ("EndListPeers".equals(messageName)) {
276                         fcpListenerManager.fireReceivedEndListPeers(new EndListPeers(fcpMessage));
277                 } else if ("SSKKeypair".equals(messageName)) {
278                         fcpListenerManager.fireReceivedSSKKeypair(new SSKKeypair(fcpMessage));
279                 } else if ("PeerRemoved".equals(messageName)) {
280                         fcpListenerManager.fireReceivedPeerRemoved(new PeerRemoved(fcpMessage));
281                 } else if ("PersistentRequestModified".equals(messageName)) {
282                         fcpListenerManager.fireReceivedPersistentRequestModified(new PersistentRequestModified(fcpMessage));
283                 } else if ("PersistentRequestRemoved".equals(messageName)) {
284                         fcpListenerManager.fireReceivedPersistentRequestRemoved(new PersistentRequestRemoved(fcpMessage));
285                 } else if ("UnknownPeerNoteType".equals(messageName)) {
286                         fcpListenerManager.fireReceivedUnknownPeerNoteType(new UnknownPeerNoteType(fcpMessage));
287                 } else if ("UnknownNodeIdentifier".equals(messageName)) {
288                         fcpListenerManager.fireReceivedUnknownNodeIdentifier(new UnknownNodeIdentifier(fcpMessage));
289                 } else if ("FCPPluginReply".equals(messageName)) {
290                         LimitedInputStream payloadInputStream = getInputStream(FcpUtils.safeParseLong(fcpMessage.getField("DataLength")));
291                         fcpListenerManager.fireReceivedFCPPluginReply(new FCPPluginReply(fcpMessage, payloadInputStream));
292                         try {
293                                 payloadInputStream.consume();
294                         } catch (IOException ioe1) {
295                                 /* ignore. */
296                         }
297                 } else if ("PluginInfo".equals(messageName)) {
298                         fcpListenerManager.fireReceivedPluginInfo(new PluginInfo(fcpMessage));
299                 } else if ("NodeData".equals(messageName)) {
300                         fcpListenerManager.fireReceivedNodeData(new NodeData(fcpMessage));
301                 } else if ("TestDDAReply".equals(messageName)) {
302                         fcpListenerManager.fireReceivedTestDDAReply(new TestDDAReply(fcpMessage));
303                 } else if ("TestDDAComplete".equals(messageName)) {
304                         fcpListenerManager.fireReceivedTestDDAComplete(new TestDDAComplete(fcpMessage));
305                 } else if ("ConfigData".equals(messageName)) {
306                         fcpListenerManager.fireReceivedConfigData(new ConfigData(fcpMessage));
307                 } else if ("NodeHello".equals(messageName)) {
308                         fcpListenerManager.fireReceivedNodeHello(new NodeHello(fcpMessage));
309                 } else if ("CloseConnectionDuplicateClientName".equals(messageName)) {
310                         fcpListenerManager.fireReceivedCloseConnectionDuplicateClientName(new CloseConnectionDuplicateClientName(fcpMessage));
311                 } else if ("SentFeed".equals(messageName)) {
312                         fcpListenerManager.fireSentFeed(new SentFeed(fcpMessage));
313                 } else if ("ReceivedBookmarkFeed".equals(messageName)) {
314                         fcpListenerManager.fireReceivedBookmarkFeed(new ReceivedBookmarkFeed(fcpMessage));
315                 } else {
316                         fcpListenerManager.fireMessageReceived(fcpMessage);
317                 }
318         }
319
320         /**
321          * Handles a disconnect from the node.
322          *
323          * @param throwable
324          *            The exception that caused the disconnect, or <code>null</code>
325          *            if there was no exception
326          */
327         synchronized void handleDisconnect(Throwable throwable) {
328                 FcpUtils.close(remoteInputStream);
329                 FcpUtils.close(remoteOutputStream);
330                 FcpUtils.close(remoteSocket);
331                 if (connectionHandler != null) {
332                         connectionHandler.stop();
333                         connectionHandler = null;
334                         fcpListenerManager.fireConnectionClosed(throwable);
335                 }
336         }
337
338         //
339         // PRIVATE METHODS
340         //
341
342         /**
343          * Incremets the counter in {@link #incomingMessageStatistics} by
344          * <cod>1</code> for the given message name.
345          *
346          * @param name
347          *            The name of the message to count
348          */
349         private void countMessage(String name) {
350                 int oldValue = 0;
351                 if (incomingMessageStatistics.containsKey(name)) {
352                         oldValue = incomingMessageStatistics.get(name);
353                 }
354                 incomingMessageStatistics.put(name, oldValue + 1);
355                 logger.finest("count for " + name + ": " + (oldValue + 1));
356         }
357
358         /**
359          * Returns a limited input stream from the node’s input stream.
360          *
361          * @param dataLength
362          *            The length of the stream
363          * @return The limited input stream
364          */
365         private synchronized LimitedInputStream getInputStream(long dataLength) {
366                 if (dataLength <= 0) {
367                         return new LimitedInputStream(null, 0);
368                 }
369                 return new LimitedInputStream(remoteInputStream, dataLength);
370         }
371
372         /**
373          * A wrapper around an {@link InputStream} that only supplies a limit number
374          * of bytes from the underlying input stream.
375          *
376          * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
377          */
378         private static class LimitedInputStream extends FilterInputStream {
379
380                 /** The remaining number of bytes that can be read. */
381                 private long remaining;
382
383                 /**
384                  * Creates a new LimitedInputStream that supplies at most
385                  * <code>length</code> bytes from the given input stream.
386                  *
387                  * @param inputStream
388                  *            The input stream
389                  * @param length
390                  *            The number of bytes to read
391                  */
392                 public LimitedInputStream(InputStream inputStream, long length) {
393                         super(inputStream);
394                         remaining = length;
395                 }
396
397                 /**
398                  * @see java.io.FilterInputStream#available()
399                  */
400                 @Override
401                 public synchronized int available() throws IOException {
402                         if (remaining == 0) {
403                                 return 0;
404                         }
405                         return (int) Math.min(super.available(), Math.min(Integer.MAX_VALUE, remaining));
406                 }
407
408                 /**
409                  * @see java.io.FilterInputStream#read()
410                  */
411                 @Override
412                 public synchronized int read() throws IOException {
413                         int read = -1;
414                         if (remaining > 0) {
415                                 read = super.read();
416                                 remaining--;
417                         }
418                         return read;
419                 }
420
421                 /**
422                  * @see java.io.FilterInputStream#read(byte[], int, int)
423                  */
424                 @Override
425                 public synchronized int read(byte[] b, int off, int len) throws IOException {
426                         if (remaining == 0) {
427                                 return -1;
428                         }
429                         int toCopy = (int) Math.min(len, Math.min(remaining, Integer.MAX_VALUE));
430                         int read = super.read(b, off, toCopy);
431                         remaining -= read;
432                         return read;
433                 }
434
435                 /**
436                  * @see java.io.FilterInputStream#skip(long)
437                  */
438                 @Override
439                 public synchronized long skip(long n) throws IOException {
440                         if ((n < 0) || (remaining == 0)) {
441                                 return 0;
442                         }
443                         long skipped = super.skip(Math.min(n, remaining));
444                         remaining -= skipped;
445                         return skipped;
446                 }
447
448                 /**
449                  * {@inheritDoc} This method does nothing, as {@link #mark(int)} and
450                  * {@link #reset()} are not supported.
451                  *
452                  * @see java.io.FilterInputStream#mark(int)
453                  */
454                 @Override
455                 public void mark(int readlimit) {
456                         /* do nothing. */
457                 }
458
459                 /**
460                  * {@inheritDoc}
461                  *
462                  * @see java.io.FilterInputStream#markSupported()
463                  * @return <code>false</code>
464                  */
465                 @Override
466                 public boolean markSupported() {
467                         return false;
468                 }
469
470                 /**
471                  * {@inheritDoc} This method does nothing, as {@link #mark(int)} and
472                  * {@link #reset()} are not supported.
473                  *
474                  * @see java.io.FilterInputStream#reset()
475                  */
476                 @Override
477                 public void reset() throws IOException {
478                         /* do nothing. */
479                 }
480
481                 /**
482                  * Consumes the input stream, i.e. read all bytes until the limit is
483                  * reached.
484                  *
485                  * @throws IOException
486                  *             if an I/O error occurs
487                  */
488                 public synchronized void consume() throws IOException {
489                         while (remaining > 0) {
490                                 skip(remaining);
491                         }
492                 }
493
494         }
495
496 }