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;
fcpConnection = new FcpConnection(address, port);
fcpConnection.addFcpListener(highLevelClientFcpListener);
ClientHello clientHello = new ClientHello(clientName);
- connectCallback = new HighLevelCallback<ConnectResult>();
+ connectCallback = new HighLevelCallback<ConnectResult>(new ConnectResult());
fcpConnection.sendMessage(clientHello);
return connectCallback;
}
public HighLevelCallback<KeyGenerationResult> generateKey() throws IOException {
String identifier = generateIdentifier("generateSSK");
GenerateSSK generateSSK = new GenerateSSK(identifier);
- HighLevelCallback<KeyGenerationResult> keyGenerationCallback = new HighLevelCallback<KeyGenerationResult>();
+ HighLevelCallback<KeyGenerationResult> keyGenerationCallback = new HighLevelCallback<KeyGenerationResult>(new KeyGenerationResult());
keyGenerationCallbacks.put(identifier, keyGenerationCallback);
fcpConnection.sendMessage(generateSSK);
return keyGenerationCallback;
public HighLevelCallback<PeerListResult> getPeers() throws IOException {
String identifier = generateIdentifier("listPeers");
ListPeers listPeers = new ListPeers(identifier, true, true);
- HighLevelCallback<PeerListResult> peerListCallback = new HighLevelCallback<PeerListResult>();
+ HighLevelCallback<PeerListResult> peerListCallback = new HighLevelCallback<PeerListResult>(new PeerListResult());
peerListCallbacks.put(identifier, peerListCallback);
fcpConnection.sendMessage(listPeers);
return peerListCallback;
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 <code>null</code> 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<String, HighLevelCallback<KeyGenerationResult>> keyGenerationEntry: keyGenerationCallbacks.entrySet()) {
+ keyGenerationEntry.getValue().getIntermediaryResult().setFailed(true);
+ keyGenerationEntry.getValue().setDone();
+ }
+ keyGenerationCallbacks.clear();
+ /* peer list callbacks. */
+ for (Entry<String, HighLevelCallback<PeerListResult>> peerListEntry: peerListCallbacks.entrySet()) {
+ peerListEntry.getValue().getIntermediaryResult().setFailed(true);
+ peerListEntry.getValue().setDone();
+ }
+ peerListCallbacks.clear();
+ } else {
+ HighLevelCallback<KeyGenerationResult> keyGenerationCallback = keyGenerationCallbacks.remove(identifier);
+ if (keyGenerationCallback != null) {
+ keyGenerationCallback.getIntermediaryResult().setFailed(true);
+ keyGenerationCallback.setDone();
+ return;
+ }
+ HighLevelCallback<PeerListResult> 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);
}
/**
* @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<PeerListResult> peerListCallback = peerListCallbacks.remove(identifier);
+ if (peerListCallback == null) {
+ return;
+ }
+ peerListCallback.setDone();
}
/**
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;
}
/**
}
String identifier = peer.getIdentifier();
HighLevelCallback<PeerListResult> peerListCallback = peerListCallbacks.get(identifier);
- PeerListResult peerListResult = peerListCallback.getIntermediaryResult();
- if (peerListResult == null) {
- peerListResult = new PeerListResult();
- peerListCallback.setResult(peerListResult, false);
+ if (peerListCallback == null) {
+ return;
}
+ peerListCallback.getIntermediaryResult().addPeer(peer);
}
/**
* @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);
}
/**
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();
}
/**