add FCPPluginReply
[jSite2.git] / src / net / pterodactylus / util / fcp / FcpConnection.java
index 81b5c47..9a13264 100644 (file)
@@ -525,6 +525,30 @@ public class FcpConnection {
        }
 
        /**
+        * Notifies all listeners that a “PluginInfo” message was received.
+        * 
+        * @param pluginInfo
+        *            The “PluginInfo” message
+        */
+       private void fireReceivedPluginInfo(PluginInfo pluginInfo) {
+               for (FcpListener fcpListener: fcpListeners) {
+                       fcpListener.receivedPluginInfo(this, pluginInfo);
+               }
+       }
+
+       /**
+        * Notifies all listeners that an “FCPPluginReply” message was received.
+        * 
+        * @param fcpPluginReply
+        *            The “FCPPluginReply” message
+        */
+       private void fireReceivedFCPPluginReply(FCPPluginReply fcpPluginReply) {
+               for (FcpListener fcpListener: fcpListeners) {
+                       fcpListener.receivedFCPPluginReply(this, fcpPluginReply);
+               }
+       }
+
+       /**
         * Notifies all listeners that a “ProtocolError” message was received.
         * 
         * @param protocolError
@@ -644,18 +668,11 @@ public class FcpConnection {
                } else if ("IdentifierCollision".equals(messageName)) {
                        fireReceivedIdentifierCollision(new IdentifierCollision(fcpMessage));
                } else if ("AllData".equals(messageName)) {
-                       long dataLength;
-                       try {
-                               dataLength = Long.valueOf(fcpMessage.getField("DataLength"));
-                       } catch (NumberFormatException nfe1) {
-                               dataLength = -1;
-                       }
-                       LimitedInputStream payloadInputStream = new LimitedInputStream(remoteInputStream, dataLength);
+                       LimitedInputStream payloadInputStream = getInputStream(FcpUtils.safeParseLong(fcpMessage.getField("DataLength")));
                        fireReceivedAllData(new AllData(fcpMessage, payloadInputStream));
                        try {
                                payloadInputStream.consume();
                        } catch (IOException ioe1) {
-                               /* FIXME - what now? */
                                /* well, ignore. when the connection handler fails, all fails. */
                        }
                } else if ("EndListPeerNotes".equals(messageName)) {
@@ -672,6 +689,16 @@ public class FcpConnection {
                        fireReceivedUnknownPeerNoteType(new UnknownPeerNoteType(fcpMessage));
                } else if ("UnknownNodeIdentifier".equals(messageName)) {
                        fireReceivedUnknownNodeIdentifier(new UnknownNodeIdentifier(fcpMessage));
+               } else if ("FCPPluginReply".equals(messageName)) {
+                       LimitedInputStream payloadInputStream = getInputStream(FcpUtils.safeParseLong(fcpMessage.getField("DataLength")));
+                       fireReceivedFCPPluginReply(new FCPPluginReply(fcpMessage, payloadInputStream));
+                       try {
+                               payloadInputStream.consume();
+                       } catch (IOException ioe1) {
+                               /* ignore. */
+                       }
+               } else if ("PluginInfo".equals(messageName)) {
+                       fireReceivedPluginInfo(new PluginInfo(fcpMessage));
                } else if ("NodeData".equals(messageName)) {
                        fireReceivedNodeData(new NodeData(fcpMessage));
                } else if ("TestDDAReply".equals(messageName)) {
@@ -718,4 +745,18 @@ public class FcpConnection {
                incomingMessageStatistics.put(name, oldValue + 1);
        }
 
+       /**
+        * Returns a limited input stream from the node’s input stream.
+        * 
+        * @param dataLength
+        *            The length of the stream
+        * @return The limited input stream
+        */
+       private LimitedInputStream getInputStream(long dataLength) {
+               if (dataLength <= 0) {
+                       return new LimitedInputStream(null, 0);
+               }
+               return new LimitedInputStream(remoteInputStream, dataLength);
+       }
+
 }