Wrapped FCP API requests into jSite requests.
[jSite2.git] / src / net / pterodactylus / jsite / core / RequestManager.java
1 /*
2  * jSite-next - RequestManager.java -
3  * Copyright © 2009 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jsite.core;
21
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;
27 import java.util.Map;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
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;
35
36 /**
37  * Manages requests.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class RequestManager implements NodeListener {
42
43         /** The logger. */
44         private static final Logger logger = Logger.getLogger(RequestManager.class.getName());
45
46         /** The node manager. */
47         private final NodeManager nodeManager;
48
49         /** Maps request IDs to requests. */
50         private final Map<String, Request> idRequests = Collections.synchronizedMap(new HashMap<String, Request>());
51
52         /**
53          * Creates a new request manager.
54          *
55          * @param nodeManager
56          *            The node manager to utilize
57          */
58         public RequestManager(NodeManager nodeManager) {
59                 this.nodeManager = nodeManager;
60         }
61
62         //
63         // ACTIONS
64         //
65
66         public void load() throws IOException {
67         }
68
69         public void save() throws IOException {
70         }
71
72         //
73         // PRIVATE METHODS
74         //
75
76         /**
77          * Checks whether the given client token is a client token created by this
78          * request manager.
79          *
80          * @param clientToken
81          *            The client token to check
82          * @return {@code true} if the client token was created by this request
83          *         manager, {@code false} otherwise
84          */
85         private boolean isKnownClientToken(String clientToken) {
86                 String[] clientTokenParts = clientToken.split("\\.");
87                 if (clientTokenParts.length != 3) {
88                         return false;
89                 }
90                 String projectIdString = clientTokenParts[0];
91                 if (projectIdString.length() != (IdGenerator.DEFAULT_LENGTH * 2)) {
92                         return false;
93                 }
94                 try {
95                         Hex.toByte(projectIdString);
96                 } catch (NumberFormatException nfe1) {
97                         return false;
98                 }
99                 return true;
100         }
101
102         /**
103          * Wraps the requests of the FCP API into jSite requests.
104          *
105          * @param requests
106          *            The requests to wrap
107          * @return The wrapped requests
108          */
109         private Collection<Request> wrapRequests(Collection<net.pterodactylus.fcp.highlevel.Request> requests) {
110                 Collection<Request> wrappedRequests = new HashSet<Request>();
111                 for (net.pterodactylus.fcp.highlevel.Request request : requests) {
112                         Request wrappedRequest = new Request(request.getIdentifier());
113                         wrappedRequest.setClientToken(request.getClientToken());
114                         wrappedRequests.add(wrappedRequest);
115                 }
116                 return wrappedRequests;
117         }
118
119         //
120         // INTERFACE NodeListener
121         //
122
123         /**
124          * {@inheritDoc}
125          */
126         public void nodeAdded(Node node) {
127                 /* ignore. */
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         public void nodeConnected(Node node) {
134                 FcpClient fcpClient = nodeManager.getFcpClient(node);
135                 if (fcpClient == null) {
136                         logger.log(Level.WARNING, "Got no FCP client for node (" + node + ")!");
137                         return;
138                 }
139                 try {
140                         Collection<Request> requests = wrapRequests(fcpClient.getRequests(true));
141                         for (Request request : requests) {
142                                 String clientToken = request.getClientToken();
143                                 if ((clientToken == null) || (clientToken.trim().length() == 0)) {
144                                         continue;
145                                 }
146                                 if (!isKnownClientToken(clientToken)) {
147                                         continue;
148                                 }
149                                 /* TODO - process request. */
150                         }
151                 } catch (IOException ioe1) {
152                         logger.log(Level.WARNING, "Could not get requests from node (" + node + ")!", ioe1);
153                 } catch (FcpException fe1) {
154                         logger.log(Level.WARNING, "Could not get requests from node (" + node + ")!", fe1);
155                 }
156         }
157
158         /**
159          * {@inheritDoc}
160          */
161         public void nodeConnectionFailed(Node node, Throwable cause) {
162                 /* ignore. */
163         }
164
165         /**
166          * {@inheritDoc}
167          */
168         public void nodeDisconnected(Node node, Throwable throwable) {
169                 /* TODO */
170         }
171
172         /**
173          * {@inheritDoc}
174          */
175         public void nodeRemoved(Node node) {
176                 /* ignore. */
177         }
178
179 }