Skip to content

Commit

Permalink
fix ambiguity. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
portlek committed Jun 14, 2024
1 parent 024af46 commit f0667f0
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 3 deletions.
9 changes: 9 additions & 0 deletions common/src/main/java/net/infumia/pubsub/Broker.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public interface Broker extends AutoCloseable {
*/
void send(Object message, Target... targets);

/**
* Sends a message globally.
* <p>
* 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> AutoCloseable listen(final Handler<T> handler) {
return this.listen(handler.type(), handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ interface BrokerCoroutines : AutoCloseable {
*/
suspend fun send(message: Any, vararg targets: Pair<String, String>)

/**
* 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.
*
Expand Down Expand Up @@ -106,6 +113,21 @@ interface BrokerCoroutines : AutoCloseable {
vararg targets: Pair<String, String>
): 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 <R : Any> request(
message: Any,
responseType: KClass<R>,
timeout: Duration
): R

/**
* Sends a request and awaits a response using the default timeout.
*
Expand Down Expand Up @@ -139,6 +161,16 @@ interface BrokerCoroutines : AutoCloseable {
*/
suspend fun <R : Any> request(message: Any, responseType: KClass<R>, vararg targets: Pair<String, String>): 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 <R : Any> request(message: Any, responseType: KClass<R>): R

/**
* Registers a responder to handle incoming messages of a specific type and produce a response.
*
Expand Down Expand Up @@ -192,6 +224,34 @@ suspend inline fun <reified R : Any> 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 <reified R : Any> BrokerCoroutines.request(
message: Any,
timeout: Duration,
vararg targets: Pair<String, String>,
): 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 <reified R : Any> BrokerCoroutines.request(
message: Any,
timeout: Duration
): R = request(message, R::class, timeout)

/**
* Sends a request and awaits a response using the default timeout.
*
Expand All @@ -218,6 +278,17 @@ suspend inline fun <reified R : Any> BrokerCoroutines.request(
vararg targets: Pair<String, String>
): 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 <reified R : Any> BrokerCoroutines.request(
message: Any
): R = request(message, R::class)

/**
* Registers a function to respond to incoming messages of a specific type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T : Any> listen(handler: HandlerCoroutines<T>): AutoCloseable =
this.delegate.listen(handler.type.java) { scope.launch { handler(it) } }

Expand Down Expand Up @@ -60,6 +64,13 @@ internal class BrokerCoroutinesImpl(
targets.map { Target.of(it.first, it.second) }
).await()

override suspend fun <R : Any> request(message: Any, responseType: KClass<R>, timeout: Duration): R =
this.delegate.request(
message,
responseType.java,
timeout.toJavaDuration()
).await()

override suspend fun <R : Any> request(message: Any, responseType: KClass<R>, targets: Collection<Target>): R =
this.delegate.request(message, responseType.java, Internal.REQUEST_TIMEOUT, targets).await()

Expand All @@ -78,6 +89,13 @@ internal class BrokerCoroutinesImpl(
targets.map { Target.of(it.first, it.second) }
).await()

override suspend fun <R : Any> request(message: Any, responseType: KClass<R>): R =
this.delegate.request(
message,
responseType.java,
Internal.REQUEST_TIMEOUT
).await()

override suspend fun <T : Any, Y: Any> respond(responder: ResponderCoroutines<T, Y>): AutoCloseable =
this.delegate.respond(responder.type.java) { scope.launch { responder(it) } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package net.infumia.pubsub

import java.util.concurrent.CompletableFuture


/**
* Sends a message to the specified targets.
*
Expand All @@ -24,7 +23,7 @@ inline fun <reified T : Any> 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.
Expand All @@ -35,7 +34,7 @@ inline fun <reified R : Any> 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.
Expand All @@ -45,6 +44,16 @@ inline fun <reified R : Any> Broker.request(message: Any, vararg targets: Target
inline fun <reified R : Any> Broker.request(message: Any, vararg targets: Pair<String, String>): CompletableFuture<R> =
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 <reified R : Any> Broker.request(message: Any): CompletableFuture<R> =
this.request(message, R::class.java)

/**
* Registers a function to respond to messages of a specific type.
*
Expand Down

0 comments on commit f0667f0

Please sign in to comment.