+/*
+ * jSite2 - RequestManager.java -
+ * Copyright © 2008 David Roden
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+package net.pterodactylus.jsite.core;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import net.pterodactylus.fcp.highlevel.HighLevelCallback;
+import net.pterodactylus.fcp.highlevel.HighLevelCallbackListener;
+import net.pterodactylus.fcp.highlevel.HighLevelClient;
+import net.pterodactylus.fcp.highlevel.RequestListResult;
+import net.pterodactylus.fcp.highlevel.RequestResult;
+import net.pterodactylus.util.logging.Logging;
+
+/**
+ * The request manager keeps track of all the request on all connected nodes.
+ * The request manager is added to the {@link NodeManager} as a
+ * {@link NodeListener} so that it can fire request-removed events in case a
+ * node is disconnected.
+ *
+ * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
+ * @version $Id$
+ */
+public class RequestManager implements NodeListener {
+
+ /** Logger. */
+ private static final Logger logger = Logging.getLogger(RequestManager.class.getName());
+
+ /** Request listeners. */
+ private List<RequestListener> requestListeners = Collections.synchronizedList(new ArrayList<RequestListener>());
+
+ /** The node manager. */
+ private NodeManager nodeManager;
+
+ //
+ // EVENT MANAGEMENT
+ //
+
+ /**
+ * Adds a request listener.
+ *
+ * @param requestListener
+ * The request listener to add
+ */
+ public void addRequestListener(RequestListener requestListener) {
+ requestListeners.add(requestListener);
+ }
+
+ /**
+ * Removes a request listener.
+ *
+ * @param requestListener
+ * The request listener to remove
+ */
+ public void removeRequestListener(RequestListener requestListener) {
+ requestListeners.remove(requestListener);
+ }
+
+ /**
+ * Notifies all listeners that a request was added.
+ *
+ * @param node
+ * The node that added the request
+ * @param request
+ * The request that was added
+ */
+ private void fireRequestAdded(Node node, Request request) {
+ for (RequestListener requestListener: requestListeners) {
+ requestListener.requestAdded(node, request);
+ }
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Sets the node manager to use.
+ *
+ * @param nodeManager
+ * The node manager
+ */
+ public void setNodeManager(NodeManager nodeManager) {
+ this.nodeManager = nodeManager;
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Requests a list of all running requests from a node. This method will
+ * block until the request has been sent!
+ *
+ * @param node
+ * The node to get all requests for
+ * @throws IOException
+ * if an I/O error occurs while communicating with the node
+ */
+ public void getRequests(final Node node) throws IOException {
+ HighLevelClient highLevelClient = nodeManager.borrowHighLevelClient(node);
+ if (highLevelClient == null) {
+ logger.log(Level.WARNING, "no client for node: " + node);
+ return;
+ }
+ try {
+ HighLevelCallback<RequestListResult> requestListCallback = highLevelClient.getRequests();
+ requestListCallback.addHighLevelCallbackListener(new HighLevelCallbackListener<RequestListResult>() {
+
+ @SuppressWarnings("synthetic-access")
+ public void gotResult(HighLevelCallback<RequestListResult> highLevelCallback) {
+ RequestListResult requestListResult;
+ try {
+ requestListResult = highLevelCallback.getResult();
+ } catch (InterruptedException e) {
+ logger.log(Level.SEVERE, "getResult() blocked and was interrupted");
+ return;
+ }
+ for (RequestResult requestResult: requestListResult) {
+ Request request = new Request(requestResult.getIdentifier());
+ /* TODO - fill request */
+ fireRequestAdded(node, request);
+ }
+ }
+ });
+ } finally {
+ nodeManager.returnHighLevelClient(highLevelClient);
+ }
+ }
+
+ //
+ // INTERFACE NodeListener
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ public void nodeConnected(Node node) {
+ /* TODO - get all requests. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void nodeDisconnected(Node node, Throwable throwable) {
+ /* TODO - remove all requests. */
+ }
+
+}