--- /dev/null
+/*
+ * jFCPlib - FcpListenerManager.java -
+ * Copyright © 2009 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;
+
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+/**
+ * Manages FCP listeners and event firing.
+ *
+ * @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
+ */
+public class FcpListenerManager {
+
+ /** The source FCP connection. */
+ private final FcpConnection fcpConnection;
+
+ /** The registered listeners. */
+ private final List<FcpListener> fcpListeners = new CopyOnWriteArrayList<FcpListener>();
+
+ /**
+ * Creates a new listener manager.
+ *
+ * @param fcpConnection
+ * The source FCP connection
+ */
+ public FcpListenerManager(FcpConnection fcpConnection) {
+ this.fcpConnection = fcpConnection;
+ }
+
+ /**
+ * Adds the given FCP listener to the list of registered listeners.
+ *
+ * @param fcpListener
+ * The FCP listener to add
+ */
+ public void addListener(FcpListener fcpListener) {
+ fcpListeners.add(fcpListener);
+ }
+
+ /**
+ * Removes the given FCP listener from the list of registered listeners.
+ *
+ * @param fcpListener
+ * The FCP listener to remove
+ */
+ public void removeListener(FcpListener fcpListener) {
+ fcpListeners.remove(fcpListener);
+ }
+
+ /**
+ * Notifies listeners that a “NodeHello” message was received.
+ *
+ * @see FcpListener#receivedNodeHello(FcpConnection, NodeHello)
+ * @param nodeHello
+ * The “NodeHello” message
+ */
+ public void fireReceivedNodeHello(NodeHello nodeHello) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedNodeHello(fcpConnection, nodeHello);
+ }
+ }
+
+ /**
+ * Notifies listeners that a “CloseConnectionDuplicateClientName” message
+ * was received.
+ *
+ * @see FcpListener#receivedCloseConnectionDuplicateClientName(FcpConnection,
+ * CloseConnectionDuplicateClientName)
+ * @param closeConnectionDuplicateClientName
+ * The “CloseConnectionDuplicateClientName” message
+ */
+ public void fireReceivedCloseConnectionDuplicateClientName(CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedCloseConnectionDuplicateClientName(fcpConnection, closeConnectionDuplicateClientName);
+ }
+ }
+
+ /**
+ * Notifies listeners that a “SSKKeypair” message was received.
+ *
+ * @see FcpListener#receivedSSKKeypair(FcpConnection, SSKKeypair)
+ * @param sskKeypair
+ * The “SSKKeypair” message
+ */
+ public void fireReceivedSSKKeypair(SSKKeypair sskKeypair) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedSSKKeypair(fcpConnection, sskKeypair);
+ }
+ }
+
+ /**
+ * Notifies listeners that a “Peer” message was received.
+ *
+ * @see FcpListener#receivedPeer(FcpConnection, Peer)
+ * @param peer
+ * The “Peer” message
+ */
+ public void fireReceivedPeer(Peer peer) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPeer(fcpConnection, peer);
+ }
+ }
+
+ /**
+ * Notifies all listeners that an “EndListPeers” message was received.
+ *
+ * @see FcpListener#receivedEndListPeers(FcpConnection, EndListPeers)
+ * @param endListPeers
+ * The “EndListPeers” message
+ */
+ public void fireReceivedEndListPeers(EndListPeers endListPeers) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedEndListPeers(fcpConnection, endListPeers);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “PeerNote” message was received.
+ *
+ * @see FcpListener#receivedPeerNote(FcpConnection, PeerNote)
+ * @param peerNote
+ */
+ public void fireReceivedPeerNote(PeerNote peerNote) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPeerNote(fcpConnection, peerNote);
+ }
+ }
+
+ /**
+ * Notifies all listeners that an “EndListPeerNotes” message was received.
+ *
+ * @see FcpListener#receivedEndListPeerNotes(FcpConnection,
+ * EndListPeerNotes)
+ * @param endListPeerNotes
+ * The “EndListPeerNotes” message
+ */
+ public void fireReceivedEndListPeerNotes(EndListPeerNotes endListPeerNotes) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedEndListPeerNotes(fcpConnection, endListPeerNotes);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “PeerRemoved” message was received.
+ *
+ * @see FcpListener#receivedPeerRemoved(FcpConnection, PeerRemoved)
+ * @param peerRemoved
+ * The “PeerRemoved” message
+ */
+ public void fireReceivedPeerRemoved(PeerRemoved peerRemoved) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPeerRemoved(fcpConnection, peerRemoved);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “NodeData” message was received.
+ *
+ * @see FcpListener#receivedNodeData(FcpConnection, NodeData)
+ * @param nodeData
+ * The “NodeData” message
+ */
+ public void fireReceivedNodeData(NodeData nodeData) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedNodeData(fcpConnection, nodeData);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “TestDDAReply” message was received.
+ *
+ * @see FcpListener#receivedTestDDAReply(FcpConnection, TestDDAReply)
+ * @param testDDAReply
+ * The “TestDDAReply” message
+ */
+ public void fireReceivedTestDDAReply(TestDDAReply testDDAReply) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedTestDDAReply(fcpConnection, testDDAReply);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “TestDDAComplete” message was received.
+ *
+ * @see FcpListener#receivedTestDDAComplete(FcpConnection, TestDDAComplete)
+ * @param testDDAComplete
+ * The “TestDDAComplete” message
+ */
+ public void fireReceivedTestDDAComplete(TestDDAComplete testDDAComplete) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedTestDDAComplete(fcpConnection, testDDAComplete);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “PersistentGet” message was received.
+ *
+ * @see FcpListener#receivedPersistentGet(FcpConnection, PersistentGet)
+ * @param persistentGet
+ * The “PersistentGet” message
+ */
+ public void fireReceivedPersistentGet(PersistentGet persistentGet) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPersistentGet(fcpConnection, persistentGet);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “PersistentPut” message was received.
+ *
+ * @see FcpListener#receivedPersistentPut(FcpConnection, PersistentPut)
+ * @param persistentPut
+ * The “PersistentPut” message
+ */
+ public void fireReceivedPersistentPut(PersistentPut persistentPut) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPersistentPut(fcpConnection, persistentPut);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “EndListPersistentRequests” message was
+ * received.
+ *
+ * @see FcpListener#receivedEndListPersistentRequests(FcpConnection,
+ * EndListPersistentRequests)
+ * @param endListPersistentRequests
+ * The “EndListPersistentRequests” message
+ */
+ public void fireReceivedEndListPersistentRequests(EndListPersistentRequests endListPersistentRequests) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedEndListPersistentRequests(fcpConnection, endListPersistentRequests);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “URIGenerated” message was received.
+ *
+ * @see FcpListener#receivedURIGenerated(FcpConnection, URIGenerated)
+ * @param uriGenerated
+ * The “URIGenerated” message
+ */
+ public void fireReceivedURIGenerated(URIGenerated uriGenerated) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedURIGenerated(fcpConnection, uriGenerated);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “DataFound” message was received.
+ *
+ * @see FcpListener#receivedDataFound(FcpConnection, DataFound)
+ * @param dataFound
+ * The “DataFound” message
+ */
+ public void fireReceivedDataFound(DataFound dataFound) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedDataFound(fcpConnection, dataFound);
+ }
+ }
+
+ /**
+ * Notifies all listeners that an “AllData” message was received.
+ *
+ * @see FcpListener#receivedAllData(FcpConnection, AllData)
+ * @param allData
+ * The “AllData” message
+ */
+ public void fireReceivedAllData(AllData allData) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedAllData(fcpConnection, allData);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “SimpleProgress” message was received.
+ *
+ * @see FcpListener#receivedSimpleProgress(FcpConnection, SimpleProgress)
+ * @param simpleProgress
+ * The “SimpleProgress” message
+ */
+ public void fireReceivedSimpleProgress(SimpleProgress simpleProgress) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedSimpleProgress(fcpConnection, simpleProgress);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “StartedCompression” message was received.
+ *
+ * @see FcpListener#receivedStartedCompression(FcpConnection,
+ * StartedCompression)
+ * @param startedCompression
+ * The “StartedCompression” message
+ */
+ public void fireReceivedStartedCompression(StartedCompression startedCompression) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedStartedCompression(fcpConnection, startedCompression);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “FinishedCompression” message was received.
+ *
+ * @see FcpListener#receviedFinishedCompression(FcpConnection,
+ * FinishedCompression)
+ * @param finishedCompression
+ * The “FinishedCompression” message
+ */
+ public void fireReceivedFinishedCompression(FinishedCompression finishedCompression) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receviedFinishedCompression(fcpConnection, finishedCompression);
+ }
+ }
+
+ /**
+ * Notifies all listeners that an “UnknownPeerNoteType” message was
+ * received.
+ *
+ * @see FcpListener#receivedUnknownPeerNoteType(FcpConnection,
+ * UnknownPeerNoteType)
+ * @param unknownPeerNoteType
+ * The “UnknownPeerNoteType” message
+ */
+ public void fireReceivedUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedUnknownPeerNoteType(fcpConnection, unknownPeerNoteType);
+ }
+ }
+
+ /**
+ * Notifies all listeners that an “UnknownNodeIdentifier” message was
+ * received.
+ *
+ * @see FcpListener#receivedUnknownNodeIdentifier(FcpConnection,
+ * UnknownNodeIdentifier)
+ * @param unknownNodeIdentifier
+ * The “UnknownNodeIdentifier” message
+ */
+ public void fireReceivedUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedUnknownNodeIdentifier(fcpConnection, unknownNodeIdentifier);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “ConfigData” message was received.
+ *
+ * @see FcpListener#receivedConfigData(FcpConnection, ConfigData)
+ * @param configData
+ * The “ConfigData” message
+ */
+ public void fireReceivedConfigData(ConfigData configData) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedConfigData(fcpConnection, configData);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “GetFailed” message was received.
+ *
+ * @see FcpListener#receivedGetFailed(FcpConnection, GetFailed)
+ * @param getFailed
+ * The “GetFailed” message
+ */
+ public void fireReceivedGetFailed(GetFailed getFailed) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedGetFailed(fcpConnection, getFailed);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “PutFailed” message was received.
+ *
+ * @see FcpListener#receivedPutFailed(FcpConnection, PutFailed)
+ * @param putFailed
+ * The “PutFailed” message
+ */
+ public void fireReceivedPutFailed(PutFailed putFailed) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPutFailed(fcpConnection, putFailed);
+ }
+ }
+
+ /**
+ * Notifies all listeners that an “IdentifierCollision” message was
+ * received.
+ *
+ * @see FcpListener#receivedIdentifierCollision(FcpConnection,
+ * IdentifierCollision)
+ * @param identifierCollision
+ * The “IdentifierCollision” message
+ */
+ public void fireReceivedIdentifierCollision(IdentifierCollision identifierCollision) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedIdentifierCollision(fcpConnection, identifierCollision);
+ }
+ }
+
+ /**
+ * Notifies all listeners that an “PersistentPutDir” message was received.
+ *
+ * @see FcpListener#receivedPersistentPutDir(FcpConnection,
+ * PersistentPutDir)
+ * @param persistentPutDir
+ * The “PersistentPutDir” message
+ */
+ public void fireReceivedPersistentPutDir(PersistentPutDir persistentPutDir) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPersistentPutDir(fcpConnection, persistentPutDir);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “PersistentRequestRemoved” message was
+ * received.
+ *
+ * @see FcpListener#receivedPersistentRequestRemoved(FcpConnection,
+ * PersistentRequestRemoved)
+ * @param persistentRequestRemoved
+ * The “PersistentRequestRemoved” message
+ */
+ public void fireReceivedPersistentRequestRemoved(PersistentRequestRemoved persistentRequestRemoved) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPersistentRequestRemoved(fcpConnection, persistentRequestRemoved);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “SubscribedUSKUpdate” message was received.
+ *
+ * @see FcpListener#receivedSubscribedUSKUpdate(FcpConnection,
+ * SubscribedUSKUpdate)
+ * @param subscribedUSKUpdate
+ * The “SubscribedUSKUpdate” message
+ */
+ public void fireReceivedSubscribedUSKUpdate(SubscribedUSKUpdate subscribedUSKUpdate) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedSubscribedUSKUpdate(fcpConnection, subscribedUSKUpdate);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “PluginInfo” message was received.
+ *
+ * @see FcpListener#receivedPluginInfo(FcpConnection, PluginInfo)
+ * @param pluginInfo
+ * The “PluginInfo” message
+ */
+ public void fireReceivedPluginInfo(PluginInfo pluginInfo) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPluginInfo(fcpConnection, pluginInfo);
+ }
+ }
+
+ /**
+ * Notifies all listeners that an “FCPPluginReply” message was received.
+ *
+ * @see FcpListener#receivedFCPPluginReply(FcpConnection, FCPPluginReply)
+ * @param fcpPluginReply
+ * The “FCPPluginReply” message
+ */
+ public void fireReceivedFCPPluginReply(FCPPluginReply fcpPluginReply) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedFCPPluginReply(fcpConnection, fcpPluginReply);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “PersistentRequestModified” message was
+ * received.
+ *
+ * @see FcpListener#receivedPersistentRequestModified(FcpConnection,
+ * PersistentRequestModified)
+ * @param persistentRequestModified
+ * The “PersistentRequestModified” message
+ */
+ public void fireReceivedPersistentRequestModified(PersistentRequestModified persistentRequestModified) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPersistentRequestModified(fcpConnection, persistentRequestModified);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “PutSuccessful” message was received.
+ *
+ * @see FcpListener#receivedPutSuccessful(FcpConnection, PutSuccessful)
+ * @param putSuccessful
+ * The “PutSuccessful” message
+ */
+ public void fireReceivedPutSuccessful(PutSuccessful putSuccessful) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPutSuccessful(fcpConnection, putSuccessful);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “PutFetchable” message was received.
+ *
+ * @see FcpListener#receivedPutFetchable(FcpConnection, PutFetchable)
+ * @param putFetchable
+ * The “PutFetchable” message
+ */
+ public void fireReceivedPutFetchable(PutFetchable putFetchable) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedPutFetchable(fcpConnection, putFetchable);
+ }
+ }
+
+ /**
+ * Notifies all listeners that a “ProtocolError” message was received.
+ *
+ * @see FcpListener#receivedProtocolError(FcpConnection, ProtocolError)
+ * @param protocolError
+ * The “ProtocolError” message
+ */
+ public void fireReceivedProtocolError(ProtocolError protocolError) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedProtocolError(fcpConnection, protocolError);
+ }
+ }
+
+ /**
+ * Notifies all registered listeners that a message has been received.
+ *
+ * @see FcpListener#receivedMessage(FcpConnection, FcpMessage)
+ * @param fcpMessage
+ * The message that was received
+ */
+ public void fireMessageReceived(FcpMessage fcpMessage) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.receivedMessage(fcpConnection, fcpMessage);
+ }
+ }
+
+ /**
+ * Notifies all listeners that the connection to the node was closed.
+ *
+ * @param throwable
+ * The exception that caused the disconnect, or <code>null</code>
+ * if there was no exception
+ * @see FcpListener#connectionClosed(FcpConnection, Throwable)
+ */
+ public void fireConnectionClosed(Throwable throwable) {
+ for (FcpListener fcpListener : fcpListeners) {
+ fcpListener.connectionClosed(fcpConnection, throwable);
+ }
+ }
+
+}