2 * fplugin - 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.freenet.plugin;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
23 import net.pterodactylus.freenet.plugin.PluginException;
24 import net.pterodactylus.freenet.plugin.event.ReceivedReplyEvent;
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;
33 import com.google.common.eventbus.EventBus;
36 * Interface for talking to other plugins. Other plugins are identified by their
37 * name and a unique connection identifier.
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class PluginConnector implements FredPluginTalker {
45 private final EventBus eventBus;
47 /** The plugin respirator. */
48 private final PluginRespirator pluginRespirator;
51 * Creates a new plugin connector.
55 * @param pluginRespirator
56 * The plugin respirator
59 public PluginConnector(EventBus eventBus, PluginRespirator pluginRespirator) {
60 this.eventBus = eventBus;
61 this.pluginRespirator = pluginRespirator;
69 * Sends a request to the given plugin.
72 * The name of the plugin
74 * The identifier of the connection
76 * The fields of the message
77 * @throws PluginException
78 * if the plugin can not be found
80 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
81 sendRequest(pluginName, identifier, fields, null);
85 * Sends a request to the given plugin.
88 * The name of the plugin
90 * The identifier of the connection
92 * The fields of the message
94 * The payload of the message (may be null)
95 * @throws PluginException
96 * if the plugin can not be found
98 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data)
99 throws PluginException {
100 getPluginTalker(pluginName, identifier).send(fields, data);
108 * Returns the plugin talker for the given plugin connection.
111 * The name of the plugin
113 * The identifier of the connection
114 * @return The plugin talker
115 * @throws PluginException
116 * if the plugin can not be found
118 private PluginTalker getPluginTalker(String pluginName, String identifier) throws PluginException {
120 return pluginRespirator.getPluginTalker(this, pluginName, identifier);
121 } catch (PluginNotFoundException pnfe1) {
122 throw new PluginException(pnfe1);
127 // INTERFACE FredPluginTalker
134 public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
135 eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data));