From 5997c20d49159eaecf9ba51e769b22e63ce75525 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 27 Oct 2019 17:43:00 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20plugin=20connector=20wi?= =?utf8?q?th=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/freenet/plugin/PluginConnector.java | 131 --------------------- .../sone/freenet/plugin/PluginConnector.kt | 52 ++++++++ 2 files changed, 52 insertions(+), 131 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java create mode 100644 src/main/kotlin/net/pterodactylus/sone/freenet/plugin/PluginConnector.kt diff --git a/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java b/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java deleted file mode 100644 index aa45dbf..0000000 --- a/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.freenet.plugin; - -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.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. - */ -@Singleton -public class PluginConnector implements FredPluginTalker { - - /** The event bus. */ - private final EventBus eventBus; - - /** The plugin respirator. */ - private final PluginRespiratorFacade pluginRespiratorFacade; - - /** - * Creates a new plugin connector. - * - * @param eventBus - * The event bus - * @param pluginRespiratorFacade - * The plugin respirator - */ - @Inject - public PluginConnector(EventBus eventBus, PluginRespiratorFacade pluginRespiratorFacade) { - this.eventBus = eventBus; - this.pluginRespiratorFacade = pluginRespiratorFacade; - } - - // - // ACTIONS - // - - /** - * Sends a request to the given plugin. - * - * @param pluginName - * The name of the plugin - * @param identifier - * The identifier of the connection - * @param fields - * The fields of the message - * @throws PluginException - * if the plugin can not be found - */ - public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields) throws PluginException { - sendRequest(pluginName, identifier, fields, null); - } - - /** - * Sends a request to the given plugin. - * - * @param pluginName - * The name of the plugin - * @param identifier - * The identifier of the connection - * @param fields - * The fields of the message - * @param data - * The payload of the message (may be null) - * @throws PluginException - * if the plugin can not be found - */ - public void sendRequest(String pluginName, String identifier, SimpleFieldSet fields, Bucket data) throws PluginException { - getPluginTalker(pluginName, identifier).send(fields, data); - } - - // - // PRIVATE METHODS - // - - /** - * Returns the plugin talker for the given plugin connection. - * - * @param pluginName - * The name of the plugin - * @param identifier - * The identifier of the connection - * @return The plugin talker - * @throws PluginException - * if the plugin can not be found - */ - private PluginTalkerFacade getPluginTalker(String pluginName, String identifier) throws PluginException { - try { - return pluginRespiratorFacade.getPluginTalker(this, pluginName, identifier); - } catch (PluginNotFoundException pnfe1) { - throw new PluginException(pnfe1); - } - } - - // - // INTERFACE FredPluginTalker - // - - /** - * {@inheritDoc} - */ - @Override - public void onReply(String pluginName, String identifier, SimpleFieldSet params, Bucket data) { - eventBus.post(new ReceivedReplyEvent(this, pluginName, identifier, params, data)); - } - -} diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/PluginConnector.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/PluginConnector.kt new file mode 100644 index 0000000..fa7c651 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/PluginConnector.kt @@ -0,0 +1,52 @@ +/* + * Sone - PluginConnector.kt - 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.freenet.plugin + +import com.google.common.eventbus.* +import com.google.inject.* +import freenet.pluginmanager.* +import freenet.support.* +import freenet.support.api.* +import net.pterodactylus.sone.freenet.plugin.event.* + +/** + * Interface for talking to other plugins. Other plugins are identified by their + * name and a unique connection identifier. + */ +@Singleton +class PluginConnector @Inject constructor( + private val eventBus: EventBus, + private val pluginRespiratorFacade: PluginRespiratorFacade +) : FredPluginTalker { + + @Throws(PluginException::class) + @JvmOverloads + fun sendRequest(pluginName: String, identifier: String, fields: SimpleFieldSet, data: Bucket? = null) = + getPluginTalker(pluginName, identifier).send(fields, data) + + private fun getPluginTalker(pluginName: String, identifier: String) = + try { + pluginRespiratorFacade.getPluginTalker(this, pluginName, identifier) + } catch (pnfe1: PluginNotFoundException) { + throw PluginException(pnfe1) + } + + override fun onReply(pluginName: String, identifier: String, params: SimpleFieldSet, data: Bucket) = + eventBus.post(ReceivedReplyEvent(this, pluginName, identifier, params, data)) + +} -- 2.7.4