2 * Sone - FcpInterface.java - Copyright © 2011 David Roden
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
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
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/>.
18 package net.pterodactylus.sone.freenet.fcp;
20 import java.util.Collections;
21 import java.util.HashMap;
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;
33 * Implementation of an FCP interface for other clients or plugins to
34 * communicate with Sone.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class FcpInterface {
40 private final Core core;
41 private final Map<String, Command> commands = Collections.synchronizedMap(new HashMap<String, Command>());
43 public FcpInterface(Core core) {
45 commands.put("Version", new VersionCommand());
46 commands.put("GetPostFeed", new GetPostFeedCommand(core));
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? */
55 String identifier = parameters.get("Identifier");
56 if ((identifier == null) || (identifier.length() == 0)) {
57 /* TODO - return error? */
60 Reply reply = command.execute(parameters, data, AccessType.values()[accessType]);
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());
69 pluginReplySender.send(replyParameters);
71 } catch (PluginNotFoundException pnfe1) {