X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fplugin%2FPluginConnector.java;h=eb4d517f7d298a43a1ede4c120e24c3712750f5c;hp=d556a6c25697852966da19ff880aef112579aa92;hb=62573c314957b1851f4fbe693b8746686caa940a;hpb=036c6fa20ce11ce8093f46fc0fce1c1da1839789 diff --git a/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java b/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java index d556a6c..eb4d517 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java @@ -1,5 +1,5 @@ /* - * Sone - PluginConnector.java - Copyright © 2010–2012 David Roden + * Sone - PluginConnector.java - Copyright © 2010–2016 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 @@ -17,11 +17,11 @@ 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; @@ -33,61 +33,31 @@ import freenet.support.api.Bucket; /** * Interface for talking to other plugins. Other plugins are identified by their * name and a unique connection identifier. - * - * @author David ‘Bombe’ Roden */ +@Singleton public class PluginConnector implements FredPluginTalker { + /** The event bus. */ + private final EventBus eventBus; + /** The plugin respirator. */ private final PluginRespirator pluginRespirator; - /** Connector listener managers for all plugin connections. */ - private final Map connectorListenerManagers = Collections.synchronizedMap(new HashMap()); - /** * Creates a new plugin connector. * + * @param eventBus + * The event bus * @param pluginRespirator * The plugin respirator */ @Inject - public PluginConnector(PluginRespirator pluginRespirator) { + public PluginConnector(EventBus eventBus, PluginRespirator pluginRespirator) { + this.eventBus = eventBus; 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); - } - - // // ACTIONS // @@ -130,43 +100,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 @@ -194,65 +127,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 David Roden - */ - 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)); } }