add constructor that connects to localhost at default port
[jSite2.git] / src / net / pterodactylus / util / fcp / FcpConnection.java
index d5f05ee..d585022 100644 (file)
@@ -70,6 +70,17 @@ public class FcpConnection {
        private Map<String, Integer> incomingMessageStatistics = Collections.synchronizedMap(new HashMap<String, Integer>());
 
        /**
+        * Creates a new FCP connection to the freenet node running on localhost,
+        * using the default port.
+        * 
+        * @throws UnknownHostException
+        *             if the hostname can not be resolved
+        */
+       public FcpConnection() throws UnknownHostException {
+               this(InetAddress.getLocalHost());
+       }
+
+       /**
         * Creates a new FCP connection to the Freenet node running on the given
         * host, listening on the default port.
         * 
@@ -292,6 +303,18 @@ public class FcpConnection {
        }
 
        /**
+        * Notifies all listeners that a “PersistentGet” message was received.
+        * 
+        * @param persistentGet
+        *            The “PersistentGet” message
+        */
+       private void fireReceivedPersistentGet(PersistentGet persistentGet) {
+               for (FcpListener fcpListener: fcpListeners) {
+                       fcpListener.receivedPersistentGet(this, persistentGet);
+               }
+       }
+
+       /**
         * Notifies all listeners that a “PersistentPut” message was received.
         * 
         * @see FcpListener#receivedPersistentPut(FcpConnection, PersistentPut)
@@ -390,6 +413,19 @@ public class FcpConnection {
        }
 
        /**
+        * Notifies all listeners that an “UnknownPeerNoteType” message was
+        * received.
+        * 
+        * @param unknownPeerNoteType
+        *            The “UnknownPeerNoteType” message
+        */
+       private void fireReceivedUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) {
+               for (FcpListener fcpListener: fcpListeners) {
+                       fcpListener.receivedUnknownPeerNoteType(this, unknownPeerNoteType);
+               }
+       }
+
+       /**
         * Notifies all listeners that a “ProtocolError” message was received.
         * 
         * @param protocolError
@@ -480,6 +516,8 @@ public class FcpConnection {
                        fireReceivedSimpleProgress(new SimpleProgress(fcpMessage));
                } else if ("ProtocolError".equals(messageName)) {
                        fireReceivedProtocolError(new ProtocolError(fcpMessage));
+               } else if ("PersistentGet".equals(messageName)) {
+                       fireReceivedPersistentGet(new PersistentGet(fcpMessage));
                } else if ("PersistentPut".equals(messageName)) {
                        fireReceivedPersistentPut(new PersistentPut(fcpMessage));
                } else if ("URIGenerated".equals(messageName)) {
@@ -519,6 +557,8 @@ public class FcpConnection {
                        fireReceivedSSKKeypair(new SSKKeypair(fcpMessage));
                } else if ("PeerRemoved".equals(messageName)) {
                        fireReceivedPeerRemoved(new PeerRemoved(fcpMessage));
+               } else if ("UnknownPeerNoteType".equals(messageName)) {
+                       fireReceivedUnknownPeerNoteType(new UnknownPeerNoteType(fcpMessage));
                } else if ("NodeData".equals(messageName)) {
                        fireReceivedNodeData(new NodeData(fcpMessage));
                } else if ("TestDDAReply".equals(messageName)) {
@@ -534,6 +574,16 @@ public class FcpConnection {
                }
        }
 
+       /**
+        * Handles a disconnect from the node.
+        */
+       synchronized void handleDisconnect() {
+               Closer.close(remoteInputStream);
+               Closer.close(remoteOutputStream);
+               Closer.close(remoteSocket);
+               connectionHandler = null;
+       }
+
        //
        // PRIVATE METHODS
        //