2 * Sone - PluginConnector.java - Copyright © 2010–2016 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 net.pterodactylus.sone.freenet.plugin.event.ReceivedReplyEvent;
22 import com.google.common.eventbus.EventBus;
23 import com.google.inject.Inject;
24 import com.google.inject.Singleton;
26 import freenet.pluginmanager.FredPluginTalker;
27 import freenet.pluginmanager.PluginNotFoundException;
28 import freenet.pluginmanager.PluginRespirator;
29 import freenet.pluginmanager.PluginTalker;
30 import freenet.support.SimpleFieldSet;
31 import freenet.support.api.Bucket;
34 * Interface for talking to other plugins. Other plugins are identified by their
35 * name and a unique connection identifier.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40 public class PluginConnector implements FredPluginTalker {
43 private final EventBus eventBus;
45 /** The plugin respirator. */
46 private final PluginRespirator pluginRespirator;
49 * Creates a new plugin connector.
53 * @param pluginRespirator
54 * The plugin respirator
57 public PluginConnector(EventBus eventBus, PluginRespirator pluginRespirator) {
58 this.eventBus = eventBus;
59 this.pluginRespirator = pluginRespirator;
67 * Sends a request to the given plugin.
70 * The name of the plugin
72 * The identifier of the connection
74 * The fields of the message
75 * @throws PluginException
76 * if the plugin can not be found
78 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
79 sendRequest(pluginName, identifier, fields, null);
83 * Sends a request to the given plugin.
86 * The name of the plugin
88 * The identifier of the connection
90 * The fields of the message
92 * The payload of the message (may be null)
93 * @throws PluginException
94 * if the plugin can not be found
96 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException {
97 getPluginTalker(pluginName, identifier).send(fields, data);
105 * Returns the plugin talker for the given plugin connection.
108 * The name of the plugin
110 * The identifier of the connection
111 * @return The plugin talker
112 * @throws PluginException
113 * if the plugin can not be found
115 private PluginTalker getPluginTalker(String pluginName, String identifier) throws PluginException {
117 return pluginRespirator.getPluginTalker(this, pluginName, identifier);
118 } catch (PluginNotFoundException pnfe1) {
119 throw new PluginException(pnfe1);
124 // INTERFACE FredPluginTalker
131 public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
132 eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data));