Add stub of Web of Trust plugin connector.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / WebOfTrustConnector.java
1 /*
2  * Sone - WebOfTrustConnector.java - Copyright © 2010 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.freenet.wot;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 import net.pterodactylus.util.logging.Logging;
29 import freenet.support.SimpleFieldSet;
30 import freenet.support.api.Bucket;
31
32 /**
33  * Connector for the Web of Trust plugin.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class WebOfTrustConnector implements ConnectorListener {
38
39         /** The logger. */
40         private static final Logger logger = Logging.getLogger(WebOfTrustConnector.class);
41
42         /** The name of the WoT plugin. */
43         private static final String WOT_PLUGIN_NAME = "plugins.WoT.WoT";
44
45         /** A random connection identifier. */
46         private static final String PLUGIN_CONNECTION_IDENTIFIER = "Sone-WoT-Connector-" + Math.abs(Math.random());
47
48         /** The current replies that we wait for. */
49         private final Map<String, Reply> replies = Collections.synchronizedMap(new HashMap<String, Reply>());
50
51         /** The plugin connector. */
52         private final PluginConnector pluginConnector;
53
54         /**
55          * Creates a new Web of Trust connector that uses the given plugin
56          * connector.
57          *
58          * @param pluginConnector
59          *            The plugin connector
60          */
61         public WebOfTrustConnector(PluginConnector pluginConnector) {
62                 this.pluginConnector = pluginConnector;
63                 pluginConnector.addConnectorListener(WOT_PLUGIN_NAME, PLUGIN_CONNECTION_IDENTIFIER, this);
64         }
65
66         //
67         // ACTIONS
68         //
69
70         /**
71          * Loads all own identities from the Web of Trust plugin.
72          *
73          * @return All own identity
74          * @throws PluginException
75          *             if the own identities can not be loaded
76          */
77         public Set<OwnIdentity> loadAllOwnIdentities() throws PluginException {
78                 Reply reply = performRequest("OwnIdentities", SimpleFieldSetConstructor.create().put("Message", "GetOwnIdentities").get());
79                 SimpleFieldSet fields = reply.getFields();
80                 int ownIdentityCounter = -1;
81                 Set<OwnIdentity> ownIdentities = new HashSet<OwnIdentity>();
82                 while (true) {
83                         String id = fields.get("Identity" + ++ownIdentityCounter);
84                         if (id == null) {
85                                 break;
86                         }
87                         String requestUri = fields.get("RequestURI" + ownIdentityCounter);
88                         String insertUri = fields.get("InsertURI" + ownIdentityCounter);
89                         String nickname = fields.get("Nickname" + ownIdentityCounter);
90                         OwnIdentity ownIdentity = new OwnIdentity(id, nickname, requestUri, insertUri);
91                         ownIdentities.add(ownIdentity);
92                 }
93                 return ownIdentities;
94         }
95
96         //
97         // PRIVATE ACTIONS
98         //
99
100         /**
101          * Sends a request containing the given fields and waits for the target
102          * message.
103          *
104          * @param targetMessage
105          *            The message of the reply to wait for
106          * @param fields
107          *            The fields of the message
108          * @return The reply message
109          * @throws PluginException
110          *             if the request could not be sent
111          */
112         private Reply performRequest(String targetMessage, SimpleFieldSet fields) throws PluginException {
113                 return performRequest(targetMessage, fields, null);
114         }
115
116         /**
117          * Sends a request containing the given fields and waits for the target
118          * message.
119          *
120          * @param targetMessage
121          *            The message of the reply to wait for
122          * @param fields
123          *            The fields of the message
124          * @param data
125          *            The payload of the message
126          * @return The reply message
127          * @throws PluginException
128          *             if the request could not be sent
129          */
130         private Reply performRequest(String targetMessage, SimpleFieldSet fields, Bucket data) throws PluginException {
131                 @SuppressWarnings("synthetic-access")
132                 Reply reply = new Reply();
133                 replies.put(targetMessage, reply);
134                 synchronized (reply) {
135                         pluginConnector.sendRequest(WOT_PLUGIN_NAME, PLUGIN_CONNECTION_IDENTIFIER, fields, data);
136                         try {
137                                 reply.wait();
138                         } catch (InterruptedException ie1) {
139                                 logger.log(Level.WARNING, "Got interrupted while waiting for reply on GetOwnIdentities.", ie1);
140                         }
141                 }
142                 return reply;
143         }
144
145         //
146         // INTERFACE ConnectorListener
147         //
148
149         /**
150          * {@inheritDoc}
151          */
152         @Override
153         public void receivedReply(PluginConnector pluginConnector, SimpleFieldSet fields, Bucket data) {
154                 String messageName = fields.get("Message");
155                 Reply reply = replies.remove(messageName);
156                 if (reply == null) {
157                         logger.log(Level.FINE, "Not waiting for a “%s” message.", messageName);
158                         return;
159                 }
160                 synchronized (reply) {
161                         reply.setFields(fields);
162                         reply.setData(data);
163                         reply.notify();
164                 }
165         }
166
167         /**
168          * Container for the data of the reply from a plugin.
169          *
170          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
171          */
172         private static class Reply {
173
174                 /** The fields of the reply. */
175                 private SimpleFieldSet fields;
176
177                 /** The payload of the reply. */
178                 private Bucket data;
179
180                 /**
181                  * Returns the fields of the reply.
182                  *
183                  * @return The fields of the reply
184                  */
185                 public SimpleFieldSet getFields() {
186                         return fields;
187                 }
188
189                 /**
190                  * Sets the fields of the reply.
191                  *
192                  * @param fields
193                  *            The fields of the reply
194                  */
195                 public void setFields(SimpleFieldSet fields) {
196                         this.fields = fields;
197                 }
198
199                 /**
200                  * Returns the payload of the reply.
201                  *
202                  * @return The payload of the reply (may be {@code null})
203                  */
204                 @SuppressWarnings("unused")
205                 public Bucket getData() {
206                         return data;
207                 }
208
209                 /**
210                  * Sets the payload of the reply.
211                  *
212                  * @param data
213                  *            The payload of the reply (may be {@code null})
214                  */
215                 public void setData(Bucket data) {
216                         this.data = data;
217                 }
218
219         }
220
221         /**
222          * Helper method to create {@link SimpleFieldSet}s with terser code.
223          *
224          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
225          */
226         private static class SimpleFieldSetConstructor {
227
228                 /** The field set being created. */
229                 private SimpleFieldSet simpleFieldSet;
230
231                 /**
232                  * Creates a new simple field set constructor.
233                  *
234                  * @param shortLived
235                  *            {@code true} if the resulting simple field set should be
236                  *            short-lived, {@code false} otherwise
237                  */
238                 private SimpleFieldSetConstructor(boolean shortLived) {
239                         simpleFieldSet = new SimpleFieldSet(shortLived);
240                 }
241
242                 //
243                 // ACCESSORS
244                 //
245
246                 /**
247                  * Returns the created simple field set.
248                  *
249                  * @return The created simple field set
250                  */
251                 public SimpleFieldSet get() {
252                         return simpleFieldSet;
253                 }
254
255                 /**
256                  * Sets the field with the given name to the given value.
257                  *
258                  * @param name
259                  *            The name of the fleld
260                  * @param value
261                  *            The value of the field
262                  * @return This constructor (for method chaining)
263                  */
264                 public SimpleFieldSetConstructor put(String name, String value) {
265                         simpleFieldSet.putOverwrite(name, value);
266                         return this;
267                 }
268
269                 //
270                 // ACTIONS
271                 //
272
273                 /**
274                  * Creates a new simple field set constructor.
275                  *
276                  * @return The created simple field set constructor
277                  */
278                 public static SimpleFieldSetConstructor create() {
279                         return create(true);
280                 }
281
282                 /**
283                  * Creates a new simple field set constructor.
284                  *
285                  * @param shortLived
286                  *            {@code true} if the resulting simple field set should be
287                  *            short-lived, {@code false} otherwise
288                  * @return The created simple field set constructor
289                  */
290                 public static SimpleFieldSetConstructor create(boolean shortLived) {
291                         SimpleFieldSetConstructor simpleFieldSetConstructor = new SimpleFieldSetConstructor(shortLived);
292                         return simpleFieldSetConstructor;
293                 }
294
295         }
296
297 }