2 * Sone - PluginConnector.java - Copyright © 2010–2019 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.support.SimpleFieldSet;
29 import freenet.support.api.Bucket;
32 * Interface for talking to other plugins. Other plugins are identified by their
33 * name and a unique connection identifier.
36 public class PluginConnector implements FredPluginTalker {
39 private final EventBus eventBus;
41 /** The plugin respirator. */
42 private final PluginRespiratorFacade pluginRespiratorFacade;
45 * Creates a new plugin connector.
49 * @param pluginRespiratorFacade
50 * The plugin respirator
53 public PluginConnector(EventBus eventBus, PluginRespiratorFacade pluginRespiratorFacade) {
54 this.eventBus = eventBus;
55 this.pluginRespiratorFacade = pluginRespiratorFacade;
63 * Sends a request to the given plugin.
66 * The name of the plugin
68 * The identifier of the connection
70 * The fields of the message
71 * @throws PluginException
72 * if the plugin can not be found
74 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
75 sendRequest(pluginName, identifier, fields, null);
79 * Sends a request to the given plugin.
82 * The name of the plugin
84 * The identifier of the connection
86 * The fields of the message
88 * The payload of the message (may be null)
89 * @throws PluginException
90 * if the plugin can not be found
92 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException {
93 getPluginTalker(pluginName, identifier).send(fields, data);
101 * Returns the plugin talker for the given plugin connection.
104 * The name of the plugin
106 * The identifier of the connection
107 * @return The plugin talker
108 * @throws PluginException
109 * if the plugin can not be found
111 private PluginTalkerFacade getPluginTalker(String pluginName, String identifier) throws PluginException {
113 return pluginRespiratorFacade.getPluginTalker(this, pluginName, identifier);
114 } catch (PluginNotFoundException pnfe1) {
115 throw new PluginException(pnfe1);
120 // INTERFACE FredPluginTalker
127 public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
128 eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data));