add isNodeConnected()
[jSite2.git] / src / net / pterodactylus / jsite / core / Core.java
1 /*
2  * jSite2 - Core.java -
3  * Copyright © 2008 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.jsite.core;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /**
26  * The core of jSite.
27  * 
28  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
29  * @version $Id$
30  */
31 public class Core {
32
33         /** The core listeners. */
34         private final List<CoreListener> coreListeners = new ArrayList<CoreListener>();
35
36         /** The node list. */
37         private List<Node> configuredNodes = new ArrayList<Node>();
38
39         /** List of currently connected nodes. */
40         private List<Node> connectedNodes = new ArrayList<Node>();
41
42         /**
43          * Creates a new core.
44          */
45         public Core() {
46         }
47
48         //
49         // LISTENER MANAGEMENT
50         //
51
52         /**
53          * Adds the given listener to the list of registered listeners.
54          * 
55          * @param coreListener
56          *            The listener to add
57          */
58         public void addCoreListener(CoreListener coreListener) {
59                 coreListeners.add(coreListener);
60         }
61
62         /**
63          * Removes the given listener from the list of registered listeners.
64          * 
65          * @param coreListener
66          *            The listener to remove
67          */
68         public void removeCoreListener(CoreListener coreListener) {
69                 coreListeners.remove(coreListener);
70         }
71
72         /**
73          * Notifies all core listeners that the core has loaded and is ready to run.
74          */
75         private void fireCoreLoaded() {
76                 for (CoreListener coreListener: coreListeners) {
77                         coreListener.coreLoaded();
78                 }
79         }
80
81         //
82         // ACCESSORS
83         //
84
85         /**
86          * Returns the list of all configured nodes.
87          * 
88          * @return All configured nodes
89          */
90         public List<Node> getNodes() {
91                 return configuredNodes;
92         }
93
94         /**
95          * Returns whether the core is currently connected to the given node.
96          * 
97          * @param node
98          *            The node to check
99          * @return <code>true</code> if the core is currently connected to the
100          *         node, <code>false</code> otherwise
101          */
102         public boolean isNodeConnected(Node node) {
103                 return connectedNodes.contains(node);
104         }
105
106         //
107         // ACTIONS
108         //
109
110         /**
111          * Starts the core.
112          */
113         public void start() {
114                 fireCoreLoaded();
115         }
116
117         /**
118          * Connects to the given node.
119          * 
120          * @param node
121          *            The node to connect to
122          */
123         public void connectToNode(Node node) {
124         }
125
126 }