From f0667f02acbd3f68ea2e684cb31753fa3dbf663c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Fri, 14 Jun 2024 20:34:06 +0300 Subject: [PATCH] fix ambiguity. (#19) --- .../main/java/net/infumia/pubsub/Broker.java | 9 +++ .../infumia/pubsub/BrokerStringAbstract.java | 5 ++ .../net/infumia/pubsub/BrokerCoroutines.kt | 71 +++++++++++++++++++ .../infumia/pubsub/BrokerCoroutinesImpl.kt | 18 +++++ .../net/infumia/pubsub/BrokerExtension.kt | 15 +++- 5 files changed, 115 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/net/infumia/pubsub/Broker.java b/common/src/main/java/net/infumia/pubsub/Broker.java index 0a6b9d6..d6bc188 100644 --- a/common/src/main/java/net/infumia/pubsub/Broker.java +++ b/common/src/main/java/net/infumia/pubsub/Broker.java @@ -35,6 +35,15 @@ public interface Broker extends AutoCloseable { */ void send(Object message, Target... targets); + /** + * Sends a message globally. + *

+ * Sends to {@link Target#global()} if {@code targets} is not specified. + * + * @param message the message to send. Cannot be {@code null} + */ + void send(Object message); + /** * Listens for messages using a handler. * diff --git a/common/src/main/java/net/infumia/pubsub/BrokerStringAbstract.java b/common/src/main/java/net/infumia/pubsub/BrokerStringAbstract.java index 3c18947..8a31c18 100644 --- a/common/src/main/java/net/infumia/pubsub/BrokerStringAbstract.java +++ b/common/src/main/java/net/infumia/pubsub/BrokerStringAbstract.java @@ -53,6 +53,11 @@ public void send(final Object message, final Target... targets) { this.send(message, Arrays.asList(targets)); } + @Override + public void send(final Object message) { + this.send(message, Collections.emptySet()); + } + @Override public final AutoCloseable listen(final Handler handler) { return this.listen(handler.type(), handler); diff --git a/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutines.kt b/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutines.kt index 9e8b937..0f816f6 100644 --- a/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutines.kt +++ b/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutines.kt @@ -36,6 +36,13 @@ interface BrokerCoroutines : AutoCloseable { */ suspend fun send(message: Any, vararg targets: Pair) + /** + * Sends a message globally. + * + * @param message the message to send. + */ + suspend fun send(message: Any) + /** * Registers a handler to listen for messages of a specific type. * @@ -106,6 +113,21 @@ interface BrokerCoroutines : AutoCloseable { vararg targets: Pair ): R + /** + * Sends a request and awaits a response within a specified timeout. + * + * @param R the type of the response. + * @param message the request message. + * @param responseType the KClass representing the expected response type. + * @param timeout the duration to wait for a response. + * @return the response to the request. + */ + suspend fun request( + message: Any, + responseType: KClass, + timeout: Duration + ): R + /** * Sends a request and awaits a response using the default timeout. * @@ -139,6 +161,16 @@ interface BrokerCoroutines : AutoCloseable { */ suspend fun request(message: Any, responseType: KClass, vararg targets: Pair): R + /** + * Sends a request and awaits a response using the default timeout. + * + * @param R the type of the response. + * @param message the request message. + * @param responseType the KClass representing the expected response type. + * @return the response to the request. + */ + suspend fun request(message: Any, responseType: KClass): R + /** * Registers a responder to handle incoming messages of a specific type and produce a response. * @@ -192,6 +224,34 @@ suspend inline fun BrokerCoroutines.request( vararg targets: Target, ): R = request(message, R::class, timeout, *targets) +/** + * Sends a request and awaits a response within a specified timeout. + * + * @param R the type of the response. + * @param message the request message. + * @param timeout the duration to wait for a response. + * @param targets the targets to send the request to. + * @return the response to the request. + */ +suspend inline fun BrokerCoroutines.request( + message: Any, + timeout: Duration, + vararg targets: Pair, +): R = request(message, R::class, timeout, *targets) + +/** + * Sends a request and awaits a response within a specified timeout. + * + * @param R the type of the response. + * @param message the request message. + * @param timeout the duration to wait for a response. + * @return the response to the request. + */ +suspend inline fun BrokerCoroutines.request( + message: Any, + timeout: Duration +): R = request(message, R::class, timeout) + /** * Sends a request and awaits a response using the default timeout. * @@ -218,6 +278,17 @@ suspend inline fun BrokerCoroutines.request( vararg targets: Pair ): R = request(message, R::class, *targets) +/** + * Sends a request and awaits a response using the default timeout. + * + * @param R the type of the response. + * @param message the request message. + * @return the response to the request. + */ +suspend inline fun BrokerCoroutines.request( + message: Any +): R = request(message, R::class) + /** * Registers a function to respond to incoming messages of a specific type. * diff --git a/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutinesImpl.kt b/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutinesImpl.kt index f8b3c78..6a3670b 100644 --- a/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutinesImpl.kt +++ b/kotlin/coroutines/src/main/kotlin/net/infumia/pubsub/BrokerCoroutinesImpl.kt @@ -27,6 +27,10 @@ internal class BrokerCoroutinesImpl( this.delegate.send(message, targets.map { Target.of(it.first, it.second) }) } + override suspend fun send(message: Any) { + this.delegate.send(message) + } + override suspend fun listen(handler: HandlerCoroutines): AutoCloseable = this.delegate.listen(handler.type.java) { scope.launch { handler(it) } } @@ -60,6 +64,13 @@ internal class BrokerCoroutinesImpl( targets.map { Target.of(it.first, it.second) } ).await() + override suspend fun request(message: Any, responseType: KClass, timeout: Duration): R = + this.delegate.request( + message, + responseType.java, + timeout.toJavaDuration() + ).await() + override suspend fun request(message: Any, responseType: KClass, targets: Collection): R = this.delegate.request(message, responseType.java, Internal.REQUEST_TIMEOUT, targets).await() @@ -78,6 +89,13 @@ internal class BrokerCoroutinesImpl( targets.map { Target.of(it.first, it.second) } ).await() + override suspend fun request(message: Any, responseType: KClass): R = + this.delegate.request( + message, + responseType.java, + Internal.REQUEST_TIMEOUT + ).await() + override suspend fun respond(responder: ResponderCoroutines): AutoCloseable = this.delegate.respond(responder.type.java) { scope.launch { responder(it) } } diff --git a/kotlin/extensions/src/main/kotlin/net/infumia/pubsub/BrokerExtension.kt b/kotlin/extensions/src/main/kotlin/net/infumia/pubsub/BrokerExtension.kt index d3dafd9..f3f3857 100644 --- a/kotlin/extensions/src/main/kotlin/net/infumia/pubsub/BrokerExtension.kt +++ b/kotlin/extensions/src/main/kotlin/net/infumia/pubsub/BrokerExtension.kt @@ -2,7 +2,6 @@ package net.infumia.pubsub import java.util.concurrent.CompletableFuture - /** * Sends a message to the specified targets. * @@ -24,7 +23,7 @@ inline fun Broker.listen(noinline handler: (T) -> Unit): AutoC this.listen(T::class.java, handler) /** - * Sends a message and expects a response of a specific type. + * Sends a message and expects a specific response. * * @param message the message to send. * @param targets the targets to send the message to. @@ -35,7 +34,7 @@ inline fun Broker.request(message: Any, vararg targets: Target this.request(message, R::class.java, *targets) /** - * Sends a message and expects a response of a specific type. + * Sends a message and expects a specific response. * * @param message the message to send. * @param targets the targets to send the message to. @@ -45,6 +44,16 @@ inline fun Broker.request(message: Any, vararg targets: Target inline fun Broker.request(message: Any, vararg targets: Pair): CompletableFuture = this.request(message, R::class.java, targets.map { Target.of(it.first, it.second) }) +/** + * Sends a message and expects a specific response. + * + * @param message the message to send. + * @param R the type of the expected response. + * @return a [CompletableFuture] representing the response to the message. + */ +inline fun Broker.request(message: Any): CompletableFuture = + this.request(message, R::class.java) + /** * Registers a function to respond to messages of a specific type. *