/**
* Package-private construtor.
+ *
+ * @param result
+ * The result of the operation
*/
- HighLevelCallback() {
+ HighLevelCallback(R result) {
+ this.result = result;
}
/**
}
/**
- * Sets the complete result of the operation. Calling this method will
- * result in all listeners being notified.
- *
- * @see #fireGotResult()
- * @param result
- * The result of the operation
- */
- void setResult(R result) {
- setResult(result, true);
- }
-
- /**
- * Sets the result of the operation. Depending on the <code>notify</code>
- * parameter the listeners are notified. You have to call this method with
- * <code>notify = true</code> after your result is completed, otherwise
- * clients will block endlessly!
- *
- * @param result
- * The result of the operation
- * @param notify
- * <code>true</code> to finalize the result and notify all
- * listeners, <code>false</code> if something in the result
- * might still change
- */
- void setResult(R result, boolean notify) {
- synchronized (syncObject) {
- this.result = result;
- if (notify) {
- resultComplete = true;
- syncObject.notifyAll();
- }
- }
- if (notify) {
- fireGotResult();
- }
- }
-
- /**
* Returns the result even if it is not yet complete.
*
* @return The result of the operation
}
/**
- * Marks the result given in with
- * {@link #setResult(HighLevelResult, boolean)} as complete and notify the
- * listeners. If the result was already complete, nothing will be done.
+ * Marks the result as complete and notify the listeners. If the result was
+ * already complete, nothing will be done.
*/
void setDone() {
synchronized (syncObject) {
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;
* @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;
+ }
+ /* now check all callbacks. */
+ synchronized (syncObject) {
+ if (connectCallback != null) {
+ connectCallback.getIntermediaryResult().setFailed(true);
+ connectCallback.setDone();
+ connectCallback = null;
+ }
+ }
+ HighLevelCallback<KeyGenerationResult> keyGenerationCallback = keyGenerationCallbacks.remove(identifier);
+ if (keyGenerationCallback != null) {
+ keyGenerationCallback.getIntermediaryResult().setFailed(true);
+ keyGenerationCallback.setDone();
+ }
}
/**
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();
}
/**
package net.pterodactylus.fcp.highlevel;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import net.pterodactylus.fcp.Peer;
+
/**
* The result of a {@link HighLevelClient#getPeers()} operation.
*
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
* @version $Id$
*/
-public class PeerListResult extends HighLevelResult {
+public class PeerListResult extends HighLevelResult implements Iterable<Peer> {
+
+ /** The list of peers. */
+ private final List<Peer> peers = new ArrayList<Peer>();
+
+ /**
+ * Adds a peer to the list.
+ *
+ * @param peer
+ * The peer to add
+ */
+ public void addPeer(Peer peer) {
+ peers.add(peer);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Iterator<Peer> iterator() {
+ return peers.iterator();
+ }
+
+ /**
+ * Returns the peer at the given index.
+ *
+ * @param index
+ * The index of the peer
+ * @return The peer
+ * @see java.util.List#get(int)
+ */
+ public Peer get(int index) {
+ return peers.get(index);
+ }
+
+ /**
+ * Returns the size of the peer list.
+ *
+ * @return The size of the peer list
+ * @see java.util.List#size()
+ */
+ public int size() {
+ return peers.size();
+ }
}