X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fnet%2Fpterodactylus%2Ffcp%2Fhighlevel%2FFcpClient.java;h=88bc13f9ec29b58b04a96a0643966739ca695a45;hb=534a085e40b52ab8df7d113e159d108744b8d2a1;hp=bcf529b6dca4a14e26ffa5e4f57c0062ff11f581;hpb=6f8d718b2a9b274e8898ab964b355bf4b349a787;p=jFCPlib.git diff --git a/src/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/net/pterodactylus/fcp/highlevel/FcpClient.java index bcf529b..88bc13f 100644 --- a/src/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -21,15 +21,23 @@ package net.pterodactylus.fcp.highlevel; import java.io.IOException; import java.net.InetAddress; +import java.net.URL; import java.net.UnknownHostException; +import java.util.HashSet; +import java.util.Set; import java.util.concurrent.CountDownLatch; +import net.pterodactylus.fcp.AddPeer; import net.pterodactylus.fcp.ClientHello; import net.pterodactylus.fcp.CloseConnectionDuplicateClientName; +import net.pterodactylus.fcp.EndListPeers; import net.pterodactylus.fcp.FcpAdapter; import net.pterodactylus.fcp.FcpConnection; import net.pterodactylus.fcp.FcpListener; +import net.pterodactylus.fcp.ListPeers; import net.pterodactylus.fcp.NodeHello; +import net.pterodactylus.fcp.NodeRef; +import net.pterodactylus.fcp.Peer; import net.pterodactylus.fcp.ProtocolError; /** @@ -142,18 +150,21 @@ public class FcpClient { } }; fcpConnection.addFcpListener(fcpListener); - fcpConnection.connect(); - ClientHello clientHello = new ClientHello(name); - fcpConnection.sendMessage(clientHello); - while (true) { - try { - fcpListener.complete(); - break; - } catch (InterruptedException e) { - /* ignore, we’ll loop. */ + try { + fcpConnection.connect(); + ClientHello clientHello = new ClientHello(name); + fcpConnection.sendMessage(clientHello); + while (true) { + try { + fcpListener.complete(); + break; + } catch (InterruptedException e) { + /* ignore, we’ll loop. */ + } } + } finally { + fcpConnection.removeFcpListener(fcpListener); } - fcpConnection.removeFcpListener(fcpListener); if (fcpListener.getFcpException() != null) { throw fcpListener.getFcpException(); } @@ -169,6 +180,160 @@ public class FcpClient { } } + // + // PEER MANAGEMENT + // + + /** + * Returns all peers that the node has. + * + * @return A set containing the node’s peers + * @throws IOException + * if an I/O error occurs + * @throws FcpException + * if an FCP error occurs + */ + public Set getPeers() throws IOException, FcpException { + final Set peers = new HashSet(); + ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() { + + /** + * {@inheritDoc} + */ + @Override + public void receivedPeer(FcpConnection fcpConnection, Peer peer) { + peers.add(peer); + } + + /** + * {@inheritDoc} + */ + @Override + public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) { + completionLatch.countDown(); + } + }; + fcpConnection.addFcpListener(fcpListener); + fcpConnection.sendMessage(new ListPeers("list-peers")); + try { + while (true) { + try { + fcpListener.complete(); + break; + } catch (InterruptedException e) { + /* ignore, we’ll loop. */ + } + } + } finally { + fcpConnection.removeFcpListener(fcpListener); + } + if (fcpListener.getFcpException() != null) { + throw fcpListener.getFcpException(); + } + return peers; + } + + /** + * Adds the given peer to the node. + * + * @param peer + * The peer to add + * @throws IOException + * if an I/O error occurs + * @throws FcpException + * if an FCP error occurs + */ + public void addPeer(Peer peer) throws IOException, FcpException { + addPeer(peer.getNodeRef()); + } + + /** + * Adds the peer defined by the noderef to the node. + * + * @param nodeRef + * The noderef that defines the new peer + * @throws IOException + * if an I/O error occurs + * @throws FcpException + * if an FCP error occurs + */ + public void addPeer(NodeRef nodeRef) throws IOException, FcpException { + addPeer(new AddPeer(nodeRef)); + } + + /** + * Adds a peer, reading the noderef from the given URL. + * + * @param url + * The URL to read the noderef from + * @throws IOException + * if an I/O error occurs + * @throws FcpException + * if an FCP error occurs + */ + public void addPeer(URL url) throws IOException, FcpException { + addPeer(new AddPeer(url)); + } + + /** + * Adds a peer, reading the noderef of the peer from the given file. + * Note: the file to read the noderef from has to reside on + * the same machine as the node! + * + * @param file + * The name of the file containing the peer’s noderef + * @throws IOException + * if an I/O error occurs + * @throws FcpException + * if an FCP error occurs + */ + public void addPeer(String file) throws IOException, FcpException { + addPeer(new AddPeer(file)); + } + + /** + * Sends the given {@link AddPeer} message to the node. This method should + * not be called directly. Use one of {@link #addPeer(Peer)}, + * {@link #addPeer(NodeRef)}, {@link #addPeer(URL)}, or + * {@link #addPeer(String)} instead. + * + * @param addPeer + * The “AddPeer” message + * @throws IOException + * if an I/O error occurs + * @throws FcpException + * if an FCP error occurs + */ + private void addPeer(AddPeer addPeer) throws IOException, FcpException { + ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() { + + /** + * {@inheritDoc} + */ + @Override + public void receivedPeer(FcpConnection fcpConnection, Peer peer) { + completionLatch.countDown(); + } + }; + fcpConnection.addFcpListener(fcpListener); + try { + fcpConnection.sendMessage(addPeer); + while (true) { + try { + fcpListener.complete(); + break; + } catch (InterruptedException ie1) { + /* ignore, we’ll loop. */ + } + } + } finally { + fcpConnection.removeFcpListener(fcpListener); + } + if (fcpListener.getFcpException() != null) { + throw fcpListener.getFcpException(); + } + } + /** * Implementation of an {@link FcpListener} that can store an * {@link FcpException} and wait for the arrival of a certain command.