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;
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;
/** 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>.
}
/**
+ * 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
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) {
peerListCallback.setDone();
return;
}
+ HighLevelCallback<PeerResult> peerCallback = peerCallbacks.remove(identifier);
+ if (peerCallback != null) {
+ peerCallback.getIntermediaryResult().setFailed(true);
+ peerCallback.setDone();
+ return;
+ }
}
}
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();
+ }
}
/**
--- /dev/null
+/*
+ * 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;
+ }
+
+}