send a message to wot and see what comes back
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / plugin / PluginConnector.java
1 /*
2  * Sone - 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.sone.freenet.plugin;
19
20 import java.io.IOException;
21 import java.util.logging.Logger;
22
23 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
24 import net.pterodactylus.sone.freenet.plugin.event.ReceivedReplyEvent;
25
26 import com.google.common.eventbus.EventBus;
27 import com.google.inject.Inject;
28 import com.google.inject.Singleton;
29
30 import freenet.clients.fcp.FCPPluginConnection;
31 import freenet.clients.fcp.FCPPluginMessage;
32 import freenet.pluginmanager.FredPluginFCPMessageHandler.ClientSideFCPMessageHandler;
33 import freenet.pluginmanager.FredPluginTalker;
34 import freenet.pluginmanager.PluginNotFoundException;
35 import freenet.pluginmanager.PluginRespirator;
36 import freenet.pluginmanager.PluginTalker;
37 import freenet.support.SimpleFieldSet;
38 import freenet.support.api.Bucket;
39
40 /**
41  * Interface for talking to other plugins. Other plugins are identified by their
42  * name and a unique connection identifier.
43  *
44  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45  */
46 @Singleton
47 public class PluginConnector implements FredPluginTalker, ClientSideFCPMessageHandler {
48
49         private static final Logger logger = Logger.getLogger(PluginConnector.class.getName());
50
51         /** The event bus. */
52         private final EventBus eventBus;
53
54         /** The plugin respirator. */
55         private final PluginRespirator pluginRespirator;
56
57         /**
58          * Creates a new plugin connector.
59          *
60          * @param eventBus
61          *            The event bus
62          * @param pluginRespirator
63          *            The plugin respirator
64          */
65         @Inject
66         public PluginConnector(EventBus eventBus, PluginRespirator pluginRespirator) {
67                 this.eventBus = eventBus;
68                 this.pluginRespirator = pluginRespirator;
69         }
70
71         public void start() throws PluginException {
72                 try {
73                         FCPPluginConnection pluginConnection = pluginRespirator.connectToOtherPlugin("plugins.WebOfTrust.WebOfTrust", this);
74                         logger.fine("Got PluginConnection: " + pluginConnection);
75                         SimpleFieldSet simpleFieldSet = new SimpleFieldSetBuilder().put("Message", "Subscribe").put("To", "Identities").get();
76                         pluginConnection.send(FCPPluginMessage.construct(simpleFieldSet, null));
77                 } catch (PluginNotFoundException pnfe1) {
78                         throw new PluginException(pnfe1);
79                 } catch (IOException ioe1) {
80                         throw new PluginException(ioe1);
81                 }
82         }
83
84         @Override
85         public FCPPluginMessage handlePluginFCPMessage(FCPPluginConnection connection, FCPPluginMessage message) {
86                 logger.fine("Got Reply: " + message);
87                 return null;
88         }
89
90         /**
91          * Sends a request to the given plugin.
92          *
93          * @param pluginName
94          *            The name of the plugin
95          * @param identifier
96          *            The identifier of the connection
97          * @param fields
98          *            The fields of the message
99          * @throws PluginException
100          *             if the plugin can not be found
101          */
102         public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException {
103                 sendRequest(pluginName, identifier, fields, null);
104         }
105
106         /**
107          * Sends a request to the given plugin.
108          *
109          * @param pluginName
110          *            The name of the plugin
111          * @param identifier
112          *            The identifier of the connection
113          * @param fields
114          *            The fields of the message
115          * @param data
116          *            The payload of the message (may be null)
117          * @throws PluginException
118          *             if the plugin can not be found
119          */
120         public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException {
121                 getPluginTalker(pluginName, identifier).send(fields, data);
122         }
123
124         //
125         // PRIVATE METHODS
126         //
127
128         /**
129          * Returns the plugin talker for the given plugin connection.
130          *
131          * @param pluginName
132          *            The name of the plugin
133          * @param identifier
134          *            The identifier of the connection
135          * @return The plugin talker
136          * @throws PluginException
137          *             if the plugin can not be found
138          */
139         private PluginTalker getPluginTalker(String pluginName, String identifier) throws PluginException {
140                 try {
141                         return pluginRespirator.getPluginTalker(this, pluginName, identifier);
142                 } catch (PluginNotFoundException pnfe1) {
143                         throw new PluginException(pnfe1);
144                 }
145         }
146
147         //
148         // INTERFACE FredPluginTalker
149         //
150
151         /**
152          * {@inheritDoc}
153          */
154         @Override
155         public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
156                 eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data));
157         }
158
159 }