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