X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fnet%2Fpterodactylus%2Ffcp%2Fhighlevel%2FHighLevelClient.java;h=5e1348729835f1b3eb26ad62fcb339f0b775ac05;hb=2ec849beb21de582bf5525e6a610d3896d5e7b2d;hp=741726f1dd6cee947a6b46d4eeaba765e6b1cd69;hpb=33e57ae1719a87ba663e1f640db264c8c698a8c4;p=jFCPlib.git diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java index 741726f..5e13487 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java @@ -25,6 +25,7 @@ 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.AllData; import net.pterodactylus.fcp.ClientHello; @@ -42,6 +43,7 @@ import net.pterodactylus.fcp.FinishedCompression; import net.pterodactylus.fcp.GenerateSSK; import net.pterodactylus.fcp.GetFailed; import net.pterodactylus.fcp.IdentifierCollision; +import net.pterodactylus.fcp.ListPeers; import net.pterodactylus.fcp.NodeData; import net.pterodactylus.fcp.NodeHello; import net.pterodactylus.fcp.Peer; @@ -100,6 +102,9 @@ public class HighLevelClient { /** Mapping from request identifiers to callbacks. */ private Map> keyGenerationCallbacks = Collections.synchronizedMap(new HashMap>()); + /** Mapping from request identifier to peer list callbacks. */ + private Map> peerListCallbacks = Collections.synchronizedMap(new HashMap>()); + /** * Creates a new high-level client that connects to a node on * localhost. @@ -181,7 +186,7 @@ public class HighLevelClient { fcpConnection = new FcpConnection(address, port); fcpConnection.addFcpListener(highLevelClientFcpListener); ClientHello clientHello = new ClientHello(clientName); - connectCallback = new HighLevelCallback(); + connectCallback = new HighLevelCallback(new ConnectResult()); fcpConnection.sendMessage(clientHello); return connectCallback; } @@ -202,13 +207,29 @@ public class HighLevelClient { public HighLevelCallback generateKey() throws IOException { String identifier = generateIdentifier("generateSSK"); GenerateSSK generateSSK = new GenerateSSK(identifier); - HighLevelCallback keyGenerationCallback = new HighLevelCallback(); + HighLevelCallback keyGenerationCallback = new HighLevelCallback(new KeyGenerationResult()); keyGenerationCallbacks.put(identifier, keyGenerationCallback); fcpConnection.sendMessage(generateSSK); return keyGenerationCallback; } /** + * Gets a list of all peers from the node. + * + * @return A callback with the peer list + * @throws IOException + * if an I/O error occurs with the node + */ + public HighLevelCallback getPeers() throws IOException { + String identifier = generateIdentifier("listPeers"); + ListPeers listPeers = new ListPeers(identifier, true, true); + HighLevelCallback peerListCallback = new HighLevelCallback(new PeerListResult()); + peerListCallbacks.put(identifier, peerListCallback); + fcpConnection.sendMessage(listPeers); + return peerListCallback; + } + + /** * Generates an identifier for the given function. * * @param function @@ -233,10 +254,69 @@ public class HighLevelClient { HighLevelClientFcpListener() { } + // + // PRIVATE METHODS + // + + /** + * Searches all callback collections for a callback with the given + * identifier and cancels it. + * + * @param identifier + * The identifier to search for, or null to + * cancel all pending requests + */ + @SuppressWarnings("synthetic-access") + private void cancelIdentifier(String identifier) { + synchronized (syncObject) { + if (connectCallback != null) { + connectCallback.getIntermediaryResult().setFailed(true); + connectCallback.setDone(); + connectCallback = null; + } + } + if (identifier == null) { + /* key generation callbacks */ + for (Entry> keyGenerationEntry: keyGenerationCallbacks.entrySet()) { + keyGenerationEntry.getValue().getIntermediaryResult().setFailed(true); + keyGenerationEntry.getValue().setDone(); + } + keyGenerationCallbacks.clear(); + /* peer list callbacks. */ + for (Entry> peerListEntry: peerListCallbacks.entrySet()) { + peerListEntry.getValue().getIntermediaryResult().setFailed(true); + peerListEntry.getValue().setDone(); + } + peerListCallbacks.clear(); + } else { + HighLevelCallback keyGenerationCallback = keyGenerationCallbacks.remove(identifier); + if (keyGenerationCallback != null) { + keyGenerationCallback.getIntermediaryResult().setFailed(true); + keyGenerationCallback.setDone(); + return; + } + HighLevelCallback peerListCallback = peerListCallbacks.remove(identifier); + if (peerListCallback != null) { + peerListCallback.getIntermediaryResult().setFailed(true); + peerListCallback.setDone(); + return; + } + } + } + + // + // INTERFACE FcpListener + // + /** * @see net.pterodactylus.fcp.FcpListener#connectionClosed(net.pterodactylus.fcp.FcpConnection) */ + @SuppressWarnings("synthetic-access") public void connectionClosed(FcpConnection fcpConnection) { + if (fcpConnection != HighLevelClient.this.fcpConnection) { + return; + } + cancelIdentifier(null); } /** @@ -278,7 +358,17 @@ public class HighLevelClient { * @see net.pterodactylus.fcp.FcpListener#receivedEndListPeers(net.pterodactylus.fcp.FcpConnection, * net.pterodactylus.fcp.EndListPeers) */ + @SuppressWarnings("synthetic-access") public void receivedEndListPeers(FcpConnection fcpConnection, EndListPeers endListPeers) { + if (fcpConnection != HighLevelClient.this.fcpConnection) { + return; + } + String identifier = endListPeers.getIdentifier(); + HighLevelCallback peerListCallback = peerListCallbacks.remove(identifier); + if (peerListCallback == null) { + return; + } + peerListCallback.setDone(); } /** @@ -332,19 +422,28 @@ public class HighLevelClient { if (fcpConnection != HighLevelClient.this.fcpConnection) { return; } - ConnectResult connectResult = new ConnectResult(); - synchronized (syncObject) { - connectCallback.setResult(connectResult); + connectCallback.getIntermediaryResult().setFailed(false); + connectCallback.setDone(); + connectCallback = null; } - connectCallback = null; } /** * @see net.pterodactylus.fcp.FcpListener#receivedPeer(net.pterodactylus.fcp.FcpConnection, * net.pterodactylus.fcp.Peer) */ + @SuppressWarnings("synthetic-access") public void receivedPeer(FcpConnection fcpConnection, Peer peer) { + if (fcpConnection != HighLevelClient.this.fcpConnection) { + return; + } + String identifier = peer.getIdentifier(); + HighLevelCallback peerListCallback = peerListCallbacks.get(identifier); + if (peerListCallback == null) { + return; + } + peerListCallback.getIntermediaryResult().addPeer(peer); } /** @@ -407,7 +506,16 @@ public class HighLevelClient { * @see net.pterodactylus.fcp.FcpListener#receivedProtocolError(net.pterodactylus.fcp.FcpConnection, * net.pterodactylus.fcp.ProtocolError) */ + @SuppressWarnings("synthetic-access") public void receivedProtocolError(FcpConnection fcpConnection, ProtocolError protocolError) { + if (fcpConnection != HighLevelClient.this.fcpConnection) { + return; + } + String identifier = protocolError.getIdentifier(); + if (identifier == null) { + return; + } + cancelIdentifier(identifier); } /** @@ -444,10 +552,10 @@ public class HighLevelClient { if (keyGenerationCallback == null) { return; } - KeyGenerationResult keyGenerationResult = new KeyGenerationResult(); + KeyGenerationResult keyGenerationResult = keyGenerationCallback.getIntermediaryResult(); keyGenerationResult.setInsertURI(sskKeypair.getInsertURI()); keyGenerationResult.setRequestURI(sskKeypair.getRequestURI()); - keyGenerationCallback.setResult(keyGenerationResult); + keyGenerationCallback.setDone(); } /**