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;
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 {
40 /** The plugin respirator. */
41 private final PluginRespirator pluginRespirator;
43 /** Connector listener managers for all plugin connections. */
44 private final Map<Pair<String, String>, ConnectorListenerManager> connectorListenerManagers = Collections.synchronizedMap(new HashMap<Pair<String, String>, ConnectorListenerManager>());
47 * Creates a new plugin connector.
49 * @param pluginRespirator
50 * The plugin respirator
52 public PluginConnector(PluginRespirator pluginRespirator) {
53 this.pluginRespirator = pluginRespirator;
57 // LISTENER MANAGEMENT
61 * Adds a connection listener for the given plugin connection.
64 * The name of the plugin
66 * The identifier of the connection
67 * @param connectorListener
70 public void addConnectorListener(String pluginName, String identifier, ConnectorListener connectorListener) {
71 getConnectorListenerManager(pluginName, identifier).addListener(connectorListener);
75 * Removes a connection listener for the given plugin connection.
78 * The name of the plugin
80 * The identifier of the connection
81 * @param connectorListener
82 * The listener to remove
84 public void removeConnectorListener(String pluginName, String identifier, ConnectorListener connectorListener) {
85 getConnectorListenerManager(pluginName, identifier).removeListener(connectorListener);
93 * Sends a request to the given plugin.
96 * The name of the plugin
98 * The identifier of the connection
100 * The fields of the message
101 * @throws PluginException
102 * if the plugin can not be found
104 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
105 sendRequest(pluginName, identifier, fields, null);
109 * Sends a request to the given plugin.
112 * The name of the plugin
114 * The identifier of the connection
116 * The fields of the message
118 * The payload of the message (may be null)
119 * @throws PluginException
120 * if the plugin can not be found
122 public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException {
123 getPluginTalker(pluginName, identifier).send(fields, data);
131 * Returns the connection listener manager for the given plugin connection,
132 * creating a new one if none does exist yet.
135 * The name of the plugin
137 * The identifier of the connection
138 * @return The connection listener manager
140 private ConnectorListenerManager getConnectorListenerManager(String pluginName, String identifier) {
141 return getConnectorListenerManager(pluginName, identifier, true);
145 * Returns the connection listener manager for the given plugin connection,
146 * optionally creating a new one if none does exist yet.
149 * The name of the plugin
151 * The identifier of the connection
153 * {@code true} to create a new manager if there is none,
154 * {@code false} to return {@code null} in that case
155 * @return The connection listener manager, or {@code null} if none existed
156 * and {@code create} is {@code false}
158 private ConnectorListenerManager getConnectorListenerManager(String pluginName, String identifier, boolean create) {
159 ConnectorListenerManager connectorListenerManager = connectorListenerManagers.get(new Pair<String, String>(pluginName, identifier));
160 if (create && (connectorListenerManager == null)) {
161 connectorListenerManager = new ConnectorListenerManager(this);
162 connectorListenerManagers.put(new Pair<String, String>(pluginName, identifier), connectorListenerManager);
164 return connectorListenerManager;
168 * Returns the plugin talker for the given plugin connection.
171 * The name of the plugin
173 * The identifier of the connection
174 * @return The plugin talker
175 * @throws PluginException
176 * if the plugin can not be found
178 private PluginTalker getPluginTalker(String pluginName, String identifier) throws PluginException {
180 return pluginRespirator.getPluginTalker(this, pluginName, identifier);
181 } catch (PluginNotFoundException pnfe1) {
182 throw new PluginException(pnfe1);
187 // INTERFACE FredPluginTalker
194 public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
195 ConnectorListenerManager connectorListenerManager = getConnectorListenerManager(pluginName, identifier, false);
196 if (connectorListenerManager == null) {
197 /* we don’t care about events for this plugin. */
200 connectorListenerManager.fireReceivedReply(params, data);