add high-level client listener
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sat, 3 May 2008 20:48:01 +0000 (20:48 +0000)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sat, 3 May 2008 20:48:01 +0000 (20:48 +0000)
git-svn-id: http://trooper/svn/projects/jFCPlib/branch/high-level-client@845 c3eda9e8-030b-0410-8277-bc7414b0a119

src/net/pterodactylus/fcp/highlevel/HighLevelClient.java
src/net/pterodactylus/fcp/highlevel/HighLevelClientListener.java [new file with mode: 0644]

index d5252ad..f1b3598 100644 (file)
@@ -27,8 +27,10 @@ import java.io.IOException;
 import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.logging.Logger;
@@ -109,6 +111,9 @@ public class HighLevelClient {
        /** The FCP connection to the node. */
        private FcpConnection fcpConnection;
 
+       /** Listeners for high-level client events. */
+       private List<HighLevelClientListener> highLevelClientListeners = Collections.synchronizedList(new ArrayList<HighLevelClientListener>());
+
        /** The listener for the connection. */
        private HighLevelClientFcpListener highLevelClientFcpListener = new HighLevelClientFcpListener();
 
@@ -193,6 +198,48 @@ public class HighLevelClient {
        }
 
        //
+       // EVENT MANAGEMENT
+       //
+
+       /**
+        * Adds the given high-level client listener to list of listeners.
+        *
+        * @param highLevelClientListener
+        *            The listener to add
+        */
+       public void addHighLevelClientListener(HighLevelClientListener highLevelClientListener) {
+               highLevelClientListeners.add(highLevelClientListener);
+       }
+
+       /**
+        * Removes the given high-level client listener from the list of listeners.
+        *
+        * @param highLevelClientListener
+        *            The listener to remove
+        */
+       public void removeHighLevelClientListener(HighLevelClientListener highLevelClientListener) {
+               highLevelClientListeners.remove(highLevelClientListener);
+       }
+
+       /**
+        * Notifies all listeners that a client has connected.
+        */
+       private void fireClientConnected() {
+               for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) {
+                       highLevelClientListener.clientConnected(this);
+               }
+       }
+
+       /**
+        * Notifies all listeners that a client has disconnected.
+        */
+       private void fireClientDisconnected() {
+               for (HighLevelClientListener highLevelClientListener: highLevelClientListeners) {
+                       highLevelClientListener.clientDisconnected(this);
+               }
+       }
+
+       //
        // ACCESSORS
        //
 
@@ -232,6 +279,7 @@ public class HighLevelClient {
         */
        public void disconnect() {
                fcpConnection.close();
+               fireClientDisconnected();
        }
 
        /**
@@ -701,6 +749,7 @@ public class HighLevelClient {
                                connectCallback.setDone();
                                connectCallback = null;
                        }
+                       fireClientConnected();
                }
 
                /**
diff --git a/src/net/pterodactylus/fcp/highlevel/HighLevelClientListener.java b/src/net/pterodactylus/fcp/highlevel/HighLevelClientListener.java
new file mode 100644 (file)
index 0000000..aef7fd4
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * jFCPlib-high-level-client - HighLevelClientListener.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.fcp.highlevel;
+
+import java.util.EventListener;
+
+/**
+ * Interface for objects that want to be notified on certain high-level client
+ * events.
+ *
+ * @author David ‘Bombe’ Roden &lt;bombe@freenetproject.org&gt;
+ * @version $Id$
+ */
+public interface HighLevelClientListener extends EventListener {
+
+       /**
+        * Notifies a listener that the given high-level client was connected to the
+        * node.
+        *
+        * @param highLevelClient
+        *            The client that was connected
+        */
+       public void clientConnected(HighLevelClient highLevelClient);
+
+       /**
+        * Notifies a listener that the given client was disconnected from the node.
+        *
+        * @param highLevelClient
+        *            The client that was disconnected
+        */
+       public void clientDisconnected(HighLevelClient highLevelClient);
+
+}