2 * Sone - PluginConnector.java - Copyright © 2010–2015 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.freenet.plugin;
20 import java.io.IOException;
21 import java.util.logging.Logger;
23 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
24 import net.pterodactylus.sone.freenet.plugin.event.ReceivedReplyEvent;
26 import com.google.common.eventbus.EventBus;
27 import com.google.inject.Inject;
28 import com.google.inject.Singleton;
30 import freenet.clients.fcp.FCPPluginConnection;
31 import freenet.clients.fcp.FCPPluginMessage;
32 import freenet.pluginmanager.FredPluginFCPMessageHandler.ClientSideFCPMessageHandler;
33 import freenet.pluginmanager.FredPluginTalker;
34 import freenet.pluginmanager.PluginNotFoundException;
35 import freenet.pluginmanager.PluginRespirator;
36 import freenet.pluginmanager.PluginTalker;
37 import freenet.support.SimpleFieldSet;
38 import freenet.support.api.Bucket;
41 * Interface for talking to other plugins. Other plugins are identified by their
42 * name and a unique connection identifier.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class PluginConnector implements FredPluginTalker, ClientSideFCPMessageHandler {
49 private static final Logger logger = Logger.getLogger(PluginConnector.class.getName());
52 private final EventBus eventBus;
54 /** The plugin respirator. */
55 private final PluginRespirator pluginRespirator;
58 * Creates a new plugin connector.
62 * @param pluginRespirator
63 * The plugin respirator
66 public PluginConnector(EventBus eventBus, PluginRespirator pluginRespirator) {
67 this.eventBus = eventBus;
68 this.pluginRespirator = pluginRespirator;
71 public void start() throws PluginException {
73 FCPPluginConnection pluginConnection = pluginRespirator.connectToOtherPlugin("plugins.WebOfTrust.WebOfTrust", this);
74 logger.fine("Got PluginConnection: " + pluginConnection);
75 SimpleFieldSet simpleFieldSet = new SimpleFieldSetBuilder().put("Message", "Subscribe").put("To", "Identities").get();
76 pluginConnection.send(FCPPluginMessage.construct(simpleFieldSet, null));
77 } catch (PluginNotFoundException pnfe1) {
78 throw new PluginException(pnfe1);
79 } catch (IOException ioe1) {
80 throw new PluginException(ioe1);
85 public FCPPluginMessage handlePluginFCPMessage(FCPPluginConnection connection, FCPPluginMessage message) {
86 logger.fine("Got Reply: " + message);
91 * Sends a request to the given plugin.
94 * The name of the plugin
96 * The identifier of the connection
98 * The fields of the message
99 * @throws PluginException
100 * if the plugin can not be found
102 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
103 sendRequest(pluginName, identifier, fields, null);
107 * Sends a request to the given plugin.
110 * The name of the plugin
112 * The identifier of the connection
114 * The fields of the message
116 * The payload of the message (may be null)
117 * @throws PluginException
118 * if the plugin can not be found
120 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException {
121 getPluginTalker(pluginName, identifier).send(fields, data);
129 * Returns the plugin talker for the given plugin connection.
132 * The name of the plugin
134 * The identifier of the connection
135 * @return The plugin talker
136 * @throws PluginException
137 * if the plugin can not be found
139 private PluginTalker getPluginTalker(String pluginName, String identifier) throws PluginException {
141 return pluginRespirator.getPluginTalker(this, pluginName, identifier);
142 } catch (PluginNotFoundException pnfe1) {
143 throw new PluginException(pnfe1);
148 // INTERFACE FredPluginTalker
155 public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
156 eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data));