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;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import net.pterodactylus.fcp.highlevel.FcpClient;
31 import net.pterodactylus.fcp.highlevel.FcpException;
32 import net.pterodactylus.fcp.highlevel.Request;
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 /** The node manager. */
47 private final NodeManager nodeManager;
49 /** Maps request IDs to requests. */
50 private final Map<String, Request> idRequests = Collections.synchronizedMap(new HashMap<String, Request>());
53 * Creates a new request manager.
56 * The node manager to utilize
58 public RequestManager(NodeManager nodeManager) {
59 this.nodeManager = nodeManager;
66 public void load() throws IOException {
69 public void save() throws IOException {
77 * Checks whether the given client token is a client token created by this
81 * The client token to check
82 * @return {@code true} if the client token was created by this request
83 * manager, {@code false} otherwise
85 private boolean isKnownClientToken(String clientToken) {
86 String[] clientTokenParts = clientToken.split("\\.");
87 if (clientTokenParts.length != 3) {
90 String projectIdString = clientTokenParts[0];
91 if (projectIdString.length() != (IdGenerator.DEFAULT_LENGTH * 2)) {
95 Hex.toByte(projectIdString);
96 } catch (NumberFormatException nfe1) {
103 // INTERFACE NodeListener
109 public void nodeAdded(Node node) {
116 public void nodeConnected(Node node) {
117 FcpClient fcpClient = nodeManager.getFcpClient(node);
118 if (fcpClient == null) {
119 logger.log(Level.WARNING, "Got no FCP client for node (" + node + ")!");
123 Collection<Request> requests = fcpClient.getRequests(true);
124 for (Request request : requests) {
125 String clientToken = request.getClientToken();
126 if ((clientToken == null) || (clientToken.trim().length() == 0)) {
129 if (!isKnownClientToken(clientToken)) {
132 /* TODO - process request. */
134 } catch (IOException ioe1) {
135 logger.log(Level.WARNING, "Could not get requests from node (" + node + ")!", ioe1);
136 } catch (FcpException fe1) {
137 logger.log(Level.WARNING, "Could not get requests from node (" + node + ")!", fe1);
144 public void nodeConnectionFailed(Node node, Throwable cause) {
151 public void nodeDisconnected(Node node, Throwable throwable) {
158 public void nodeRemoved(Node node) {