Add dedicated listener manager.
[jFCPlib.git] / src / net / pterodactylus / fcp / FcpListenerManager.java
1 /*
2  * jFCPlib - FcpListenerManager.java -
3  * Copyright © 2009 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.fcp;
21
22 import java.util.List;
23 import java.util.concurrent.CopyOnWriteArrayList;
24
25 /**
26  * Manages FCP listeners and event firing.
27  *
28  * @author David ‘Bombe’ Roden <bombe@pterodactylus.net>
29  */
30 public class FcpListenerManager {
31
32         /** The source FCP connection. */
33         private final FcpConnection fcpConnection;
34
35         /** The registered listeners. */
36         private final List<FcpListener> fcpListeners = new CopyOnWriteArrayList<FcpListener>();
37
38         /**
39          * Creates a new listener manager.
40          *
41          * @param fcpConnection
42          *            The source FCP connection
43          */
44         public FcpListenerManager(FcpConnection fcpConnection) {
45                 this.fcpConnection = fcpConnection;
46         }
47
48         /**
49          * Adds the given FCP listener to the list of registered listeners.
50          *
51          * @param fcpListener
52          *            The FCP listener to add
53          */
54         public void addListener(FcpListener fcpListener) {
55                 fcpListeners.add(fcpListener);
56         }
57
58         /**
59          * Removes the given FCP listener from the list of registered listeners.
60          *
61          * @param fcpListener
62          *            The FCP listener to remove
63          */
64         public void removeListener(FcpListener fcpListener) {
65                 fcpListeners.remove(fcpListener);
66         }
67
68         /**
69          * Notifies listeners that a “NodeHello” message was received.
70          *
71          * @see FcpListener#receivedNodeHello(FcpConnection, NodeHello)
72          * @param nodeHello
73          *            The “NodeHello” message
74          */
75         public void fireReceivedNodeHello(NodeHello nodeHello) {
76                 for (FcpListener fcpListener : fcpListeners) {
77                         fcpListener.receivedNodeHello(fcpConnection, nodeHello);
78                 }
79         }
80
81         /**
82          * Notifies listeners that a “CloseConnectionDuplicateClientName” message
83          * was received.
84          *
85          * @see FcpListener#receivedCloseConnectionDuplicateClientName(FcpConnection,
86          *      CloseConnectionDuplicateClientName)
87          * @param closeConnectionDuplicateClientName
88          *            The “CloseConnectionDuplicateClientName” message
89          */
90         public void fireReceivedCloseConnectionDuplicateClientName(CloseConnectionDuplicateClientName closeConnectionDuplicateClientName) {
91                 for (FcpListener fcpListener : fcpListeners) {
92                         fcpListener.receivedCloseConnectionDuplicateClientName(fcpConnection, closeConnectionDuplicateClientName);
93                 }
94         }
95
96         /**
97          * Notifies listeners that a “SSKKeypair” message was received.
98          *
99          * @see FcpListener#receivedSSKKeypair(FcpConnection, SSKKeypair)
100          * @param sskKeypair
101          *            The “SSKKeypair” message
102          */
103         public void fireReceivedSSKKeypair(SSKKeypair sskKeypair) {
104                 for (FcpListener fcpListener : fcpListeners) {
105                         fcpListener.receivedSSKKeypair(fcpConnection, sskKeypair);
106                 }
107         }
108
109         /**
110          * Notifies listeners that a “Peer” message was received.
111          *
112          * @see FcpListener#receivedPeer(FcpConnection, Peer)
113          * @param peer
114          *            The “Peer” message
115          */
116         public void fireReceivedPeer(Peer peer) {
117                 for (FcpListener fcpListener : fcpListeners) {
118                         fcpListener.receivedPeer(fcpConnection, peer);
119                 }
120         }
121
122         /**
123          * Notifies all listeners that an “EndListPeers” message was received.
124          *
125          * @see FcpListener#receivedEndListPeers(FcpConnection, EndListPeers)
126          * @param endListPeers
127          *            The “EndListPeers” message
128          */
129         public void fireReceivedEndListPeers(EndListPeers endListPeers) {
130                 for (FcpListener fcpListener : fcpListeners) {
131                         fcpListener.receivedEndListPeers(fcpConnection, endListPeers);
132                 }
133         }
134
135         /**
136          * Notifies all listeners that a “PeerNote” message was received.
137          *
138          * @see FcpListener#receivedPeerNote(FcpConnection, PeerNote)
139          * @param peerNote
140          */
141         public void fireReceivedPeerNote(PeerNote peerNote) {
142                 for (FcpListener fcpListener : fcpListeners) {
143                         fcpListener.receivedPeerNote(fcpConnection, peerNote);
144                 }
145         }
146
147         /**
148          * Notifies all listeners that an “EndListPeerNotes” message was received.
149          *
150          * @see FcpListener#receivedEndListPeerNotes(FcpConnection,
151          *      EndListPeerNotes)
152          * @param endListPeerNotes
153          *            The “EndListPeerNotes” message
154          */
155         public void fireReceivedEndListPeerNotes(EndListPeerNotes endListPeerNotes) {
156                 for (FcpListener fcpListener : fcpListeners) {
157                         fcpListener.receivedEndListPeerNotes(fcpConnection, endListPeerNotes);
158                 }
159         }
160
161         /**
162          * Notifies all listeners that a “PeerRemoved” message was received.
163          *
164          * @see FcpListener#receivedPeerRemoved(FcpConnection, PeerRemoved)
165          * @param peerRemoved
166          *            The “PeerRemoved” message
167          */
168         public void fireReceivedPeerRemoved(PeerRemoved peerRemoved) {
169                 for (FcpListener fcpListener : fcpListeners) {
170                         fcpListener.receivedPeerRemoved(fcpConnection, peerRemoved);
171                 }
172         }
173
174         /**
175          * Notifies all listeners that a “NodeData” message was received.
176          *
177          * @see FcpListener#receivedNodeData(FcpConnection, NodeData)
178          * @param nodeData
179          *            The “NodeData” message
180          */
181         public void fireReceivedNodeData(NodeData nodeData) {
182                 for (FcpListener fcpListener : fcpListeners) {
183                         fcpListener.receivedNodeData(fcpConnection, nodeData);
184                 }
185         }
186
187         /**
188          * Notifies all listeners that a “TestDDAReply” message was received.
189          *
190          * @see FcpListener#receivedTestDDAReply(FcpConnection, TestDDAReply)
191          * @param testDDAReply
192          *            The “TestDDAReply” message
193          */
194         public void fireReceivedTestDDAReply(TestDDAReply testDDAReply) {
195                 for (FcpListener fcpListener : fcpListeners) {
196                         fcpListener.receivedTestDDAReply(fcpConnection, testDDAReply);
197                 }
198         }
199
200         /**
201          * Notifies all listeners that a “TestDDAComplete” message was received.
202          *
203          * @see FcpListener#receivedTestDDAComplete(FcpConnection, TestDDAComplete)
204          * @param testDDAComplete
205          *            The “TestDDAComplete” message
206          */
207         public void fireReceivedTestDDAComplete(TestDDAComplete testDDAComplete) {
208                 for (FcpListener fcpListener : fcpListeners) {
209                         fcpListener.receivedTestDDAComplete(fcpConnection, testDDAComplete);
210                 }
211         }
212
213         /**
214          * Notifies all listeners that a “PersistentGet” message was received.
215          *
216          * @see FcpListener#receivedPersistentGet(FcpConnection, PersistentGet)
217          * @param persistentGet
218          *            The “PersistentGet” message
219          */
220         public void fireReceivedPersistentGet(PersistentGet persistentGet) {
221                 for (FcpListener fcpListener : fcpListeners) {
222                         fcpListener.receivedPersistentGet(fcpConnection, persistentGet);
223                 }
224         }
225
226         /**
227          * Notifies all listeners that a “PersistentPut” message was received.
228          *
229          * @see FcpListener#receivedPersistentPut(FcpConnection, PersistentPut)
230          * @param persistentPut
231          *            The “PersistentPut” message
232          */
233         public void fireReceivedPersistentPut(PersistentPut persistentPut) {
234                 for (FcpListener fcpListener : fcpListeners) {
235                         fcpListener.receivedPersistentPut(fcpConnection, persistentPut);
236                 }
237         }
238
239         /**
240          * Notifies all listeners that a “EndListPersistentRequests” message was
241          * received.
242          *
243          * @see FcpListener#receivedEndListPersistentRequests(FcpConnection,
244          *      EndListPersistentRequests)
245          * @param endListPersistentRequests
246          *            The “EndListPersistentRequests” message
247          */
248         public void fireReceivedEndListPersistentRequests(EndListPersistentRequests endListPersistentRequests) {
249                 for (FcpListener fcpListener : fcpListeners) {
250                         fcpListener.receivedEndListPersistentRequests(fcpConnection, endListPersistentRequests);
251                 }
252         }
253
254         /**
255          * Notifies all listeners that a “URIGenerated” message was received.
256          *
257          * @see FcpListener#receivedURIGenerated(FcpConnection, URIGenerated)
258          * @param uriGenerated
259          *            The “URIGenerated” message
260          */
261         public void fireReceivedURIGenerated(URIGenerated uriGenerated) {
262                 for (FcpListener fcpListener : fcpListeners) {
263                         fcpListener.receivedURIGenerated(fcpConnection, uriGenerated);
264                 }
265         }
266
267         /**
268          * Notifies all listeners that a “DataFound” message was received.
269          *
270          * @see FcpListener#receivedDataFound(FcpConnection, DataFound)
271          * @param dataFound
272          *            The “DataFound” message
273          */
274         public void fireReceivedDataFound(DataFound dataFound) {
275                 for (FcpListener fcpListener : fcpListeners) {
276                         fcpListener.receivedDataFound(fcpConnection, dataFound);
277                 }
278         }
279
280         /**
281          * Notifies all listeners that an “AllData” message was received.
282          *
283          * @see FcpListener#receivedAllData(FcpConnection, AllData)
284          * @param allData
285          *            The “AllData” message
286          */
287         public void fireReceivedAllData(AllData allData) {
288                 for (FcpListener fcpListener : fcpListeners) {
289                         fcpListener.receivedAllData(fcpConnection, allData);
290                 }
291         }
292
293         /**
294          * Notifies all listeners that a “SimpleProgress” message was received.
295          *
296          * @see FcpListener#receivedSimpleProgress(FcpConnection, SimpleProgress)
297          * @param simpleProgress
298          *            The “SimpleProgress” message
299          */
300         public void fireReceivedSimpleProgress(SimpleProgress simpleProgress) {
301                 for (FcpListener fcpListener : fcpListeners) {
302                         fcpListener.receivedSimpleProgress(fcpConnection, simpleProgress);
303                 }
304         }
305
306         /**
307          * Notifies all listeners that a “StartedCompression” message was received.
308          *
309          * @see FcpListener#receivedStartedCompression(FcpConnection,
310          *      StartedCompression)
311          * @param startedCompression
312          *            The “StartedCompression” message
313          */
314         public void fireReceivedStartedCompression(StartedCompression startedCompression) {
315                 for (FcpListener fcpListener : fcpListeners) {
316                         fcpListener.receivedStartedCompression(fcpConnection, startedCompression);
317                 }
318         }
319
320         /**
321          * Notifies all listeners that a “FinishedCompression” message was received.
322          *
323          * @see FcpListener#receviedFinishedCompression(FcpConnection,
324          *      FinishedCompression)
325          * @param finishedCompression
326          *            The “FinishedCompression” message
327          */
328         public void fireReceivedFinishedCompression(FinishedCompression finishedCompression) {
329                 for (FcpListener fcpListener : fcpListeners) {
330                         fcpListener.receviedFinishedCompression(fcpConnection, finishedCompression);
331                 }
332         }
333
334         /**
335          * Notifies all listeners that an “UnknownPeerNoteType” message was
336          * received.
337          *
338          * @see FcpListener#receivedUnknownPeerNoteType(FcpConnection,
339          *      UnknownPeerNoteType)
340          * @param unknownPeerNoteType
341          *            The “UnknownPeerNoteType” message
342          */
343         public void fireReceivedUnknownPeerNoteType(UnknownPeerNoteType unknownPeerNoteType) {
344                 for (FcpListener fcpListener : fcpListeners) {
345                         fcpListener.receivedUnknownPeerNoteType(fcpConnection, unknownPeerNoteType);
346                 }
347         }
348
349         /**
350          * Notifies all listeners that an “UnknownNodeIdentifier” message was
351          * received.
352          *
353          * @see FcpListener#receivedUnknownNodeIdentifier(FcpConnection,
354          *      UnknownNodeIdentifier)
355          * @param unknownNodeIdentifier
356          *            The “UnknownNodeIdentifier” message
357          */
358         public void fireReceivedUnknownNodeIdentifier(UnknownNodeIdentifier unknownNodeIdentifier) {
359                 for (FcpListener fcpListener : fcpListeners) {
360                         fcpListener.receivedUnknownNodeIdentifier(fcpConnection, unknownNodeIdentifier);
361                 }
362         }
363
364         /**
365          * Notifies all listeners that a “ConfigData” message was received.
366          *
367          * @see FcpListener#receivedConfigData(FcpConnection, ConfigData)
368          * @param configData
369          *            The “ConfigData” message
370          */
371         public void fireReceivedConfigData(ConfigData configData) {
372                 for (FcpListener fcpListener : fcpListeners) {
373                         fcpListener.receivedConfigData(fcpConnection, configData);
374                 }
375         }
376
377         /**
378          * Notifies all listeners that a “GetFailed” message was received.
379          *
380          * @see FcpListener#receivedGetFailed(FcpConnection, GetFailed)
381          * @param getFailed
382          *            The “GetFailed” message
383          */
384         public void fireReceivedGetFailed(GetFailed getFailed) {
385                 for (FcpListener fcpListener : fcpListeners) {
386                         fcpListener.receivedGetFailed(fcpConnection, getFailed);
387                 }
388         }
389
390         /**
391          * Notifies all listeners that a “PutFailed” message was received.
392          *
393          * @see FcpListener#receivedPutFailed(FcpConnection, PutFailed)
394          * @param putFailed
395          *            The “PutFailed” message
396          */
397         public void fireReceivedPutFailed(PutFailed putFailed) {
398                 for (FcpListener fcpListener : fcpListeners) {
399                         fcpListener.receivedPutFailed(fcpConnection, putFailed);
400                 }
401         }
402
403         /**
404          * Notifies all listeners that an “IdentifierCollision” message was
405          * received.
406          *
407          * @see FcpListener#receivedIdentifierCollision(FcpConnection,
408          *      IdentifierCollision)
409          * @param identifierCollision
410          *            The “IdentifierCollision” message
411          */
412         public void fireReceivedIdentifierCollision(IdentifierCollision identifierCollision) {
413                 for (FcpListener fcpListener : fcpListeners) {
414                         fcpListener.receivedIdentifierCollision(fcpConnection, identifierCollision);
415                 }
416         }
417
418         /**
419          * Notifies all listeners that an “PersistentPutDir” message was received.
420          *
421          * @see FcpListener#receivedPersistentPutDir(FcpConnection,
422          *      PersistentPutDir)
423          * @param persistentPutDir
424          *            The “PersistentPutDir” message
425          */
426         public void fireReceivedPersistentPutDir(PersistentPutDir persistentPutDir) {
427                 for (FcpListener fcpListener : fcpListeners) {
428                         fcpListener.receivedPersistentPutDir(fcpConnection, persistentPutDir);
429                 }
430         }
431
432         /**
433          * Notifies all listeners that a “PersistentRequestRemoved” message was
434          * received.
435          *
436          * @see FcpListener#receivedPersistentRequestRemoved(FcpConnection,
437          *      PersistentRequestRemoved)
438          * @param persistentRequestRemoved
439          *            The “PersistentRequestRemoved” message
440          */
441         public void fireReceivedPersistentRequestRemoved(PersistentRequestRemoved persistentRequestRemoved) {
442                 for (FcpListener fcpListener : fcpListeners) {
443                         fcpListener.receivedPersistentRequestRemoved(fcpConnection, persistentRequestRemoved);
444                 }
445         }
446
447         /**
448          * Notifies all listeners that a “SubscribedUSKUpdate” message was received.
449          *
450          * @see FcpListener#receivedSubscribedUSKUpdate(FcpConnection,
451          *      SubscribedUSKUpdate)
452          * @param subscribedUSKUpdate
453          *            The “SubscribedUSKUpdate” message
454          */
455         public void fireReceivedSubscribedUSKUpdate(SubscribedUSKUpdate subscribedUSKUpdate) {
456                 for (FcpListener fcpListener : fcpListeners) {
457                         fcpListener.receivedSubscribedUSKUpdate(fcpConnection, subscribedUSKUpdate);
458                 }
459         }
460
461         /**
462          * Notifies all listeners that a “PluginInfo” message was received.
463          *
464          * @see FcpListener#receivedPluginInfo(FcpConnection, PluginInfo)
465          * @param pluginInfo
466          *            The “PluginInfo” message
467          */
468         public void fireReceivedPluginInfo(PluginInfo pluginInfo) {
469                 for (FcpListener fcpListener : fcpListeners) {
470                         fcpListener.receivedPluginInfo(fcpConnection, pluginInfo);
471                 }
472         }
473
474         /**
475          * Notifies all listeners that an “FCPPluginReply” message was received.
476          *
477          * @see FcpListener#receivedFCPPluginReply(FcpConnection, FCPPluginReply)
478          * @param fcpPluginReply
479          *            The “FCPPluginReply” message
480          */
481         public void fireReceivedFCPPluginReply(FCPPluginReply fcpPluginReply) {
482                 for (FcpListener fcpListener : fcpListeners) {
483                         fcpListener.receivedFCPPluginReply(fcpConnection, fcpPluginReply);
484                 }
485         }
486
487         /**
488          * Notifies all listeners that a “PersistentRequestModified” message was
489          * received.
490          *
491          * @see FcpListener#receivedPersistentRequestModified(FcpConnection,
492          *      PersistentRequestModified)
493          * @param persistentRequestModified
494          *            The “PersistentRequestModified” message
495          */
496         public void fireReceivedPersistentRequestModified(PersistentRequestModified persistentRequestModified) {
497                 for (FcpListener fcpListener : fcpListeners) {
498                         fcpListener.receivedPersistentRequestModified(fcpConnection, persistentRequestModified);
499                 }
500         }
501
502         /**
503          * Notifies all listeners that a “PutSuccessful” message was received.
504          *
505          * @see FcpListener#receivedPutSuccessful(FcpConnection, PutSuccessful)
506          * @param putSuccessful
507          *            The “PutSuccessful” message
508          */
509         public void fireReceivedPutSuccessful(PutSuccessful putSuccessful) {
510                 for (FcpListener fcpListener : fcpListeners) {
511                         fcpListener.receivedPutSuccessful(fcpConnection, putSuccessful);
512                 }
513         }
514
515         /**
516          * Notifies all listeners that a “PutFetchable” message was received.
517          *
518          * @see FcpListener#receivedPutFetchable(FcpConnection, PutFetchable)
519          * @param putFetchable
520          *            The “PutFetchable” message
521          */
522         public void fireReceivedPutFetchable(PutFetchable putFetchable) {
523                 for (FcpListener fcpListener : fcpListeners) {
524                         fcpListener.receivedPutFetchable(fcpConnection, putFetchable);
525                 }
526         }
527
528         /**
529          * Notifies all listeners that a “ProtocolError” message was received.
530          *
531          * @see FcpListener#receivedProtocolError(FcpConnection, ProtocolError)
532          * @param protocolError
533          *            The “ProtocolError” message
534          */
535         public void fireReceivedProtocolError(ProtocolError protocolError) {
536                 for (FcpListener fcpListener : fcpListeners) {
537                         fcpListener.receivedProtocolError(fcpConnection, protocolError);
538                 }
539         }
540
541         /**
542          * Notifies all registered listeners that a message has been received.
543          *
544          * @see FcpListener#receivedMessage(FcpConnection, FcpMessage)
545          * @param fcpMessage
546          *            The message that was received
547          */
548         public void fireMessageReceived(FcpMessage fcpMessage) {
549                 for (FcpListener fcpListener : fcpListeners) {
550                         fcpListener.receivedMessage(fcpConnection, fcpMessage);
551                 }
552         }
553
554         /**
555          * Notifies all listeners that the connection to the node was closed.
556          *
557          * @param throwable
558          *            The exception that caused the disconnect, or <code>null</code>
559          *            if there was no exception
560          * @see FcpListener#connectionClosed(FcpConnection, Throwable)
561          */
562         public void fireConnectionClosed(Throwable throwable) {
563                 for (FcpListener fcpListener : fcpListeners) {
564                         fcpListener.connectionClosed(fcpConnection, throwable);
565                 }
566         }
567
568 }