♻️ Make plugin connector fully suspendable
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 4 Nov 2019 20:04:42 +0000 (21:04 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 4 Nov 2019 20:04:42 +0000 (21:04 +0100)
src/main/kotlin/net/pterodactylus/sone/freenet/plugin/FredPluginConnector.kt
src/main/kotlin/net/pterodactylus/sone/freenet/plugin/PluginConnector.kt
src/main/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnector.kt
src/test/kotlin/net/pterodactylus/sone/freenet/wot/PluginWebOfTrustConnectorTest.kt

index 45afb36..98849c4 100644 (file)
@@ -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<PluginReply>()
                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)
                }
index 86b0378..16c506f 100644 (file)
@@ -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
 
 }
 
index 4557074..fae90ae 100644 (file)
@@ -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"))
+                               }
                        }
                }
        }
index 84acd65..1af177f 100644 (file)
@@ -319,7 +319,7 @@ private fun <R> PluginConnector.connect(block: PluginWebOfTrustConnector.() -> R
 
 fun createPluginConnector(message: String, fieldsMatcher: Matcher<SimpleFieldSet> = IsAnything<SimpleFieldSet>(), 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 {