2 * jSite-next - RequestManager.java -
3 * Copyright © 2009 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.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.HashSet;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.fcp.highlevel.FcpClient;
32 import net.pterodactylus.fcp.highlevel.FcpException;
33 import net.pterodactylus.jsite.util.IdGenerator;
34 import net.pterodactylus.util.number.Hex;
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class RequestManager implements NodeListener {
44 private static final Logger logger = Logger.getLogger(RequestManager.class.getName());
46 /** Request event manager. */
47 private final RequestListenerManager requestListenerManager = new RequestListenerManager();
49 /** The node manager. */
50 private final NodeManager nodeManager;
52 /** Maps request IDs to requests. */
53 private final Map<String, Request> idRequests = Collections.synchronizedMap(new HashMap<String, Request>());
56 * Creates a new request manager.
59 * The node manager to utilize
61 public RequestManager(NodeManager nodeManager) {
62 this.nodeManager = nodeManager;
66 // LISTENER MANAGEMENT
70 * Adds the given request listener to the list of registered listeners.
72 * @see RequestListenerManager#addListener(RequestListener)
73 * @param requestListener
74 * The request listener to add
76 public void addRequestListener(RequestListener requestListener) {
77 requestListenerManager.addListener(requestListener);
81 * Removes the given request listener from the list of registered listeners.
83 * @see RequestListenerManager#removeListener(RequestListener)
84 * @param requestListener
85 * The request listener to remove
87 public void removeRequestListener(RequestListener requestListener) {
88 requestListenerManager.removeListener(requestListener);
95 public void load() throws IOException {
98 public void save() throws IOException {
106 * Checks whether the given client token is a client token created by this
110 * The client token to check
111 * @return {@code true} if the client token was created by this request
112 * manager, {@code false} otherwise
114 private boolean isKnownClientToken(String clientToken) {
115 String[] clientTokenParts = clientToken.split("\\.");
116 if (clientTokenParts.length != 3) {
119 String projectIdString = clientTokenParts[0];
120 if (projectIdString.length() != (IdGenerator.DEFAULT_LENGTH * 2)) {
123 int clientTokenHashCode = -1;
125 Hex.toByte(projectIdString);
126 Long.valueOf(clientTokenParts[1]);
127 clientTokenHashCode = Integer.valueOf(clientTokenParts[2]);
128 } catch (NumberFormatException nfe1) {
131 if ((clientTokenParts[0] + "." + clientTokenParts[1]).hashCode() != clientTokenHashCode) {
138 * Generates a client token for the given project.
141 * The project to generate a client token for
142 * @return The generated client token
144 public String generateClientToken(Project project) {
145 String clientToken = project.getId() + "." + System.currentTimeMillis();
146 clientToken += "." + clientToken.hashCode();
151 * Wraps the requests of the FCP API into jSite requests.
154 * The requests to wrap
155 * @return The wrapped requests
157 private Collection<Request> wrapRequests(Collection<net.pterodactylus.fcp.highlevel.Request> requests) {
158 Collection<Request> wrappedRequests = new HashSet<Request>();
159 for (net.pterodactylus.fcp.highlevel.Request request : requests) {
160 Request wrappedRequest = new Request(request.getIdentifier());
161 wrappedRequest.setClientToken(request.getClientToken());
162 wrappedRequests.add(wrappedRequest);
164 return wrappedRequests;
168 // INTERFACE NodeListener
174 public void nodeAdded(Node node) {
181 public void nodeConnected(Node node) {
182 FcpClient fcpClient = nodeManager.getFcpClient(node);
183 if (fcpClient == null) {
184 logger.log(Level.WARNING, "Got no FCP client for node (" + node + ")!");
188 Collection<Request> requests = wrapRequests(fcpClient.getRequests(true));
189 for (Request request : requests) {
190 String clientToken = request.getClientToken();
191 if ((clientToken == null) || (clientToken.trim().length() == 0)) {
194 if (!isKnownClientToken(clientToken)) {
197 /* TODO - process request. */
199 } catch (IOException ioe1) {
200 logger.log(Level.WARNING, "Could not get requests from node (" + node + ")!", ioe1);
201 } catch (FcpException fe1) {
202 logger.log(Level.WARNING, "Could not get requests from node (" + node + ")!", fe1);
209 public void nodeConnectionFailed(Node node, Throwable cause) {
216 public void nodeDisconnected(Node node, Throwable throwable) {
223 public void nodeRemoved(Node node) {