X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Ffcp%2Fhighlevel%2FFcpClient.java;h=3012e0011c1fc9b262921c3190b6ba2e5162fc5f;hb=e0d4c56493b97a16514ca865cad0ae42a2d8a5a2;hp=8a7a90d071c0b158fb31bc083e7b0659391f6c29;hpb=1a1ee191a57a6fc9e676b47ef068bd1f0403a1bc;p=jFCPlib.git diff --git a/src/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/net/pterodactylus/fcp/highlevel/FcpClient.java index 8a7a90d..3012e00 100644 --- a/src/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -1,6 +1,5 @@ /* - * jFCPlib - FcpClient.java - - * Copyright © 2009 David Roden + * jFCPlib - FcpClient.java - Copyright © 2009 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 @@ -47,11 +46,13 @@ import net.pterodactylus.fcp.FcpConnection; import net.pterodactylus.fcp.FcpListener; import net.pterodactylus.fcp.GenerateSSK; import net.pterodactylus.fcp.GetFailed; +import net.pterodactylus.fcp.GetNode; import net.pterodactylus.fcp.ListPeerNotes; import net.pterodactylus.fcp.ListPeers; import net.pterodactylus.fcp.ListPersistentRequests; import net.pterodactylus.fcp.ModifyPeer; import net.pterodactylus.fcp.ModifyPeerNote; +import net.pterodactylus.fcp.NodeData; import net.pterodactylus.fcp.NodeHello; import net.pterodactylus.fcp.NodeRef; import net.pterodactylus.fcp.Peer; @@ -79,6 +80,9 @@ public class FcpClient { /** Object used for synchronization. */ private final Object syncObject = new Object(); + /** Listener management. */ + private final FcpClientListenerManager fcpClientListenerManager = new FcpClientListenerManager(this); + /** The name of this client. */ private final String name; @@ -155,6 +159,62 @@ public class FcpClient { public FcpClient(String name, InetAddress host, int port) { this.name = name; fcpConnection = new FcpConnection(host, port); + fcpConnection.addFcpListener(new FcpAdapter() { + + /** + * {@inheritDoc} + */ + @Override + @SuppressWarnings("synthetic-access") + public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) { + connected = false; + fcpClientListenerManager.fireFcpClientDisconnected(); + } + }); + } + + // + // LISTENER MANAGEMENT + // + + /** + * Adds an FCP listener to the underlying connection. + * + * @param fcpListener + * The FCP listener to add + */ + public void addFcpListener(FcpListener fcpListener) { + fcpConnection.addFcpListener(fcpListener); + } + + /** + * Removes an FCP listener from the underlying connection. + * + * @param fcpListener + * The FCP listener to remove + */ + public void removeFcpListener(FcpListener fcpListener) { + fcpConnection.removeFcpListener(fcpListener); + } + + /** + * Adds an FCP client listener to the list of registered listeners. + * + * @param fcpClientListener + * The FCP client listener to add + */ + public void addFcpClientListener(FcpClientListener fcpClientListener) { + fcpClientListenerManager.addListener(fcpClientListener); + } + + /** + * Removes an FCP client listener from the list of registered listeners. + * + * @param fcpClientListener + * The FCP client listener to remove + */ + public void removeFcpClientListener(FcpClientListener fcpClientListener) { + fcpClientListenerManager.removeListener(fcpClientListener); } // @@ -886,6 +946,48 @@ public class FcpClient { } // + // NODE INFORMATION + // + + /** + * Returns information about the node. + * + * @param giveOpennetRef + * Whether to return the OpenNet reference + * @param withPrivate + * Whether to return private node data + * @param withVolatile + * Whether to return volatile node data + * @return Node information + * @throws FcpException + * if an FCP error occurs + * @throws IOException + * if an I/O error occurs + */ + public NodeData getNodeInformation(final Boolean giveOpennetRef, final Boolean withPrivate, final Boolean withVolatile) throws IOException, FcpException { + final ObjectWrapper nodeDataWrapper = new ObjectWrapper(); + new ExtendedFcpAdapter() { + + @Override + @SuppressWarnings("synthetic-access") + public void run() throws IOException { + GetNode getNodeMessage = new GetNode(giveOpennetRef, withPrivate, withVolatile); + fcpConnection.sendMessage(getNodeMessage); + } + + /** + * {@inheritDoc} + */ + @Override + public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) { + nodeDataWrapper.set(nodeData); + completionLatch.countDown(); + } + }.execute(); + return nodeDataWrapper.get(); + } + + // // PRIVATE METHODS //