add request management
[jSite2.git] / src / net / pterodactylus / jsite / core / RequestManager.java
1 /*
2  * jSite2 - RequestManager.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.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
29 import net.pterodactylus.fcp.highlevel.HighLevelCallback;
30 import net.pterodactylus.fcp.highlevel.HighLevelCallbackListener;
31 import net.pterodactylus.fcp.highlevel.HighLevelClient;
32 import net.pterodactylus.fcp.highlevel.RequestListResult;
33 import net.pterodactylus.fcp.highlevel.RequestResult;
34 import net.pterodactylus.util.logging.Logging;
35
36 /**
37  * The request manager keeps track of all the request on all connected nodes.
38  * The request manager is added to the {@link NodeManager} as a
39  * {@link NodeListener} so that it can fire request-removed events in case a
40  * node is disconnected.
41  * 
42  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
43  * @version $Id$
44  */
45 public class RequestManager implements NodeListener {
46
47         /** Logger. */
48         private static final Logger logger = Logging.getLogger(RequestManager.class.getName());
49
50         /** Request listeners. */
51         private List<RequestListener> requestListeners = Collections.synchronizedList(new ArrayList<RequestListener>());
52
53         /** The node manager. */
54         private NodeManager nodeManager;
55
56         //
57         // EVENT MANAGEMENT
58         //
59
60         /**
61          * Adds a request listener.
62          * 
63          * @param requestListener
64          *            The request listener to add
65          */
66         public void addRequestListener(RequestListener requestListener) {
67                 requestListeners.add(requestListener);
68         }
69
70         /**
71          * Removes a request listener.
72          * 
73          * @param requestListener
74          *            The request listener to remove
75          */
76         public void removeRequestListener(RequestListener requestListener) {
77                 requestListeners.remove(requestListener);
78         }
79
80         /**
81          * Notifies all listeners that a request was added.
82          * 
83          * @param node
84          *            The node that added the request
85          * @param request
86          *            The request that was added
87          */
88         private void fireRequestAdded(Node node, Request request) {
89                 for (RequestListener requestListener: requestListeners) {
90                         requestListener.requestAdded(node, request);
91                 }
92         }
93
94         //
95         // ACCESSORS
96         //
97
98         /**
99          * Sets the node manager to use.
100          * 
101          * @param nodeManager
102          *            The node manager
103          */
104         public void setNodeManager(NodeManager nodeManager) {
105                 this.nodeManager = nodeManager;
106         }
107
108         //
109         // ACTIONS
110         //
111
112         /**
113          * Requests a list of all running requests from a node. This method will
114          * block until the request has been sent!
115          * 
116          * @param node
117          *            The node to get all requests for
118          * @throws IOException
119          *             if an I/O error occurs while communicating with the node
120          */
121         public void getRequests(final Node node) throws IOException {
122                 HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node);
123                 if (highLevelClient == null) {
124                         logger.log(Level.WARNING, "no client for node: " + node);
125                         return;
126                 }
127                 try {
128                         HighLevelCallback<RequestListResult> requestListCallback = highLevelClient.getRequests();
129                         requestListCallback.addHighLevelCallbackListener(new HighLevelCallbackListener<RequestListResult>() {
130
131                                 @SuppressWarnings("synthetic-access")
132                                 public void gotResult(HighLevelCallback<RequestListResult> highLevelCallback) {
133                                         RequestListResult requestListResult;
134                                         try {
135                                                 requestListResult = highLevelCallback.getResult();
136                                         } catch (InterruptedException e) {
137                                                 logger.log(Level.SEVERE, "getResult() blocked and was interrupted");
138                                                 return;
139                                         }
140                                         for (RequestResult requestResult: requestListResult) {
141                                                 Request request = new Request(requestResult.getIdentifier());
142                                                 /* TODO - fill request */
143                                                 fireRequestAdded(node, request);
144                                         }
145                                 }
146                         });
147                 } finally {
148                         nodeManager.returnHighLevelClient(highLevelClient);
149                 }
150         }
151
152         //
153         // INTERFACE NodeListener
154         //
155
156         /**
157          * {@inheritDoc}
158          */
159         public void nodeConnected(Node node) {
160                 /* TODO - get all requests. */
161         }
162
163         /**
164          * {@inheritDoc}
165          */
166         public void nodeDisconnected(Node node, Throwable throwable) {
167                 /* TODO - remove all requests. */
168         }
169
170 }