add addPeer() methods
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 15 Apr 2008 21:20:15 +0000 (21:20 +0000)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 15 Apr 2008 21:20:15 +0000 (21:20 +0000)
git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@835 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/fcp/highlevel/HighLevelClient.java
src/net/pterodactylus/fcp/highlevel/PeerResult.java [new file with mode: 0644]

index 5e13487..4671245 100644 (file)
@@ -21,12 +21,14 @@ package net.pterodactylus.fcp.highlevel;
 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.net.URL;
 import java.net.UnknownHostException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 
+import net.pterodactylus.fcp.AddPeer;
 import net.pterodactylus.fcp.AllData;
 import net.pterodactylus.fcp.ClientHello;
 import net.pterodactylus.fcp.CloseConnectionDuplicateClientName;
@@ -46,6 +48,7 @@ import net.pterodactylus.fcp.IdentifierCollision;
 import net.pterodactylus.fcp.ListPeers;
 import net.pterodactylus.fcp.NodeData;
 import net.pterodactylus.fcp.NodeHello;
+import net.pterodactylus.fcp.NodeRef;
 import net.pterodactylus.fcp.Peer;
 import net.pterodactylus.fcp.PeerNote;
 import net.pterodactylus.fcp.PeerRemoved;
@@ -105,6 +108,9 @@ public class HighLevelClient {
        /** Mapping from request identifier to peer list callbacks. */
        private Map<String, HighLevelCallback<PeerListResult>> peerListCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<PeerListResult>>());
 
+       /** Mapping from request identifier to peer callbacks. */
+       private Map<String, HighLevelCallback<PeerResult>> peerCallbacks = Collections.synchronizedMap(new HashMap<String, HighLevelCallback<PeerResult>>());
+
        /**
         * Creates a new high-level client that connects to a node on
         * <code>localhost</code>.
@@ -230,6 +236,64 @@ public class HighLevelClient {
        }
 
        /**
+        * Adds the peer whose noderef is stored in the given file.
+        * 
+        * @param nodeRefFile
+        *            The name of the file the peer’s noderef is stored in
+        * @return A peer callback
+        * @throws IOException
+        *             if an I/O error occurs communicating with the node
+        */
+       public HighLevelCallback<PeerResult> addPeer(String nodeRefFile) throws IOException {
+               String identifier = generateIdentifier("addPeer");
+               AddPeer addPeer = new AddPeer(nodeRefFile);
+               HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult());
+               peerCallbacks.put(identifier, peerCallback);
+               fcpConnection.sendMessage(addPeer);
+               return peerCallback;
+       }
+
+       /**
+        * Adds the peer whose noderef is stored in the given file.
+        * 
+        * @param nodeRefURL
+        *            The URL where the peer’s noderef is stored
+        * @return A peer callback
+        * @throws IOException
+        *             if an I/O error occurs communicating with the node
+        */
+       public HighLevelCallback<PeerResult> addPeer(URL nodeRefURL) throws IOException {
+               String identifier = generateIdentifier("addPeer");
+               AddPeer addPeer = new AddPeer(nodeRefURL);
+               HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult());
+               peerCallbacks.put(identifier, peerCallback);
+               fcpConnection.sendMessage(addPeer);
+               return peerCallback;
+       }
+
+       /**
+        * Adds the peer whose noderef is stored in the given file.
+        * 
+        * @param nodeRef
+        *            The peer’s noderef
+        * @return A peer callback
+        * @throws IOException
+        *             if an I/O error occurs communicating with the node
+        */
+       public HighLevelCallback<PeerResult> addPeer(NodeRef nodeRef) throws IOException {
+               String identifier = generateIdentifier("addPeer");
+               AddPeer addPeer = new AddPeer(nodeRef);
+               HighLevelCallback<PeerResult> peerCallback = new HighLevelCallback<PeerResult>(new PeerResult());
+               peerCallbacks.put(identifier, peerCallback);
+               fcpConnection.sendMessage(addPeer);
+               return peerCallback;
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       /**
         * Generates an identifier for the given function.
         * 
         * @param function
@@ -288,6 +352,12 @@ public class HighLevelClient {
                                        peerListEntry.getValue().setDone();
                                }
                                peerListCallbacks.clear();
+                               /* peer callbacks. */
+                               for (Entry<String, HighLevelCallback<PeerResult>> peerEntry: peerCallbacks.entrySet()) {
+                                       peerEntry.getValue().getIntermediaryResult().setFailed(true);
+                                       peerEntry.getValue().setDone();
+                               }
+                               peerCallbacks.clear();
                        } else {
                                HighLevelCallback<KeyGenerationResult> keyGenerationCallback = keyGenerationCallbacks.remove(identifier);
                                if (keyGenerationCallback != null) {
@@ -301,6 +371,12 @@ public class HighLevelClient {
                                        peerListCallback.setDone();
                                        return;
                                }
+                               HighLevelCallback<PeerResult> peerCallback = peerCallbacks.remove(identifier);
+                               if (peerCallback != null) {
+                                       peerCallback.getIntermediaryResult().setFailed(true);
+                                       peerCallback.setDone();
+                                       return;
+                               }
                        }
                }
 
@@ -439,11 +515,19 @@ public class HighLevelClient {
                                return;
                        }
                        String identifier = peer.getIdentifier();
+                       if (identifier == null) {
+                               return;
+                       }
                        HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.get(identifier);
-                       if (peerListCallback == null) {
+                       if (peerListCallback != null) {
+                               peerListCallback.getIntermediaryResult().addPeer(peer);
                                return;
                        }
-                       peerListCallback.getIntermediaryResult().addPeer(peer);
+                       HighLevelCallback<PeerResult> peerResult = peerCallbacks.remove(identifier);
+                       if (peerResult != null) {
+                               peerResult.getIntermediaryResult().setPeer(peer);
+                               peerResult.setDone();
+                       }
                }
 
                /**
diff --git a/src/net/pterodactylus/fcp/highlevel/PeerResult.java b/src/net/pterodactylus/fcp/highlevel/PeerResult.java
new file mode 100644 (file)
index 0000000..47e78b5
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * jFCPlib-high-level-client - PeerResult.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.fcp.highlevel;
+
+import net.pterodactylus.fcp.Peer;
+
+/**
+ * The peer result is the result of several operations:
+ * {@link HighLevelClient#addPeer(String)},
+ * {@link HighLevelClient#addPeer(java.net.URL)}, or
+ * {@link HighLevelClient#addPeer(net.pterodactylus.fcp.NodeRef)}.
+ * 
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @version $Id$
+ */
+public class PeerResult extends HighLevelResult {
+
+       /** The peer. */
+       private Peer peer;
+
+       /**
+        * Returns the peer.
+        * 
+        * @return The peer
+        */
+       public Peer getPeer() {
+               return peer;
+       }
+
+       /**
+        * Sets the peer.
+        * 
+        * @param peer
+        *            The peer
+        */
+       void setPeer(Peer peer) {
+               this.peer = peer;
+       }
+
+}