* Interface for talking to other plugins. Other plugins are identified by their
* name and a unique connection identifier.
*/
-@Singleton
-class PluginConnector @Inject constructor(
+interface PluginConnector {
+
+ /**
+ * Sends a message to another plugin running in the same node.
+ *
+ * @param pluginName The fully qualified name of the plugin
+ * @param identifier The unique identifier of the request
+ * @param fields The message being sent
+ * @param data Optional data
+ */
+ @Throws(PluginException::class)
+ fun sendRequest(pluginName: String, identifier: String, fields: SimpleFieldSet, data: Bucket? = null): Unit
+
+}
+
+/**
+ * Fred-based [PluginConnector] implementation.
+ */
+class FredPluginConnector @Inject constructor(
private val eventBus: EventBus,
private val pluginRespiratorFacade: PluginRespiratorFacade
-) : FredPluginTalker {
+) : PluginConnector, FredPluginTalker {
- @Throws(PluginException::class)
- @JvmOverloads
- fun sendRequest(pluginName: String, identifier: String, fields: SimpleFieldSet, data: Bucket? = null) =
+ override fun sendRequest(pluginName: String, identifier: String, fields: SimpleFieldSet, data: Bucket?) =
getPluginTalker(pluginName, identifier).send(fields, data)
private fun getPluginTalker(pluginName: String, identifier: String) =
override fun configure(binder: Binder): Unit = binder.run {
bind(PluginRespiratorFacade::class.java).toProvider(Provider { FredPluginRespiratorFacade(pluginRespirator) }).`in`(Singleton::class.java)
+ bind(PluginConnector::class.java).to(FredPluginConnector::class.java).`in`(Singleton::class.java)
bind(Node::class.java).toProvider(Provider { pluginRespirator.node })
bind(HighLevelSimpleClient::class.java).toProvider(Provider<HighLevelSimpleClient> { pluginRespirator.hlSimpleClient!! })
bind(ToadletContainer::class.java).toProvider(Provider<ToadletContainer> { pluginRespirator.toadletContainer })
--- /dev/null
+/* EventBus and Subscribe are marked @Beta, ignore that. And Fred stuff is
+ * often marked as deprecated even though there is no replacement. */
+@file:Suppress("UnstableApiUsage", "DEPRECATION")
+
+package net.pterodactylus.sone.freenet.plugin
+
+import com.google.common.eventbus.*
+import freenet.pluginmanager.*
+import freenet.support.*
+import freenet.support.api.*
+import freenet.support.io.*
+import net.pterodactylus.sone.freenet.*
+import net.pterodactylus.sone.freenet.plugin.event.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import org.junit.*
+import org.junit.rules.*
+
+/**
+ * Unit test for [PluginConnector].
+ */
+class PluginConnectorTest {
+
+ @Rule
+ @JvmField
+ val expectedException = ExpectedException.none()!!
+
+ private val eventBus = EventBus()
+ private val pluginRespirator = object : PluginRespiratorFacade {
+ val call1Parameters = mutableListOf<Call1Parameters>()
+ val call2Parameters = mutableListOf<Call2Parameters>()
+ override fun getPluginTalker(pluginTalker: FredPluginTalker, pluginName: String, identifier: String) =
+ if ("wrong" in pluginName) {
+ throw PluginNotFoundException()
+ } else {
+ object : PluginTalkerFacade {
+ override fun send(pluginParameters: SimpleFieldSet, data: Bucket?) = Unit
+ .also { call2Parameters += Call2Parameters(pluginParameters, data) }
+ }.also { call1Parameters += Call1Parameters(pluginTalker, pluginName, identifier) }
+ }
+ }
+ private val pluginConnector = FredPluginConnector(eventBus, pluginRespirator)
+
+ @Test
+ fun `sending request calls correct method on plugin respirator`() {
+ pluginConnector.sendRequest("test.plugin", "test-request-1", fields)
+ assertThat(pluginRespirator.call1Parameters, hasSize(1))
+ assertThat(pluginRespirator.call1Parameters[0].pluginTalker, sameInstance<FredPluginTalker>(pluginConnector))
+ assertThat(pluginRespirator.call1Parameters[0].pluginName, equalTo("test.plugin"))
+ assertThat(pluginRespirator.call1Parameters[0].identifier, equalTo("test-request-1"))
+ }
+
+ @Test
+ fun `sending request with bucket calls correct method on plugin respirator`() {
+ pluginConnector.sendRequest("test.plugin", "test-request-1", fields, data)
+ assertThat(pluginRespirator.call1Parameters, hasSize(1))
+ assertThat(pluginRespirator.call1Parameters[0].pluginTalker, sameInstance<FredPluginTalker>(pluginConnector))
+ assertThat(pluginRespirator.call1Parameters[0].pluginName, equalTo("test.plugin"))
+ assertThat(pluginRespirator.call1Parameters[0].identifier, equalTo("test-request-1"))
+ }
+
+ @Test
+ fun `sending request to incorrect plugin translates exception correctly`() {
+ expectedException.expect(PluginException::class.java)
+ pluginConnector.sendRequest("wrong.plugin", "test-request-1", fields)
+ }
+
+ @Test
+ fun `sending request with bucket to incorrect plugin translates exception correctly`() {
+ expectedException.expect(PluginException::class.java)
+ pluginConnector.sendRequest("wrong.plugin", "test-request-1", fields, data)
+ }
+
+ @Test
+ fun `sending request calls correct method on plugin talker`() {
+ pluginConnector.sendRequest("test.plugin", "test-request-1", fields)
+ assertThat(pluginRespirator.call2Parameters, hasSize(1))
+ assertThat(pluginRespirator.call2Parameters[0].pluginParameters, equalTo(fields))
+ assertThat(pluginRespirator.call2Parameters[0].data, nullValue())
+ }
+
+ @Test
+ fun `sending request with bucket calls correct method on plugin talker`() {
+ pluginConnector.sendRequest("test.plugin", "test-request-1", fields, data)
+ assertThat(pluginRespirator.call2Parameters, hasSize(1))
+ assertThat(pluginRespirator.call2Parameters[0].pluginParameters, equalTo(fields))
+ assertThat(pluginRespirator.call2Parameters[0].data, equalTo<Bucket?>(data))
+ }
+
+ @Test
+ fun `reply is sent to event bus correctly`() {
+ val listener = object {
+ val receivedReplyEvents = mutableListOf<ReceivedReplyEvent>()
+ @Subscribe
+ fun onReply(receivedReplyEvent: ReceivedReplyEvent) = Unit.also { receivedReplyEvents += receivedReplyEvent }
+ }
+ eventBus.register(listener)
+ pluginConnector.onReply("test.plugin", "test-request-1", fields, data)
+ assertThat(listener.receivedReplyEvents, hasSize(1))
+ assertThat(listener.receivedReplyEvents[0].pluginName(), equalTo("test.plugin"))
+ assertThat(listener.receivedReplyEvents[0].identifier(), equalTo("test-request-1"))
+ assertThat(listener.receivedReplyEvents[0].fieldSet(), equalTo(fields))
+ assertThat(listener.receivedReplyEvents[0].data(), equalTo<Bucket?>(data))
+ }
+
+}
+
+private val fields = SimpleFieldSetBuilder().put("foo", "bar").get()
+private val data = ArrayBucket(byteArrayOf(1, 2))
+
+private data class Call1Parameters(val pluginTalker: FredPluginTalker, val pluginName: String, val identifier: String)
+private data class Call2Parameters(val pluginParameters: SimpleFieldSet, val data: Bucket?)
+++ /dev/null
-/* EventBus and Subscribe are marked @Beta, ignore that. And Fred stuff is
- * often marked as deprecated even though there is no replacement. */
-@file:Suppress("UnstableApiUsage", "DEPRECATION")
-
-package net.pterodactylus.sone.freenet.plugin
-
-import com.google.common.eventbus.*
-import freenet.pluginmanager.*
-import freenet.support.*
-import freenet.support.api.*
-import freenet.support.io.*
-import net.pterodactylus.sone.freenet.*
-import net.pterodactylus.sone.freenet.plugin.event.*
-import org.hamcrest.MatcherAssert.*
-import org.hamcrest.Matchers.*
-import org.junit.*
-import org.junit.rules.*
-
-/**
- * Unit test for [PluginConnector].
- */
-class PluginConnectorTest {
-
- @Rule
- @JvmField
- val expectedException = ExpectedException.none()!!
-
- private val eventBus = EventBus()
- private val pluginRespirator = object : PluginRespiratorFacade {
- val call1Parameters = mutableListOf<Call1Parameters>()
- val call2Parameters = mutableListOf<Call2Parameters>()
- override fun getPluginTalker(pluginTalker: FredPluginTalker, pluginName: String, identifier: String) =
- if ("wrong" in pluginName) {
- throw PluginNotFoundException()
- } else {
- object : PluginTalkerFacade {
- override fun send(pluginParameters: SimpleFieldSet, data: Bucket?) = Unit
- .also { call2Parameters += Call2Parameters(pluginParameters, data) }
- }.also { call1Parameters += Call1Parameters(pluginTalker, pluginName, identifier) }
- }
- }
- private val pluginConnector = PluginConnector(eventBus, pluginRespirator)
-
- @Test
- fun `sending request calls correct method on plugin respirator`() {
- pluginConnector.sendRequest("test.plugin", "test-request-1", fields)
- assertThat(pluginRespirator.call1Parameters, hasSize(1))
- assertThat(pluginRespirator.call1Parameters[0].pluginTalker, sameInstance<FredPluginTalker>(pluginConnector))
- assertThat(pluginRespirator.call1Parameters[0].pluginName, equalTo("test.plugin"))
- assertThat(pluginRespirator.call1Parameters[0].identifier, equalTo("test-request-1"))
- }
-
- @Test
- fun `sending request with bucket calls correct method on plugin respirator`() {
- pluginConnector.sendRequest("test.plugin", "test-request-1", fields, data)
- assertThat(pluginRespirator.call1Parameters, hasSize(1))
- assertThat(pluginRespirator.call1Parameters[0].pluginTalker, sameInstance<FredPluginTalker>(pluginConnector))
- assertThat(pluginRespirator.call1Parameters[0].pluginName, equalTo("test.plugin"))
- assertThat(pluginRespirator.call1Parameters[0].identifier, equalTo("test-request-1"))
- }
-
- @Test
- fun `sending request to incorrect plugin translates exception correctly`() {
- expectedException.expect(PluginException::class.java)
- pluginConnector.sendRequest("wrong.plugin", "test-request-1", fields)
- }
-
- @Test
- fun `sending request with bucket to incorrect plugin translates exception correctly`() {
- expectedException.expect(PluginException::class.java)
- pluginConnector.sendRequest("wrong.plugin", "test-request-1", fields, data)
- }
-
- @Test
- fun `sending request calls correct method on plugin talker`() {
- pluginConnector.sendRequest("test.plugin", "test-request-1", fields)
- assertThat(pluginRespirator.call2Parameters, hasSize(1))
- assertThat(pluginRespirator.call2Parameters[0].pluginParameters, equalTo(fields))
- assertThat(pluginRespirator.call2Parameters[0].data, nullValue())
- }
-
- @Test
- fun `sending request with bucket calls correct method on plugin talker`() {
- pluginConnector.sendRequest("test.plugin", "test-request-1", fields, data)
- assertThat(pluginRespirator.call2Parameters, hasSize(1))
- assertThat(pluginRespirator.call2Parameters[0].pluginParameters, equalTo(fields))
- assertThat(pluginRespirator.call2Parameters[0].data, equalTo<Bucket?>(data))
- }
-
- @Test
- fun `reply is sent to event bus correctly`() {
- val listener = object {
- val receivedReplyEvents = mutableListOf<ReceivedReplyEvent>()
- @Subscribe
- fun onReply(receivedReplyEvent: ReceivedReplyEvent) = Unit.also { receivedReplyEvents += receivedReplyEvent }
- }
- eventBus.register(listener)
- pluginConnector.onReply("test.plugin", "test-request-1", fields, data)
- assertThat(listener.receivedReplyEvents, hasSize(1))
- assertThat(listener.receivedReplyEvents[0].pluginName(), equalTo("test.plugin"))
- assertThat(listener.receivedReplyEvents[0].identifier(), equalTo("test-request-1"))
- assertThat(listener.receivedReplyEvents[0].fieldSet(), equalTo(fields))
- assertThat(listener.receivedReplyEvents[0].data(), equalTo<Bucket?>(data))
- }
-
-}
-
-private val fields = SimpleFieldSetBuilder().put("foo", "bar").get()
-private val data = ArrayBucket(byteArrayOf(1, 2))
-
-private data class Call1Parameters(val pluginTalker: FredPluginTalker, val pluginName: String, val identifier: String)
-private data class Call2Parameters(val pluginParameters: SimpleFieldSet, val data: Bucket?)
verifySingletonInstance<PluginRespiratorFacade>()
}
+ @Test
+ fun `plugin connector is returned correctly`() {
+ assertThat(injector.getInstance<PluginConnector>(), notNullValue())
+ }
+
+ @Test
+ fun `plugin connector facade is returned as singleton`() {
+ verifySingletonInstance<PluginConnector>()
+ }
+
}
createInjector(
SoneModule(sonePlugin, EventBus()),
FreenetInterface::class.isProvidedByDeepMock(),
- PluginRespiratorFacade::class.isProvidedByDeepMock()
+ PluginRespiratorFacade::class.isProvidedByDeepMock(),
+ PluginConnector::class.isProvidedByDeepMock()
)
}
val injector = createInjector(
SoneModule(sonePlugin, eventBus),
FreenetInterface::class.isProvidedByDeepMock(),
- PluginRespiratorFacade::class.isProvidedByDeepMock()
+ PluginRespiratorFacade::class.isProvidedByDeepMock(),
+ PluginConnector::class.isProvidedByDeepMock()
)
val core = injector.getInstance<Core>()
verify(eventBus).register(core)