♻️ Use facade instead of real plugin respirator
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / plugin / PluginConnector.java
index e7bf828..aa45dbf 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - PluginConnector.java - Copyright © 2010 David Roden
+ * Sone - PluginConnector.java - Copyright © 2010–2019 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.freenet.plugin;
 
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
+import net.pterodactylus.sone.freenet.plugin.event.ReceivedReplyEvent;
+
+import com.google.common.eventbus.EventBus;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
 
-import net.pterodactylus.util.collection.Pair;
 import freenet.pluginmanager.FredPluginTalker;
 import freenet.pluginmanager.PluginNotFoundException;
-import freenet.pluginmanager.PluginRespirator;
-import freenet.pluginmanager.PluginTalker;
 import freenet.support.SimpleFieldSet;
 import freenet.support.api.Bucket;
 
 /**
  * Interface for talking to other plugins. Other plugins are identified by their
  * name and a unique connection identifier.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
+@Singleton
 public class PluginConnector implements FredPluginTalker {
 
-       /** The plugin respirator. */
-       private final PluginRespirator pluginRespirator;
+       /** The event bus. */
+       private final EventBus eventBus;
 
-       /** Connector listener managers for all plugin connections. */
-       private final Map<Pair<String, String>, ConnectorListenerManager> connectorListenerManagers = Collections.synchronizedMap(new HashMap<Pair<String, String>, ConnectorListenerManager>());
+       /** The plugin respirator. */
+       private final PluginRespiratorFacade pluginRespiratorFacade;
 
        /**
         * Creates a new plugin connector.
         *
-        * @param pluginRespirator
+        * @param eventBus
+        *            The event bus
+        * @param pluginRespiratorFacade
         *            The plugin respirator
         */
-       public PluginConnector(PluginRespirator pluginRespirator) {
-               this.pluginRespirator = pluginRespirator;
-       }
-
-       //
-       // LISTENER MANAGEMENT
-       //
-
-       /**
-        * Adds a connection listener for the given plugin connection.
-        *
-        * @param pluginName
-        *            The name of the plugin
-        * @param identifier
-        *            The identifier of the connection
-        * @param connectorListener
-        *            The listener to add
-        */
-       public void addConnectorListener(String pluginName, String identifier, ConnectorListener connectorListener) {
-               getConnectorListenerManager(pluginName, identifier).addListener(connectorListener);
-       }
-
-       /**
-        * Removes a connection listener for the given plugin connection.
-        *
-        * @param pluginName
-        *            The name of the plugin
-        * @param identifier
-        *            The identifier of the connection
-        * @param connectorListener
-        *            The listener to remove
-        */
-       public void removeConnectorListener(String pluginName, String identifier, ConnectorListener connectorListener) {
-               getConnectorListenerManager(pluginName, identifier).removeListener(connectorListener);
+       @Inject
+       public PluginConnector(EventBus eventBus, PluginRespiratorFacade pluginRespiratorFacade) {
+               this.eventBus = eventBus;
+               this.pluginRespiratorFacade = pluginRespiratorFacade;
        }
 
        //
@@ -128,43 +98,6 @@ public class PluginConnector implements FredPluginTalker {
        //
 
        /**
-        * Returns the connection listener manager for the given plugin connection,
-        * creating a new one if none does exist yet.
-        *
-        * @param pluginName
-        *            The name of the plugin
-        * @param identifier
-        *            The identifier of the connection
-        * @return The connection listener manager
-        */
-       private ConnectorListenerManager getConnectorListenerManager(String pluginName, String identifier) {
-               return getConnectorListenerManager(pluginName, identifier, true);
-       }
-
-       /**
-        * Returns the connection listener manager for the given plugin connection,
-        * optionally creating a new one if none does exist yet.
-        *
-        * @param pluginName
-        *            The name of the plugin
-        * @param identifier
-        *            The identifier of the connection
-        * @param create
-        *            {@code true} to create a new manager if there is none,
-        *            {@code false} to return {@code null} in that case
-        * @return The connection listener manager, or {@code null} if none existed
-        *         and {@code create} is {@code false}
-        */
-       private ConnectorListenerManager getConnectorListenerManager(String pluginName, String identifier, boolean create) {
-               ConnectorListenerManager connectorListenerManager = connectorListenerManagers.get(new Pair<String, String>(pluginName, identifier));
-               if (create && (connectorListenerManager == null)) {
-                       connectorListenerManager = new ConnectorListenerManager(this);
-                       connectorListenerManagers.put(new Pair<String, String>(pluginName, identifier), connectorListenerManager);
-               }
-               return connectorListenerManager;
-       }
-
-       /**
         * Returns the plugin talker for the given plugin connection.
         *
         * @param pluginName
@@ -175,9 +108,9 @@ public class PluginConnector implements FredPluginTalker {
         * @throws PluginException
         *             if the plugin can not be found
         */
-       private PluginTalker getPluginTalker(String pluginName, String identifier) throws PluginException {
+       private PluginTalkerFacade getPluginTalker(String pluginName, String identifier) throws PluginException {
                try {
-                       return pluginRespirator.getPluginTalker(this, pluginName, identifier);
+                       return pluginRespiratorFacade.getPluginTalker(this, pluginName, identifier);
                } catch (PluginNotFoundException pnfe1) {
                        throw new PluginException(pnfe1);
                }
@@ -192,12 +125,7 @@ public class PluginConnector implements FredPluginTalker {
         */
        @Override
        public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) {
-               ConnectorListenerManager connectorListenerManager = getConnectorListenerManager(pluginName, identifier, false);
-               if (connectorListenerManager == null) {
-                       /* we don’t care about events for this plugin. */
-                       return;
-               }
-               connectorListenerManager.fireReceivedReply(params, data);
+               eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data));
        }
 
 }