From: David ‘Bombe’ Roden Date: Tue, 15 Apr 2008 21:20:15 +0000 (+0000) Subject: add addPeer() methods X-Git-Tag: v0.1.1~152 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=6e21203105cf44107c51d80ad8d98816726023b5;p=jFCPlib.git add addPeer() methods git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@835 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java index 5e13487..4671245 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java @@ -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> peerListCallbacks = Collections.synchronizedMap(new HashMap>()); + /** Mapping from request identifier to peer callbacks. */ + private Map> peerCallbacks = Collections.synchronizedMap(new HashMap>()); + /** * Creates a new high-level client that connects to a node on * localhost. @@ -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 addPeer(String nodeRefFile) throws IOException { + String identifier = generateIdentifier("addPeer"); + AddPeer addPeer = new AddPeer(nodeRefFile); + HighLevelCallback peerCallback = new HighLevelCallback(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 addPeer(URL nodeRefURL) throws IOException { + String identifier = generateIdentifier("addPeer"); + AddPeer addPeer = new AddPeer(nodeRefURL); + HighLevelCallback peerCallback = new HighLevelCallback(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 addPeer(NodeRef nodeRef) throws IOException { + String identifier = generateIdentifier("addPeer"); + AddPeer addPeer = new AddPeer(nodeRef); + HighLevelCallback peerCallback = new HighLevelCallback(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> peerEntry: peerCallbacks.entrySet()) { + peerEntry.getValue().getIntermediaryResult().setFailed(true); + peerEntry.getValue().setDone(); + } + peerCallbacks.clear(); } else { HighLevelCallback keyGenerationCallback = keyGenerationCallbacks.remove(identifier); if (keyGenerationCallback != null) { @@ -301,6 +371,12 @@ public class HighLevelClient { peerListCallback.setDone(); return; } + HighLevelCallback 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 peerListCallback = peerListCallbacks.get(identifier); - if (peerListCallback == null) { + if (peerListCallback != null) { + peerListCallback.getIntermediaryResult().addPeer(peer); return; } - peerListCallback.getIntermediaryResult().addPeer(peer); + HighLevelCallback 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 index 0000000..47e78b5 --- /dev/null +++ b/src/net/pterodactylus/fcp/highlevel/PeerResult.java @@ -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 <bombe@freenetproject.org> + * @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; + } + +}