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