c6af12d047fc0d7dbf3fc378259a8aa86116055d
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / fcp / FcpInterface.java
1 /*
2  * Sone - FcpInterface.java - Copyright © 2011 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify it under
5  * the terms of the GNU General Public License as published by the Free Software
6  * Foundation, either version 3 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.freenet.fcp;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import net.pterodactylus.sone.core.Core;
25 import net.pterodactylus.sone.freenet.fcp.Command.AccessType;
26 import net.pterodactylus.sone.freenet.fcp.Command.Reply;
27 import freenet.pluginmanager.PluginNotFoundException;
28 import freenet.pluginmanager.PluginReplySender;
29 import freenet.support.SimpleFieldSet;
30 import freenet.support.api.Bucket;
31
32 /**
33  * Implementation of an FCP interface for other clients or plugins to
34  * communicate with Sone.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class FcpInterface {
39
40         private final Core core;
41         private final Map<String, Command> commands = Collections.synchronizedMap(new HashMap<String, Command>());
42
43         public FcpInterface(Core core) {
44                 this.core = core;
45                 commands.put("Version", new VersionCommand());
46                 commands.put("GetPostFeed", new GetPostFeedCommand(core));
47         }
48
49         public void handle(PluginReplySender pluginReplySender, SimpleFieldSet parameters, Bucket data, int accessType) {
50                 Command command = commands.get(parameters.get("Message"));
51                 if (command == null) {
52                         /* TODO - return error? */
53                         return;
54                 }
55                 String identifier = parameters.get("Identifier");
56                 if ((identifier == null) || (identifier.length() == 0)) {
57                         /* TODO - return error? */
58                         return;
59                 }
60                 Reply reply = command.execute(parameters, data, AccessType.values()[accessType]);
61                 try {
62                         SimpleFieldSet replyParameters = reply.getReplyParameters();
63                         replyParameters.putOverwrite("Identifier", identifier);
64                         if (reply.hasData()) {
65                                 pluginReplySender.send(replyParameters, reply.getData());
66                         } else if (reply.hasBucket()) {
67                                 pluginReplySender.send(replyParameters, reply.getBucket());
68                         } else {
69                                 pluginReplySender.send(replyParameters);
70                         }
71                 } catch (PluginNotFoundException pnfe1) {
72                         /* TODO - log */
73                 }
74         }
75
76 }