make connections work
[jSite2.git] / src / net / pterodactylus / jsite / core / CoreImpl.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.io.IOException;
23 import java.net.UnknownHostException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 /**
28  * The core of jSite.
29  * 
30  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
31  * @version $Id$
32  */
33 public class CoreImpl implements Core, NodeListener, RequestListener {
34
35         /** The core listeners. */
36         private final List<CoreListener> coreListeners = new ArrayList<CoreListener>();
37
38         /** The project manager. */
39         private ProjectManager projectManager;
40
41         /** The node manager. */
42         private NodeManager nodeManager;
43
44         /** The request manager. */
45         /* TODO - remove */
46         @SuppressWarnings("unused")
47         private RequestManager requestManager;
48
49         //
50         // LISTENER MANAGEMENT
51         //
52
53         /**
54          * {@inheritDoc}
55          */
56         public void addCoreListener(CoreListener coreListener) {
57                 coreListeners.add(coreListener);
58         }
59
60         /**
61          * {@inheritDoc}
62          */
63         public void removeCoreListener(CoreListener coreListener) {
64                 coreListeners.remove(coreListener);
65         }
66
67         /**
68          * Notifies all listeners that the projects were loaded successfully.
69          * 
70          * @param directory
71          *            The directory the projects were loaded from
72          */
73         private void fireLoadingProjectsDone(String directory) {
74                 for (CoreListener coreListener: coreListeners) {
75                         coreListener.loadingProjectsDone(directory);
76                 }
77         }
78
79         /**
80          * Notifies all core listeners that loading the projects from the given
81          * directory has failed.
82          * 
83          * @param directory
84          *            The directory the projects were tried to load from
85          * @param throwable
86          *            The exception that occured when loading projects
87          */
88         private void fireLoadingProjectsFailed(String directory, Throwable throwable) {
89                 for (CoreListener coreListener: coreListeners) {
90                         coreListener.loadingProjectsFailed(directory, throwable);
91                 }
92         }
93
94         /**
95          * Notifies all listeners that the projects were successfully saved.
96          * 
97          * @param directory
98          *            The directory the projects were saved to
99          */
100         private void fireSavingProjectsDone(String directory) {
101                 for (CoreListener coreListener: coreListeners) {
102                         coreListener.savingProjectsDone(directory);
103                 }
104         }
105
106         /**
107          * Notifies all listeners that the projects could not be saved.
108          * 
109          * @param directory
110          *            The directory the projects were to be saved to
111          * @param throwable
112          *            The exception that occured when saving the projects
113          */
114         private void fireSavingProjectsFailed(String directory, Throwable throwable) {
115                 for (CoreListener coreListener: coreListeners) {
116                         coreListener.savingProjectsFailed(directory, throwable);
117                 }
118         }
119
120         /**
121          * Notifies all listeners that the nodes were successfully loaded.
122          * 
123          * @param directory
124          *            The directory the nodes were loaded from
125          */
126         private void fireLoadingNodesDone(String directory) {
127                 for (CoreListener coreListener: coreListeners) {
128                         coreListener.loadingNodesDone(directory);
129                 }
130         }
131
132         /**
133          * Notifies all listeners that loading the nodes has failed.
134          * 
135          * @param directory
136          *            The directory the nodes were loaded from
137          * @param throwable
138          *            The exception that occured while loading the nodes
139          */
140         private void fireLoadingNodesFailed(String directory, Throwable throwable) {
141                 for (CoreListener coreListener: coreListeners) {
142                         coreListener.loadingNodesFailed(directory, throwable);
143                 }
144         }
145
146         /**
147          * Notifies all listeners that the nodes were saved successfully.
148          * 
149          * @param directory
150          *            The directory the nodes were saved to
151          */
152         private void fireSavingNodesDone(String directory) {
153                 for (CoreListener coreListener: coreListeners) {
154                         coreListener.savingNodesDone(directory);
155                 }
156         }
157
158         /**
159          * Notifies all listeners that saving the nodes has failed.
160          * 
161          * @param directory
162          *            The directory the nodes were saved to
163          * @param throwable
164          *            The exception that occured while saving the nodes
165          */
166         private void fireSavingNodesFailed(String directory, Throwable throwable) {
167                 for (CoreListener coreListener: coreListeners) {
168                         coreListener.savingProjectsFailed(directory, throwable);
169                 }
170         }
171
172         /**
173          * Notifies all core listeners that the core has loaded and is ready to run.
174          */
175         private void fireCoreLoaded() {
176                 for (CoreListener coreListener: coreListeners) {
177                         coreListener.coreLoaded();
178                 }
179         }
180
181         /**
182          * Notifies all listeners that the core was stopped.
183          */
184         private void fireCoreStopped() {
185                 for (CoreListener coreListener: coreListeners) {
186                         coreListener.coreStopped();
187                 }
188         }
189
190         /**
191          * Notifies all listeners that a node was added to the core.
192          * 
193          * @param node
194          *            The node that was added
195          */
196         private void fireNodeAdded(Node node) {
197                 for (CoreListener coreListener: coreListeners) {
198                         coreListener.nodeAdded(node);
199                 }
200         }
201
202         /**
203          * Notifies all listeners that a node was removed from the core.
204          * 
205          * @param node
206          *            The node that was removed
207          */
208         private void fireNodeRemoved(Node node) {
209                 for (CoreListener coreListener: coreListeners) {
210                         coreListener.nodeRemoved(node);
211                 }
212         }
213
214         /**
215          * Notifies all listeners that a connection to the given node is now being
216          * established.
217          * 
218          * @param node
219          *            The node that is being connected to
220          */
221         private void fireNodeConnecting(Node node) {
222                 for (CoreListener coreListener: coreListeners) {
223                         coreListener.nodeConnecting(node);
224                 }
225         }
226
227         /**
228          * Notifies all listeners that the given node is now connected.
229          * 
230          * @param node
231          *            The node that is now connected
232          */
233         private void fireNodeConnected(Node node) {
234                 for (CoreListener coreListener: coreListeners) {
235                         coreListener.nodeConnected(node);
236                 }
237         }
238
239         /**
240          * Notifies all listeners that a connection to a node has failed.
241          * 
242          * @param node
243          *            The node that could not be connected
244          * @param cause
245          *            The cause of the failure
246          */
247         private void fireNodeConnectionFailed(Node node, Throwable cause) {
248                 for (CoreListener coreListener: coreListeners) {
249                         coreListener.nodeConnectionFailed(node, cause);
250                 }
251         }
252
253         /**
254          * Notifies all listeners that the given node was disconnected.
255          * 
256          * @param node
257          *            The node that is now disconnected
258          * @param throwable
259          *            The exception that caused the disconnect, or <code>null</code>
260          *            if there was no exception
261          */
262         private void fireNodeDisconnected(Node node, Throwable throwable) {
263                 for (CoreListener coreListener: coreListeners) {
264                         coreListener.nodeDisconnected(node, throwable);
265                 }
266         }
267
268         /**
269          * Notifies all listeners that a request was added to a node.
270          * 
271          * @param node
272          *            The node the request was added to
273          * @param request
274          *            The request that was added
275          */
276         private void fireRequestAdded(Node node, Request request) {
277                 for (CoreListener coreListener: coreListeners) {
278                         coreListener.requestAdded(node, request);
279                 }
280         }
281
282         /**
283          * Notifies all listeners that a request progressed.
284          * 
285          * @param node
286          *            The node that runs the request
287          * @param request
288          *            The request
289          * @param totalBlocks
290          *            The total number of blocks
291          * @param requiredBlocks
292          *            The number of required blocks
293          * @param successfulBlocks
294          *            The number of successful blocks
295          * @param failedBlocks
296          *            The number of failed blocks
297          * @param fatallyFailedBlocks
298          *            The number of fatally failed blocks
299          * @param finalizedTotal
300          *            <code>true</code> if the total number of blocks in final,
301          *            <code>false</code> otherwise
302          */
303         private void fireRequestProgressed(Node node, Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
304                 for (CoreListener coreListener: coreListeners) {
305                         coreListener.requestProgressed(request, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal);
306                 }
307         }
308
309         //
310         // ACCESSORS
311         //
312
313         /**
314          * Returns the project manager.
315          * 
316          * @return The project manager
317          */
318         public ProjectManager getProjectManager() {
319                 return projectManager;
320         }
321
322         /**
323          * Sets the project manager to use.
324          * 
325          * @param projectManager
326          *            The project manager to use
327          */
328         public void setProjectManager(ProjectManager projectManager) {
329                 this.projectManager = projectManager;
330         }
331
332         /**
333          * Returns the node manager.
334          * 
335          * @return The node manager
336          */
337         public NodeManager getNodeManager() {
338                 return nodeManager;
339         }
340
341         /**
342          * Sets the node manager to use.
343          * 
344          * @param nodeManager
345          *            The node manager to use
346          */
347         public void setNodeManager(NodeManager nodeManager) {
348                 this.nodeManager = nodeManager;
349         }
350
351         /**
352          * Sets the request manager to use.
353          * 
354          * @param requestManager
355          *            The request manager to use
356          */
357         public void setRequestManager(RequestManager requestManager) {
358                 this.requestManager = requestManager;
359         }
360
361         /**
362          * {@inheritDoc}
363          */
364         public List<Node> getNodes() {
365                 return nodeManager.getNodes();
366         }
367
368         /**
369          * {@inheritDoc}
370          */
371         public boolean isNodeConnected(Node node) {
372                 return nodeManager.hasNode(node);
373         }
374
375         //
376         // ACTIONS
377         //
378
379         /**
380          * {@inheritDoc}
381          */
382         public void start() {
383                 try {
384                         projectManager.load();
385                         fireLoadingProjectsDone(projectManager.getDirectory());
386                 } catch (IOException ioe1) {
387                         fireLoadingProjectsFailed(projectManager.getDirectory(), ioe1);
388                 }
389                 try {
390                         nodeManager.load();
391                         fireLoadingNodesDone(nodeManager.getDirectory());
392                 } catch (IOException ioe1) {
393                         fireLoadingNodesFailed(nodeManager.getDirectory(), ioe1);
394                 }
395                 fireCoreLoaded();
396         }
397
398         /**
399          * {@inheritDoc}
400          */
401         public void stop() {
402                 try {
403                         projectManager.save();
404                         fireSavingProjectsDone(projectManager.getDirectory());
405                 } catch (IOException ioe1) {
406                         fireSavingProjectsFailed(projectManager.getDirectory(), ioe1);
407                 }
408                 try {
409                         nodeManager.save();
410                         fireSavingNodesDone(nodeManager.getDirectory());
411                 } catch (IOException ioe1) {
412                         fireSavingNodesFailed(nodeManager.getDirectory(), ioe1);
413                 }
414                 fireCoreStopped();
415         }
416
417         /**
418          * {@inheritDoc}
419          */
420         public boolean addNode(Node node) throws UnknownHostException {
421                 return nodeManager.addNode(node);
422         }
423
424         /**
425          * {@inheritDoc}
426          */
427         public void removeNode(Node node) {
428                 nodeManager.removeNode(node);
429         }
430
431         /**
432          * {@inheritDoc}
433          */
434         public void connectToNode(Node node) {
435                 fireNodeConnecting(node);
436                 nodeManager.connect(node);
437         }
438
439         /**
440          * {@inheritDoc}
441          */
442         public void disconnectFromNode(Node node) {
443                 nodeManager.disconnect(node);
444         }
445
446         //
447         // PRIVATE METHODS
448         //
449
450         /**
451          * Loads the configuration.
452          */
453         @SuppressWarnings("unused")
454         private void loadConfig() {
455                 /* TODO */
456         }
457
458         /**
459          * Saves the configuration.
460          */
461         @SuppressWarnings("unused")
462         private void saveConfig() {
463                 /* TODO */
464         }
465
466         //
467         // INTERFACE NodeListener
468         //
469
470         /**
471          * {@inheritDoc}
472          */
473         public void nodeAdded(Node node) {
474                 fireNodeAdded(node);
475         }
476
477         /**
478          * {@inheritDoc}
479          */
480         public void nodeRemoved(Node node) {
481                 fireNodeRemoved(node);
482         }
483
484         /**
485          * {@inheritDoc}
486          */
487         public void nodeConnected(Node node) {
488                 fireNodeConnected(node);
489         }
490
491         /**
492          * {@inheritDoc}
493          */
494         public void nodeConnectionFailed(Node node, Throwable cause) {
495                 fireNodeConnectionFailed(node, cause);
496         }
497
498         /**
499          * {@inheritDoc}
500          */
501         public void nodeDisconnected(Node node, Throwable throwable) {
502                 fireNodeDisconnected(node, throwable);
503         }
504
505         //
506         // INTERFACE RequestListener
507         //
508
509         /**
510          * {@inheritDoc}
511          */
512         public void requestAdded(Node node, Request request) {
513                 fireRequestAdded(node, request);
514         }
515
516         /**
517          * @see net.pterodactylus.jsite.core.RequestListener#requestProgressed(net.pterodactylus.jsite.core.Node,
518          *      net.pterodactylus.jsite.core.Request, int, int, int, int, int,
519          *      boolean)
520          */
521         public void requestProgressed(Node node, Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
522                 fireRequestProgressed(node, request, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal);
523         }
524 }