♻️ Use facade instead of real plugin respirator
[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.support.SimpleFieldSet;
29 import freenet.support.api.Bucket;
30
31 /**
32  * Interface for talking to other plugins. Other plugins are identified by their
33  * name and a unique connection identifier.
34  */
35 @Singleton
36 public class PluginConnector implements FredPluginTalker {
37
38         /** The event bus. */
39         private final EventBus eventBus;
40
41         /** The plugin respirator. */
42         private final PluginRespiratorFacade pluginRespiratorFacade;
43
44         /**
45          * Creates a new plugin connector.
46          *
47          * @param eventBus
48          *            The event bus
49          * @param pluginRespiratorFacade
50          *            The plugin respirator
51          */
52         @Inject
53         public PluginConnector(EventBus eventBus, PluginRespiratorFacade pluginRespiratorFacade) {
54                 this.eventBus = eventBus;
55                 this.pluginRespiratorFacade = pluginRespiratorFacade;
56         }
57
58         //
59         // ACTIONS
60         //
61
62         /**
63          * Sends a request to the given plugin.
64          *
65          * @param pluginName
66          *            The name of the plugin
67          * @param identifier
68          *            The identifier of the connection
69          * @param fields
70          *            The fields of the message
71          * @throws PluginException
72          *             if the plugin can not be found
73          */
74         public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
75                 sendRequest(pluginName, identifier, fields, null);
76         }
77
78         /**
79          * Sends a request to the given plugin.
80          *
81          * @param pluginName
82          *            The name of the plugin
83          * @param identifier
84          *            The identifier of the connection
85          * @param fields
86          *            The fields of the message
87          * @param data
88          *            The payload of the message (may be null)
89          * @throws PluginException
90          *             if the plugin can not be found
91          */
92         public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException {
93                 getPluginTalker(pluginName, identifier).send(fields, data);
94         }
95
96         //
97         // PRIVATE METHODS
98         //
99
100         /**
101          * Returns the plugin talker for the given plugin connection.
102          *
103          * @param pluginName
104          *            The name of the plugin
105          * @param identifier
106          *            The identifier of the connection
107          * @return The plugin talker
108          * @throws PluginException
109          *             if the plugin can not be found
110          */
111         private PluginTalkerFacade getPluginTalker(String pluginName, String identifier) throws PluginException {
112                 try {
113                         return pluginRespiratorFacade.getPluginTalker(this, pluginName, identifier);
114                 } catch (PluginNotFoundException pnfe1) {
115                         throw new PluginException(pnfe1);
116                 }
117         }
118
119         //
120         // INTERFACE FredPluginTalker
121         //
122
123         /**
124          * {@inheritDoc}
125          */
126         @Override
127         public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
128                 eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data));
129         }
130
131 }