✅ Add test for plugin connector
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / plugin / PluginConnector.java
index d556a6c..aa45dbf 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - PluginConnector.java - Copyright © 2010–2012 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 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<PluginIdentifier, ConnectorListenerManager> connectorListenerManagers = Collections.synchronizedMap(new HashMap<PluginIdentifier, 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
         */
        @Inject
-       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);
+       public PluginConnector(EventBus eventBus, PluginRespiratorFacade pluginRespiratorFacade) {
+               this.eventBus = eventBus;
+               this.pluginRespiratorFacade = pluginRespiratorFacade;
        }
 
        //
@@ -130,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 PluginIdentifier(pluginName, identifier));
-               if (create && (connectorListenerManager == null)) {
-                       connectorListenerManager = new ConnectorListenerManager(this);
-                       connectorListenerManagers.put(new PluginIdentifier(pluginName, identifier), connectorListenerManager);
-               }
-               return connectorListenerManager;
-       }
-
-       /**
         * Returns the plugin talker for the given plugin connection.
         *
         * @param pluginName
@@ -177,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);
                }
@@ -194,65 +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);
-       }
-
-       /**
-        * Container for identifying plugins. Plugins are identified by their plugin
-        * name and their unique identifier.
-        *
-        * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
-        */
-       private static class PluginIdentifier {
-
-               /** The plugin name. */
-               private final String pluginName;
-
-               /** The plugin identifier. */
-               private final String identifier;
-
-               /**
-                * Creates a new plugin identifier.
-                *
-                * @param pluginName
-                *            The name of the plugin
-                * @param identifier
-                *            The identifier of the plugin
-                */
-               public PluginIdentifier(String pluginName, String identifier) {
-                       this.pluginName = pluginName;
-                       this.identifier = identifier;
-               }
-
-               //
-               // OBJECT METHODS
-               //
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               public int hashCode() {
-                       return pluginName.hashCode() ^ identifier.hashCode();
-               }
-
-               /**
-                * {@inheritDoc}
-                */
-               @Override
-               public boolean equals(Object object) {
-                       if (!(object instanceof PluginIdentifier)) {
-                               return false;
-                       }
-                       PluginIdentifier pluginIdentifier = (PluginIdentifier) object;
-                       return pluginName.equals(pluginIdentifier.pluginName) && identifier.equals(pluginIdentifier.identifier);
-               }
-
+               eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data));
        }
 
 }