56ac80795e76aa1130748c6c5dd1367623946ff6
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / plugin / PluginConnector.java
1 /*
2  * Sone - PluginConnector.java - Copyright © 2010–2013 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 net.pterodactylus.sone.freenet.plugin.event.ReceivedReplyEvent;
21
22 import com.google.common.eventbus.EventBus;
23 import com.google.inject.Inject;
24 import com.google.inject.Singleton;
25
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;
32
33 /**
34  * Interface for talking to other plugins. Other plugins are identified by their
35  * name and a unique connection identifier.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 @Singleton
40 public class PluginConnector implements FredPluginTalker {
41
42         /** The event bus. */
43         private final EventBus eventBus;
44
45         /** The plugin respirator. */
46         private final PluginRespirator pluginRespirator;
47
48         /**
49          * Creates a new plugin connector.
50          *
51          * @param eventBus
52          *            The event bus
53          * @param pluginRespirator
54          *            The plugin respirator
55          */
56         @Inject
57         public PluginConnector(EventBus eventBus, PluginRespirator pluginRespirator) {
58                 this.eventBus = eventBus;
59                 this.pluginRespirator = pluginRespirator;
60         }
61
62         //
63         // ACTIONS
64         //
65
66         /**
67          * Sends a request to the given plugin.
68          *
69          * @param pluginName
70          *            The name of the plugin
71          * @param identifier
72          *            The identifier of the connection
73          * @param fields
74          *            The fields of the message
75          * @throws PluginException
76          *             if the plugin can not be found
77          */
78         public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
79                 sendRequest(pluginName, identifier, fields, null);
80         }
81
82         /**
83          * Sends a request to the given plugin.
84          *
85          * @param pluginName
86          *            The name of the plugin
87          * @param identifier
88          *            The identifier of the connection
89          * @param fields
90          *            The fields of the message
91          * @param data
92          *            The payload of the message (may be null)
93          * @throws PluginException
94          *             if the plugin can not be found
95          */
96         public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException {
97                 getPluginTalker(pluginName, identifier).send(fields, data);
98         }
99
100         //
101         // PRIVATE METHODS
102         //
103
104         /**
105          * Returns the plugin talker for the given plugin connection.
106          *
107          * @param pluginName
108          *            The name of the plugin
109          * @param identifier
110          *            The identifier of the connection
111          * @return The plugin talker
112          * @throws PluginException
113          *             if the plugin can not be found
114          */
115         private PluginTalker getPluginTalker(String pluginName, String identifier) throws PluginException {
116                 try {
117                         return pluginRespirator.getPluginTalker(this, pluginName, identifier);
118                 } catch (PluginNotFoundException pnfe1) {
119                         throw new PluginException(pnfe1);
120                 }
121         }
122
123         //
124         // INTERFACE FredPluginTalker
125         //
126
127         /**
128          * {@inheritDoc}
129          */
130         @Override
131         public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
132                 eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data));
133         }
134
135 }