Use dependency injection to create Sone instance.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / plugin / PluginConnector.java
1 /*
2  * Sone - PluginConnector.java - Copyright © 2010–2012 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.freenet.plugin;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import net.pterodactylus.util.collection.Pair;
25
26 import com.google.inject.Inject;
27
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;
34
35 /**
36  * Interface for talking to other plugins. Other plugins are identified by their
37  * name and a unique connection identifier.
38  *
39  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
40  */
41 public class PluginConnector implements FredPluginTalker {
42
43         /** The plugin respirator. */
44         private final PluginRespirator pluginRespirator;
45
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>());
48
49         /**
50          * Creates a new plugin connector.
51          *
52          * @param pluginRespirator
53          *            The plugin respirator
54          */
55         @Inject
56         public PluginConnector(PluginRespirator pluginRespirator) {
57                 this.pluginRespirator = pluginRespirator;
58         }
59
60         //
61         // LISTENER MANAGEMENT
62         //
63
64         /**
65          * Adds a connection listener for the given plugin connection.
66          *
67          * @param pluginName
68          *            The name of the plugin
69          * @param identifier
70          *            The identifier of the connection
71          * @param connectorListener
72          *            The listener to add
73          */
74         public void addConnectorListener(String pluginName, String identifier, ConnectorListener connectorListener) {
75                 getConnectorListenerManager(pluginName, identifier).addListener(connectorListener);
76         }
77
78         /**
79          * Removes a connection listener for the given plugin connection.
80          *
81          * @param pluginName
82          *            The name of the plugin
83          * @param identifier
84          *            The identifier of the connection
85          * @param connectorListener
86          *            The listener to remove
87          */
88         public void removeConnectorListener(String pluginName, String identifier, ConnectorListener connectorListener) {
89                 getConnectorListenerManager(pluginName, identifier).removeListener(connectorListener);
90         }
91
92         //
93         // ACTIONS
94         //
95
96         /**
97          * Sends a request to the given plugin.
98          *
99          * @param pluginName
100          *            The name of the plugin
101          * @param identifier
102          *            The identifier of the connection
103          * @param fields
104          *            The fields of the message
105          * @throws PluginException
106          *             if the plugin can not be found
107          */
108         public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
109                 sendRequest(pluginName, identifier, fields, null);
110         }
111
112         /**
113          * Sends a request to the given plugin.
114          *
115          * @param pluginName
116          *            The name of the plugin
117          * @param identifier
118          *            The identifier of the connection
119          * @param fields
120          *            The fields of the message
121          * @param data
122          *            The payload of the message (may be null)
123          * @throws PluginException
124          *             if the plugin can not be found
125          */
126         public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException {
127                 getPluginTalker(pluginName, identifier).send(fields, data);
128         }
129
130         //
131         // PRIVATE METHODS
132         //
133
134         /**
135          * Returns the connection listener manager for the given plugin connection,
136          * creating a new one if none does exist yet.
137          *
138          * @param pluginName
139          *            The name of the plugin
140          * @param identifier
141          *            The identifier of the connection
142          * @return The connection listener manager
143          */
144         private ConnectorListenerManager getConnectorListenerManager(String pluginName, String identifier) {
145                 return getConnectorListenerManager(pluginName, identifier, true);
146         }
147
148         /**
149          * Returns the connection listener manager for the given plugin connection,
150          * optionally creating a new one if none does exist yet.
151          *
152          * @param pluginName
153          *            The name of the plugin
154          * @param identifier
155          *            The identifier of the connection
156          * @param create
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}
161          */
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);
167                 }
168                 return connectorListenerManager;
169         }
170
171         /**
172          * Returns the plugin talker for the given plugin connection.
173          *
174          * @param pluginName
175          *            The name of the plugin
176          * @param identifier
177          *            The identifier of the connection
178          * @return The plugin talker
179          * @throws PluginException
180          *             if the plugin can not be found
181          */
182         private PluginTalker getPluginTalker(String pluginName, String identifier) throws PluginException {
183                 try {
184                         return pluginRespirator.getPluginTalker(this, pluginName, identifier);
185                 } catch (PluginNotFoundException pnfe1) {
186                         throw new PluginException(pnfe1);
187                 }
188         }
189
190         //
191         // INTERFACE FredPluginTalker
192         //
193
194         /**
195          * {@inheritDoc}
196          */
197         @Override
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. */
202                         return;
203                 }
204                 connectorListenerManager.fireReceivedReply(params, data);
205         }
206
207 }