X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Ffcp%2Fhighlevel%2FHighLevelClient.java;h=d139ce2ee95f6a8b8e29c8ddf06f8f69dfa2ddd8;hb=17c6a031550816e0976f0c61ca5854092357e73c;hp=0ec6da6f44761cd2936e719bf4cb0887db601094;hpb=16353715c279d8b535b7aabb287764e591788c92;p=jFCPlib.git diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java index 0ec6da6..d139ce2 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java @@ -27,10 +27,13 @@ import java.io.IOException; import java.net.InetAddress; import java.net.URL; import java.net.UnknownHostException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.fcp.AddPeer; @@ -53,6 +56,7 @@ import net.pterodactylus.fcp.GenerateSSK; import net.pterodactylus.fcp.GetFailed; import net.pterodactylus.fcp.IdentifierCollision; import net.pterodactylus.fcp.ListPeers; +import net.pterodactylus.fcp.ListPersistentRequests; import net.pterodactylus.fcp.NodeData; import net.pterodactylus.fcp.NodeHello; import net.pterodactylus.fcp.NodeRef; @@ -109,6 +113,9 @@ public class HighLevelClient { /** The FCP connection to the node. */ private FcpConnection fcpConnection; + /** Listeners for high-level client events. */ + private List highLevelClientListeners = Collections.synchronizedList(new ArrayList()); + /** The listener for the connection. */ private HighLevelClientFcpListener highLevelClientFcpListener = new HighLevelClientFcpListener(); @@ -130,6 +137,9 @@ public class HighLevelClient { /** Mapping from request identifiers to download callbacks. */ private Map> downloadCallbacks = Collections.synchronizedMap(new HashMap>()); + /** The callback for {@link #getRequests()}. */ + private HighLevelCallback requestListCallback; + /** * Creates a new high-level client that connects to a node on * localhost. @@ -193,9 +203,66 @@ public class HighLevelClient { } // + // EVENT MANAGEMENT + // + + /** + * Adds the given high-level client listener to list of listeners. + * + * @param highLevelClientListener + * The listener to add + */ + public void addHighLevelClientListener(HighLevelClientListener highLevelClientListener) { + highLevelClientListeners.add(highLevelClientListener); + } + + /** + * Removes the given high-level client listener from the list of listeners. + * + * @param highLevelClientListener + * The listener to remove + */ + public void removeHighLevelClientListener(HighLevelClientListener highLevelClientListener) { + highLevelClientListeners.remove(highLevelClientListener); + } + + /** + * Notifies all listeners that a client has connected. + */ + private void fireClientConnected() { + for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) { + highLevelClientListener.clientConnected(this); + } + } + + /** + * Notifies all listeners that a client has disconnected. + * + * @param throwable + * The exception that caused the disconnect, or null + * if there was no exception + */ + private void fireClientDisconnected(Throwable throwable) { + for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) { + highLevelClientListener.clientDisconnected(this, throwable); + } + } + + // // ACCESSORS // + /** + * Returns the FCP connection that backs this high-level client. This method + * should be used with care as fiddling around with the FCP connection can + * easily break the high-level client if you don’t know what you’re doing! + * + * @return The FCP connection of this client + */ + public FcpConnection getFcpConnection() { + return fcpConnection; + } + // // ACTIONS // @@ -210,6 +277,7 @@ public class HighLevelClient { public HighLevelCallback connect() throws IOException { fcpConnection = new FcpConnection(address, port); fcpConnection.addFcpListener(highLevelClientFcpListener); + fcpConnection.connect(); ClientHello clientHello = new ClientHello(clientName); connectCallback = new HighLevelCallback(new ConnectResult()); fcpConnection.sendMessage(clientHello); @@ -220,6 +288,7 @@ public class HighLevelClient { * Disconnects the client from the node. */ public void disconnect() { + disconnect(null); } /** @@ -358,6 +427,26 @@ public class HighLevelClient { return downloadCallback; } + /** + * Requests a list of all running requests from the node. + * + * @return The request list result + * @throws IOException + * if an I/O errors communicating with the node + */ + public HighLevelCallback getRequests() throws IOException { + String identifier = generateIdentifier("list-persistent-requests"); + ListPersistentRequests listPersistentRequests = new ListPersistentRequests(); + synchronized (syncObject) { + if (requestListCallback != null) { + logger.log(Level.SEVERE, "getRequests() called with request still running!"); + } + requestListCallback = new HighLevelCallback(new RequestListResult(identifier)); + } + fcpConnection.sendMessage(listPersistentRequests); + return requestListCallback; + } + // // PRIVATE METHODS // @@ -374,6 +463,19 @@ public class HighLevelClient { } /** + * Disconnects the client from the node, handing the given Throwable to + * {@link #fireClientDisconnected(Throwable)}. + * + * @param throwable + * The exception that caused the disconnect, or null + * if there was no exception + */ + private void disconnect(Throwable throwable) { + fcpConnection.close(); + fireClientDisconnected(throwable); + } + + /** * FCP listener for {@link HighLevelClient}. * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> @@ -388,6 +490,7 @@ public class HighLevelClient { * Creates a new FCP listener for {@link HighLevelClient}. */ HighLevelClientFcpListener() { + /* do nothing. */ } // @@ -410,6 +513,11 @@ public class HighLevelClient { connectCallback.setDone(); connectCallback = null; } + if (requestListCallback != null) { + requestListCallback.getIntermediaryResult().setFailed(true); + requestListCallback.setDone(); + requestListCallback = null; + } } if (identifier == null) { /* key generation callbacks */ @@ -547,14 +655,16 @@ public class HighLevelClient { // /** - * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection) + * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection, + * Throwable) */ @SuppressWarnings("synthetic-access") - public void connectionClosed(FcpConnection fcpConnection) { + public void connectionClosed(FcpConnection fcpConnection, Throwable throwable) { if (fcpConnection != HighLevelClient.this.fcpConnection) { return; } cancelIdentifier(null); + disconnect(throwable); } /** @@ -562,6 +672,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.AllData) */ public void receivedAllData(FcpConnection fcpConnection, AllData allData) { + /* TODO */ } /** @@ -569,6 +680,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.CloseConnectionDuplicateClientName) */ public void receivedCloseConnectionDuplicateClientName(FcpConnection fcpConnection, CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) { + /* TODO */ } /** @@ -576,6 +688,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.ConfigData) */ public void receivedConfigData(FcpConnection fcpConnection, ConfigData configData) { + /* TODO */ } /** @@ -583,6 +696,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.DataFound) */ public void receivedDataFound(FcpConnection fcpConnection, DataFound dataFound) { + /* TODO */ } /** @@ -590,6 +704,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.EndListPeerNotes) */ public void receivedEndListPeerNotes(FcpConnection fcpConnection, EndListPeerNotes endListPeerNotes) { + /* TODO */ } /** @@ -613,7 +728,19 @@ public class HighLevelClient { * @see net.pterodactylus.fcp.FcpListener#receivedEndListPersistentRequests(net.pterodactylus.fcp.FcpConnection, * net.pterodactylus.fcp.EndListPersistentRequests) */ + @SuppressWarnings("synthetic-access") public void receivedEndListPersistentRequests(FcpConnection fcpConnection, EndListPersistentRequests endListPersistentRequests) { + if (fcpConnection != HighLevelClient.this.fcpConnection) { + return; + } + synchronized (syncObject) { + if (HighLevelClient.this.requestListCallback == null) { + logger.log(Level.WARNING, "got EndListPersistentRequests without running request!"); + return; + } + requestListCallback.setDone(); + requestListCallback = null; + } } /** @@ -621,6 +748,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.FCPPluginReply) */ public void receivedFCPPluginReply(FcpConnection fcpConnection, FCPPluginReply fcpPluginReply) { + /* TODO */ } /** @@ -648,6 +776,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.IdentifierCollision) */ public void receivedIdentifierCollision(FcpConnection fcpConnection, IdentifierCollision identifierCollision) { + /* TODO */ } /** @@ -655,6 +784,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.FcpMessage) */ public void receivedMessage(FcpConnection fcpConnection, FcpMessage fcpMessage) { + /* TODO */ } /** @@ -662,6 +792,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.NodeData) */ public void receivedNodeData(FcpConnection fcpConnection, NodeData nodeData) { + /* TODO */ } /** @@ -678,6 +809,7 @@ public class HighLevelClient { connectCallback.setDone(); connectCallback = null; } + fireClientConnected(); } /** @@ -712,6 +844,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.PeerNote) */ public void receivedPeerNote(FcpConnection fcpConnection, PeerNote peerNote) { + /* TODO */ } /** @@ -719,6 +852,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.PeerRemoved) */ public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) { + /* TODO */ } /** @@ -730,9 +864,16 @@ public class HighLevelClient { if (fcpConnection != HighLevelClient.this.fcpConnection) { return; } + synchronized (syncObject) { + if (requestListCallback != null) { + RequestListResult requestListResult = requestListCallback.getIntermediaryResult(); + requestListResult.addRequestResult(new GetRequestResult(persistentGet)); + return; + } + } String identifier = persistentGet.getIdentifier(); if (downloadCallbacks.containsKey(identifier)) { - /* ignore, because a download does not care about this. */ + /* TODO */ return; } } @@ -741,14 +882,36 @@ public class HighLevelClient { * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPut(net.pterodactylus.fcp.FcpConnection, * net.pterodactylus.fcp.PersistentPut) */ + @SuppressWarnings("synthetic-access") public void receivedPersistentPut(FcpConnection fcpConnection, PersistentPut persistentPut) { + if (fcpConnection != HighLevelClient.this.fcpConnection) { + return; + } + synchronized (syncObject) { + if (requestListCallback != null) { + RequestListResult requestListResult = requestListCallback.getIntermediaryResult(); + requestListResult.addRequestResult(new PutRequestResult(persistentPut)); + return; + } + } } /** * @see net.pterodactylus.fcp.FcpListener#receivedPersistentPutDir(net.pterodactylus.fcp.FcpConnection, * net.pterodactylus.fcp.PersistentPutDir) */ + @SuppressWarnings("synthetic-access") public void receivedPersistentPutDir(FcpConnection fcpConnection, PersistentPutDir persistentPutDir) { + if (fcpConnection != HighLevelClient.this.fcpConnection) { + return; + } + synchronized (syncObject) { + if (requestListCallback != null) { + RequestListResult requestListResult = requestListCallback.getIntermediaryResult(); + requestListResult.addRequestResult(new PutDirRequestResult(persistentPutDir)); + return; + } + } } /** @@ -756,6 +919,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.PersistentRequestModified) */ public void receivedPersistentRequestModified(FcpConnection fcpConnection, PersistentRequestModified persistentRequestModified) { + /* TODO */ } /** @@ -763,6 +927,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.PersistentRequestRemoved) */ public void receivedPersistentRequestRemoved(FcpConnection fcpConnection, PersistentRequestRemoved persistentRequestRemoved) { + /* TODO */ } /** @@ -770,6 +935,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.PluginInfo) */ public void receivedPluginInfo(FcpConnection fcpConnection, PluginInfo pluginInfo) { + /* TODO */ } /** @@ -793,6 +959,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.PutFailed) */ public void receivedPutFailed(FcpConnection fcpConnection, PutFailed putFailed) { + /* TODO */ } /** @@ -800,6 +967,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.PutFetchable) */ public void receivedPutFetchable(FcpConnection fcpConnection, PutFetchable putFetchable) { + /* TODO */ } /** @@ -807,6 +975,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.PutSuccessful) */ public void receivedPutSuccessful(FcpConnection fcpConnection, PutSuccessful putSuccessful) { + /* TODO */ } /** @@ -859,6 +1028,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.StartedCompression) */ public void receivedStartedCompression(FcpConnection fcpConnection, StartedCompression startedCompression) { + /* TODO */ } /** @@ -866,6 +1036,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.SubscribedUSKUpdate) */ public void receivedSubscribedUSKUpdate(FcpConnection fcpConnection, SubscribedUSKUpdate subscribedUSKUpdate) { + /* TODO */ } /** @@ -921,6 +1092,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.URIGenerated) */ public void receivedURIGenerated(FcpConnection fcpConnection, URIGenerated uriGenerated) { + /* TODO */ } /** @@ -928,6 +1100,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.UnknownNodeIdentifier) */ public void receivedUnknownNodeIdentifier(FcpConnection fcpConnection, UnknownNodeIdentifier unknownNodeIdentifier) { + /* TODO */ } /** @@ -935,6 +1108,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.UnknownPeerNoteType) */ public void receivedUnknownPeerNoteType(FcpConnection fcpConnection, UnknownPeerNoteType unknownPeerNoteType) { + /* TODO */ } /** @@ -942,6 +1116,7 @@ public class HighLevelClient { * net.pterodactylus.fcp.FinishedCompression) */ public void receviedFinishedCompression(FcpConnection fcpConnection, FinishedCompression finishedCompression) { + /* TODO */ } }