2 * jSite2 - RequestManager.java -
3 * Copyright © 2008 David Roden
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.
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.
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.
20 package net.pterodactylus.jsite.core;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
32 import net.pterodactylus.fcp.highlevel.HighLevelCallback;
33 import net.pterodactylus.fcp.highlevel.HighLevelCallbackListener;
34 import net.pterodactylus.fcp.highlevel.HighLevelClient;
35 import net.pterodactylus.fcp.highlevel.RequestListResult;
36 import net.pterodactylus.fcp.highlevel.RequestResult;
37 import net.pterodactylus.util.logging.Logging;
40 * The request manager keeps track of all the request on all connected nodes.
41 * The request manager is added to the {@link NodeManager} as a
42 * {@link NodeListener} so that it can fire request-removed events in case a
43 * node is disconnected.
45 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
48 public class RequestManager implements NodeListener {
51 private static final Logger logger = Logging.getLogger(RequestManager.class.getName());
53 /** Request listeners. */
54 private List<RequestListener> requestListeners = Collections.synchronizedList(new ArrayList<RequestListener>());
56 /** The node manager. */
57 private NodeManager nodeManager;
59 /** Request lists for all nodes. */
60 @SuppressWarnings("unused")
61 private Map<Node, Set<Request>> nodeRequests = Collections.synchronizedMap(new HashMap<Node, Set<Request>>());
68 * Adds a request listener.
70 * @param requestListener
71 * The request listener to add
73 public void addRequestListener(RequestListener requestListener) {
74 requestListeners.add(requestListener);
78 * Removes a request listener.
80 * @param requestListener
81 * The request listener to remove
83 public void removeRequestListener(RequestListener requestListener) {
84 requestListeners.remove(requestListener);
88 * Notifies all listeners that a request was added.
91 * The node that added the request
93 * The request that was added
95 private void fireRequestAdded(Node node, Request request) {
96 for (RequestListener requestListener: requestListeners) {
97 requestListener.requestAdded(node, request);
106 * Sets the node manager to use.
111 public void setNodeManager(NodeManager nodeManager) {
112 this.nodeManager = nodeManager;
124 * Requests a list of all running requests from a node. This method will
125 * block until the request has been sent!
128 * The node to get all requests for
129 * @throws IOException
130 * if an I/O error occurs while communicating with the node
132 private void getRequests(final Node node) throws IOException {
133 HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node);
134 if (highLevelClient == null) {
135 logger.log(Level.WARNING, "no client for node: " + node);
139 HighLevelCallback<RequestListResult> requestListCallback = highLevelClient.getRequests();
140 requestListCallback.addHighLevelCallbackListener(new HighLevelCallbackListener<RequestListResult>() {
142 @SuppressWarnings("synthetic-access")
143 public void gotResult(HighLevelCallback<RequestListResult> highLevelCallback) {
144 RequestListResult requestListResult;
146 requestListResult = highLevelCallback.getResult();
147 } catch (InterruptedException e) {
148 logger.log(Level.SEVERE, "getResult() blocked and was interrupted");
151 for (RequestResult requestResult: requestListResult) {
152 Request request = new Request(requestResult.getIdentifier());
153 /* TODO - fill request */
154 fireRequestAdded(node, request);
159 nodeManager.returnHighLevelClient(highLevelClient);
164 // INTERFACE NodeListener
170 public void nodeAdded(Node node) {
177 public void nodeRemoved(Node node) {
184 public void nodeConnected(Node node) {
185 HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node);
186 if (highLevelClient == null) {
187 logger.log(Level.WARNING, "got no high-level client for node " + node);
191 highLevelClient.setWatchGlobal(true);
192 } catch (IOException ioe1) {
193 /* ignore exception, disconnects are handled elsewhere. */
195 nodeManager.returnHighLevelClient(highLevelClient);
199 } catch (IOException e) {
200 /* ignore exception, disconnects are handled elsewhere. */
207 public void nodeDisconnected(Node node, Throwable throwable) {
208 /* TODO - remove all requests. */