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.List;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
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;
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.
42 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
45 public class RequestManager implements NodeListener {
48 private static final Logger logger = Logging.getLogger(RequestManager.class.getName());
50 /** Request listeners. */
51 private List<RequestListener> requestListeners = Collections.synchronizedList(new ArrayList<RequestListener>());
53 /** The node manager. */
54 private NodeManager nodeManager;
61 * Adds a request listener.
63 * @param requestListener
64 * The request listener to add
66 public void addRequestListener(RequestListener requestListener) {
67 requestListeners.add(requestListener);
71 * Removes a request listener.
73 * @param requestListener
74 * The request listener to remove
76 public void removeRequestListener(RequestListener requestListener) {
77 requestListeners.remove(requestListener);
81 * Notifies all listeners that a request was added.
84 * The node that added the request
86 * The request that was added
88 private void fireRequestAdded(Node node, Request request) {
89 for (RequestListener requestListener: requestListeners) {
90 requestListener.requestAdded(node, request);
99 * Sets the node manager to use.
104 public void setNodeManager(NodeManager nodeManager) {
105 this.nodeManager = nodeManager;
113 * Requests a list of all running requests from a node. This method will
114 * block until the request has been sent!
117 * The node to get all requests for
118 * @throws IOException
119 * if an I/O error occurs while communicating with the node
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);
128 HighLevelCallback<RequestListResult> requestListCallback = highLevelClient.getRequests();
129 requestListCallback.addHighLevelCallbackListener(new HighLevelCallbackListener<RequestListResult>() {
131 @SuppressWarnings("synthetic-access")
132 public void gotResult(HighLevelCallback<RequestListResult> highLevelCallback) {
133 RequestListResult requestListResult;
135 requestListResult = highLevelCallback.getResult();
136 } catch (InterruptedException e) {
137 logger.log(Level.SEVERE, "getResult() blocked and was interrupted");
140 for (RequestResult requestResult: requestListResult) {
141 Request request = new Request(requestResult.getIdentifier());
142 /* TODO - fill request */
143 fireRequestAdded(node, request);
148 nodeManager.returnHighLevelClient(highLevelClient);
153 // INTERFACE NodeListener
159 public void nodeConnected(Node node) {
160 /* TODO - get all requests. */
166 public void nodeDisconnected(Node node, Throwable throwable) {
167 /* TODO - remove all requests. */