2 * Sone - PluginConnector.java - Copyright © 2010–2013 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;
25 import freenet.pluginmanager.FredPluginTalker;
26 import freenet.pluginmanager.PluginNotFoundException;
27 import freenet.pluginmanager.PluginRespirator;
28 import freenet.pluginmanager.PluginTalker;
29 import freenet.support.SimpleFieldSet;
30 import freenet.support.api.Bucket;
33 * Interface for talking to other plugins. Other plugins are identified by their
34 * name and a unique connection identifier.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class PluginConnector implements FredPluginTalker {
41 private final EventBus eventBus;
43 /** The plugin respirator. */
44 private final PluginRespirator pluginRespirator;
47 * Creates a new plugin connector.
51 * @param pluginRespirator
52 * The plugin respirator
55 public PluginConnector(EventBus eventBus, PluginRespirator pluginRespirator) {
56 this.eventBus = eventBus;
57 this.pluginRespirator = pluginRespirator;
65 * Sends a request to the given plugin.
68 * The name of the plugin
70 * The identifier of the connection
72 * The fields of the message
73 * @throws PluginException
74 * if the plugin can not be found
76 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
77 sendRequest(pluginName, identifier, fields, null);
81 * Sends a request to the given plugin.
84 * The name of the plugin
86 * The identifier of the connection
88 * The fields of the message
90 * The payload of the message (may be null)
91 * @throws PluginException
92 * if the plugin can not be found
94 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException {
95 getPluginTalker(pluginName, identifier).send(fields, data);
103 * Returns the plugin talker for the given plugin connection.
106 * The name of the plugin
108 * The identifier of the connection
109 * @return The plugin talker
110 * @throws PluginException
111 * if the plugin can not be found
113 private PluginTalker getPluginTalker(String pluginName, String identifier) throws PluginException {
115 return pluginRespirator.getPluginTalker(this, pluginName, identifier);
116 } catch (PluginNotFoundException pnfe1) {
117 throw new PluginException(pnfe1);
122 // INTERFACE FredPluginTalker
129 public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
130 eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data));