*/
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 {
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)
}
* @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
}
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.*
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"))
+ }
}
}
}
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 {