create callback with fixed result
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 15 Apr 2008 18:06:02 +0000 (18:06 +0000)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Tue, 15 Apr 2008 18:06:02 +0000 (18:06 +0000)
add ListPeers implementation

git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@828 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/fcp/highlevel/HighLevelCallback.java
src/net/pterodactylus/fcp/highlevel/HighLevelClient.java
src/net/pterodactylus/fcp/highlevel/PeerListResult.java

index 3e3e70f..27fc6bb 100644 (file)
@@ -48,8 +48,12 @@ public class HighLevelCallback<R extends HighLevelResult> {
 
        /**
         * Package-private construtor.
+        * 
+        * @param result
+        *            The result of the operation
         */
-       HighLevelCallback() {
+       HighLevelCallback(R result) {
+               this.result = result;
        }
 
        /**
@@ -137,44 +141,6 @@ public class HighLevelCallback<R extends HighLevelResult> {
        }
 
        /**
-        * 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
@@ -186,9 +152,8 @@ public class HighLevelCallback<R extends HighLevelResult> {
        }
 
        /**
-        * 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) {
index 48399da..14be335 100644 (file)
@@ -185,7 +185,7 @@ public class HighLevelClient {
                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;
        }
@@ -206,7 +206,7 @@ public class HighLevelClient {
        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;
@@ -222,7 +222,7 @@ public class HighLevelClient {
        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;
@@ -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<PeerListResult> 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<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);
                }
 
                /**
@@ -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<KeyGenerationResult> 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();
                }
 
                /**
index ca2b264..e69be56 100644 (file)
 
 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 &lt;bombe@freenetproject.org&gt;
  * @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();
+       }
 
 }