From: David ‘Bombe’ Roden Date: Tue, 15 Apr 2008 18:06:02 +0000 (+0000) Subject: create callback with fixed result X-Git-Tag: v0.1.1~157 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=60144e6607f54352fdd28869e4af8b567c4e69da;p=jFCPlib.git create callback with fixed result add ListPeers implementation git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@828 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java b/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java index 3e3e70f..27fc6bb 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java @@ -48,8 +48,12 @@ public class HighLevelCallback { /** * Package-private construtor. + * + * @param result + * The result of the operation */ - HighLevelCallback() { + HighLevelCallback(R result) { + this.result = result; } /** @@ -137,44 +141,6 @@ public class HighLevelCallback { } /** - * 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 notify - * parameter the listeners are notified. You have to call this method with - * notify = true after your result is completed, otherwise - * clients will block endlessly! - * - * @param result - * The result of the operation - * @param notify - * true to finalize the result and notify all - * listeners, false 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 @@ -186,9 +152,8 @@ public class HighLevelCallback { } /** - * 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) { diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java index 48399da..14be335 100644 --- a/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java +++ b/src/net/pterodactylus/fcp/highlevel/HighLevelClient.java @@ -185,7 +185,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; } @@ -206,7 +206,7 @@ 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; @@ -222,7 +222,7 @@ public class HighLevelClient { public HighLevelCallback getPeers() throws IOException { String identifier = generateIdentifier("listPeers"); ListPeers listPeers = new ListPeers(identifier, true, true); - HighLevelCallback peerListCallback = new HighLevelCallback(); + HighLevelCallback peerListCallback = new HighLevelCallback(new PeerListResult()); peerListCallbacks.put(identifier, peerListCallback); fcpConnection.sendMessage(listPeers); return peerListCallback; @@ -298,7 +298,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(); } /** @@ -352,12 +362,11 @@ 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; } /** @@ -371,11 +380,10 @@ public class HighLevelClient { } String identifier = peer.getIdentifier(); HighLevelCallback 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); } /** @@ -438,7 +446,28 @@ 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; + } + /* now check all callbacks. */ + synchronized (syncObject) { + if (connectCallback != null) { + connectCallback.getIntermediaryResult().setFailed(true); + connectCallback.setDone(); + connectCallback = null; + } + } + HighLevelCallback keyGenerationCallback = keyGenerationCallbacks.remove(identifier); + if (keyGenerationCallback != null) { + keyGenerationCallback.getIntermediaryResult().setFailed(true); + keyGenerationCallback.setDone(); + } } /** @@ -475,10 +504,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(); } /** diff --git a/src/net/pterodactylus/fcp/highlevel/PeerListResult.java b/src/net/pterodactylus/fcp/highlevel/PeerListResult.java index ca2b264..e69be56 100644 --- a/src/net/pterodactylus/fcp/highlevel/PeerListResult.java +++ b/src/net/pterodactylus/fcp/highlevel/PeerListResult.java @@ -19,12 +19,60 @@ 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 { + + /** The list of peers. */ + private final List peers = new ArrayList(); + + /** + * Adds a peer to the list. + * + * @param peer + * The peer to add + */ + public void addPeer(Peer peer) { + peers.add(peer); + } + + /** + * {@inheritDoc} + */ + public Iterator 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(); + } }