From: David ‘Bombe’ Roden Date: Mon, 4 Nov 2019 20:04:42 +0000 (+0100) Subject: ♻️ Make plugin connector fully suspendable X-Git-Tag: v81^2~56 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=9b102d5ff09775e3867ad8709f22baf0ad608d30;p=Sone.git ♻️ Make plugin connector fully suspendable --- diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/FredPluginConnector.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/FredPluginConnector.kt index 45afb36..98849c4 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/FredPluginConnector.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/FredPluginConnector.kt @@ -16,7 +16,7 @@ import javax.inject.* */ class FredPluginConnector @Inject constructor(private val pluginRespiratorFacade: PluginRespiratorFacade) : PluginConnector { - override fun sendRequest(pluginName: String, fields: SimpleFieldSet, data: Bucket?): PluginReply { + override suspend fun sendRequest(pluginName: String, fields: SimpleFieldSet, data: Bucket?): PluginReply { val receivedReply = Channel() val responseReceiver = FredPluginTalker { _, _, responseFields, responseData -> GlobalScope.launch { @@ -26,9 +26,7 @@ class FredPluginConnector @Inject constructor(private val pluginRespiratorFacade try { val pluginTalker = pluginRespiratorFacade.getPluginTalker(responseReceiver, pluginName, "") pluginTalker.send(fields, data) - return runBlocking { - receivedReply.receive() - } + return receivedReply.receive() } catch (e: PluginNotFoundException) { throw PluginException(cause = e) } diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/PluginConnector.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/PluginConnector.kt index 86b0378..16c506f 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/PluginConnector.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/plugin/PluginConnector.kt @@ -36,7 +36,7 @@ interface PluginConnector { * @throws PluginException if the plugin identified by [pluginName] does not exist */ @Throws(PluginException::class) - fun sendRequest(pluginName: String, fields: SimpleFieldSet, data: Bucket? = null): PluginReply + suspend fun sendRequest(pluginName: String, fields: SimpleFieldSet, data: Bucket? = null): PluginReply } diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt index 4557074..fae90ae 100644 --- a/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt @@ -19,6 +19,7 @@ package net.pterodactylus.sone.freenet.wot import com.google.inject.* import freenet.support.* +import kotlinx.coroutines.* import net.pterodactylus.sone.freenet.* import net.pterodactylus.sone.freenet.plugin.* import java.lang.String.* @@ -82,10 +83,12 @@ class PluginWebOfTrustConnector @Inject constructor(private val pluginConnector: private fun performRequest(fields: SimpleFieldSet): PluginReply { logger.log(Level.FINE, format("Sending FCP Request: %s", fields.get("Message"))) - return pluginConnector.sendRequest(WOT_PLUGIN_NAME, fields).also { - logger.log(Level.FINEST, format("Received FCP Response for %s: %s", fields.get("Message"), it.fields.get("Message"))) - if ("Error" == it.fields.get("Message")) { - throw PluginException("Could not perform request for " + fields.get("Message")) + return runBlocking { + pluginConnector.sendRequest(WOT_PLUGIN_NAME, fields).also { + logger.log(Level.FINEST, format("Received FCP Response for %s: %s", fields.get("Message"), it.fields.get("Message"))) + if ("Error" == it.fields.get("Message")) { + throw PluginException("Could not perform request for " + fields.get("Message")) + } } } } diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnectorTest.kt index 84acd65..1af177f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnectorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnectorTest.kt @@ -319,7 +319,7 @@ private fun PluginConnector.connect(block: PluginWebOfTrustConnector.() -> R fun createPluginConnector(message: String, fieldsMatcher: Matcher = IsAnything(), build: SimpleFieldSetBuilder.() -> Unit = {}) = object : PluginConnector { - override fun sendRequest(pluginName: String, fields: SimpleFieldSet, data: Bucket?) = + override suspend fun sendRequest(pluginName: String, fields: SimpleFieldSet, data: Bucket?) = if ((pluginName != wotPluginName) || (fields.get("Message") != message)) { throw PluginException() } else {