2 * Sone - PluginConnector.java - Copyright © 2010–2012 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.util.Collections;
21 import java.util.HashMap;
24 import net.pterodactylus.util.collection.Pair;
26 import com.google.inject.Inject;
28 import freenet.pluginmanager.FredPluginTalker;
29 import freenet.pluginmanager.PluginNotFoundException;
30 import freenet.pluginmanager.PluginRespirator;
31 import freenet.pluginmanager.PluginTalker;
32 import freenet.support.SimpleFieldSet;
33 import freenet.support.api.Bucket;
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>
41 public class PluginConnector implements FredPluginTalker {
43 /** The plugin respirator. */
44 private final PluginRespirator pluginRespirator;
46 /** Connector listener managers for all plugin connections. */
47 private final Map<Pair<String, String>, ConnectorListenerManager> connectorListenerManagers = Collections.synchronizedMap(new HashMap<Pair<String, String>, ConnectorListenerManager>());
50 * Creates a new plugin connector.
52 * @param pluginRespirator
53 * The plugin respirator
56 public PluginConnector(PluginRespirator pluginRespirator) {
57 this.pluginRespirator = pluginRespirator;
61 // LISTENER MANAGEMENT
65 * Adds a connection listener for the given plugin connection.
68 * The name of the plugin
70 * The identifier of the connection
71 * @param connectorListener
74 public void addConnectorListener(String pluginName, String identifier, ConnectorListener connectorListener) {
75 getConnectorListenerManager(pluginName, identifier).addListener(connectorListener);
79 * Removes a connection listener for the given plugin connection.
82 * The name of the plugin
84 * The identifier of the connection
85 * @param connectorListener
86 * The listener to remove
88 public void removeConnectorListener(String pluginName, String identifier, ConnectorListener connectorListener) {
89 getConnectorListenerManager(pluginName, identifier).removeListener(connectorListener);
97 * Sends a request to the given plugin.
100 * The name of the plugin
102 * The identifier of the connection
104 * The fields of the message
105 * @throws PluginException
106 * if the plugin can not be found
108 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
109 sendRequest(pluginName, identifier, fields, null);
113 * Sends a request to the given plugin.
116 * The name of the plugin
118 * The identifier of the connection
120 * The fields of the message
122 * The payload of the message (may be null)
123 * @throws PluginException
124 * if the plugin can not be found
126 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException {
127 getPluginTalker(pluginName, identifier).send(fields, data);
135 * Returns the connection listener manager for the given plugin connection,
136 * creating a new one if none does exist yet.
139 * The name of the plugin
141 * The identifier of the connection
142 * @return The connection listener manager
144 private ConnectorListenerManager getConnectorListenerManager(String pluginName, String identifier) {
145 return getConnectorListenerManager(pluginName, identifier, true);
149 * Returns the connection listener manager for the given plugin connection,
150 * optionally creating a new one if none does exist yet.
153 * The name of the plugin
155 * The identifier of the connection
157 * {@code true} to create a new manager if there is none,
158 * {@code false} to return {@code null} in that case
159 * @return The connection listener manager, or {@code null} if none existed
160 * and {@code create} is {@code false}
162 private ConnectorListenerManager getConnectorListenerManager(String pluginName, String identifier, boolean create) {
163 ConnectorListenerManager connectorListenerManager = connectorListenerManagers.get(new Pair<String, String>(pluginName, identifier));
164 if (create && (connectorListenerManager == null)) {
165 connectorListenerManager = new ConnectorListenerManager(this);
166 connectorListenerManagers.put(new Pair<String, String>(pluginName, identifier), connectorListenerManager);
168 return connectorListenerManager;
172 * Returns the plugin talker for the given plugin connection.
175 * The name of the plugin
177 * The identifier of the connection
178 * @return The plugin talker
179 * @throws PluginException
180 * if the plugin can not be found
182 private PluginTalker getPluginTalker(String pluginName, String identifier) throws PluginException {
184 return pluginRespirator.getPluginTalker(this, pluginName, identifier);
185 } catch (PluginNotFoundException pnfe1) {
186 throw new PluginException(pnfe1);
191 // INTERFACE FredPluginTalker
198 public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
199 ConnectorListenerManager connectorListenerManager = getConnectorListenerManager(pluginName, identifier, false);
200 if (connectorListenerManager == null) {
201 /* we don’t care about events for this plugin. */
204 connectorListenerManager.fireReceivedReply(params, data);