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.HighLevelProgress;
36 import net.pterodactylus.fcp.highlevel.HighLevelProgressListener;
37 import net.pterodactylus.fcp.highlevel.RequestListResult;
38 import net.pterodactylus.fcp.highlevel.RequestResult;
39 import net.pterodactylus.util.logging.Logging;
42 * The request manager keeps track of all the request on all connected nodes.
43 * The request manager is added to the {@link NodeManager} as a
44 * {@link NodeListener} so that it can fire request-removed events in case a
45 * node is disconnected.
47 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
50 public class RequestManager implements NodeListener, HighLevelProgressListener {
53 private static final Logger logger = Logging.getLogger(RequestManager.class.getName());
55 /** Request listeners. */
56 private List<RequestListener> requestListeners = Collections.synchronizedList(new ArrayList<RequestListener>());
58 /** The node manager. */
59 private NodeManager nodeManager;
61 /** Request lists for all nodes. */
62 @SuppressWarnings("unused")
63 private Map<Node, Set<Request>> nodeRequests = Collections.synchronizedMap(new HashMap<Node, Set<Request>>());
70 * Adds a request listener.
72 * @param requestListener
73 * The request listener to add
75 public void addRequestListener(RequestListener requestListener) {
76 requestListeners.add(requestListener);
80 * Removes a request listener.
82 * @param requestListener
83 * The request listener to remove
85 public void removeRequestListener(RequestListener requestListener) {
86 requestListeners.remove(requestListener);
90 * Notifies all listeners that a request was added.
93 * The node that added the request
95 * The request that was added
97 private void fireRequestAdded(Node node, Request request) {
98 for (RequestListener requestListener: requestListeners) {
99 requestListener.requestAdded(node, request);
104 * Notifies all listeners that a request progressed.
107 * The node that runs the request
111 * The total number of blocks
112 * @param requiredBlocks
113 * The number of required blocks
114 * @param successfulBlocks
115 * The number of successful blocks
116 * @param failedBlocks
117 * The number of failed blocks
118 * @param fatallyFailedBlocks
119 * The number of fatally failed blocks
120 * @param finalizedTotal
121 * <code>true</code> if the total number of blocks in final,
122 * <code>false</code> otherwise
124 private void fireRequestProgressed(Node node, Request request, int totalBlocks, int requiredBlocks, int successfulBlocks, int failedBlocks, int fatallyFailedBlocks, boolean finalizedTotal) {
125 for (RequestListener requestListener: requestListeners) {
126 requestListener.requestProgressed(node, request, totalBlocks, requiredBlocks, successfulBlocks, failedBlocks, fatallyFailedBlocks, finalizedTotal);
135 * Sets the node manager to use.
140 public void setNodeManager(NodeManager nodeManager) {
141 this.nodeManager = nodeManager;
153 * Requests a list of all running requests from a node. This method will
154 * block until the request has been sent!
157 * The node to get all requests for
158 * @throws IOException
159 * if an I/O error occurs while communicating with the node
161 private void getRequests(final Node node) throws IOException {
162 HighLevelClient highLevelClient = nodeManager.getHighLevelClient(node);
163 if (highLevelClient == null) {
164 logger.log(Level.WARNING, "no client for node: " + node);
167 HighLevelCallback<RequestListResult> requestListCallback = highLevelClient.getRequests();
168 requestListCallback.addHighLevelCallbackListener(new HighLevelCallbackListener<RequestListResult>() {
170 @SuppressWarnings("synthetic-access")
171 public void gotResult(HighLevelCallback<RequestListResult> highLevelCallback) {
172 RequestListResult requestListResult;
174 requestListResult = highLevelCallback.getResult();
175 } catch (InterruptedException e) {
176 logger.log(Level.SEVERE, "getResult() blocked and was interrupted");
179 for (RequestResult requestResult: requestListResult) {
180 Request request = new Request(requestResult.getIdentifier());
181 /* TODO - fill request */
182 fireRequestAdded(node, request);
189 // INTERFACE NodeListener
195 public void nodeAdded(Node node) {
196 HighLevelClient highLevelClient = nodeManager.getHighLevelClient(node);
197 if (highLevelClient == null) {
198 logger.warning("got nodeAdded but no high-level client: " + node);
201 highLevelClient.addHighLevelProgressListener(this);
207 public void nodeRemoved(Node node) {
208 HighLevelClient highLevelClient = nodeManager.getHighLevelClient(node);
209 if (highLevelClient == null) {
210 logger.warning("got nodeRemoved but no high-level client: " + node);
213 highLevelClient.removeHighLevelProgressListener(this);
219 public void nodeConnected(Node node) {
220 HighLevelClient highLevelClient = nodeManager.getHighLevelClient(node);
221 if (highLevelClient == null) {
222 logger.log(Level.WARNING, "got no high-level client for node " + node);
226 highLevelClient.setWatchGlobal(true);
228 } catch (IOException e) {
229 /* ignore exception, disconnects are handled elsewhere. */
236 public void nodeConnectionFailed(Node node, Throwable cause) {
237 /* we don't care about this. */
243 public void nodeDisconnected(Node node, Throwable throwable) {
244 HighLevelClient highLevelClient = nodeManager.getHighLevelClient(node);
245 if (highLevelClient == null) {
246 logger.warning("got nodeDisconnected from node without high-level client: " + node);
252 // INTERFACE HighLevelProgressListener
256 * @see net.pterodactylus.fcp.highlevel.HighLevelProgressListener#progressReceived(java.lang.String,
257 * net.pterodactylus.fcp.highlevel.HighLevelProgress)
259 public void progressReceived(String identifier, HighLevelProgress highLevelProgress) {
260 fireRequestProgressed(null, new Request(identifier), highLevelProgress.getTotalBlocks(), highLevelProgress.getRequiredBlocks(), highLevelProgress.getSuccessfulBlocks(), highLevelProgress.getFailedBlocks(), highLevelProgress.getFatallyFailedBlocks(), highLevelProgress.isTotalFinalized());