From: Matthias Kruk Date: Sat, 15 Apr 2023 07:05:21 +0000 (+0900) Subject: include/ipc: Allow passing multiple topics to ipc_endpoint_subscribe() X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=468661251c083e296591b9f16047df36ea84f060;p=toolbox include/ipc: Allow passing multiple topics to ipc_endpoint_subscribe() The ipc_endpoint_subscribe() function can only be used to subscribe to one topic at a time, making error checking in scripts that subscribe to more than one topic needlessly complicated. This commit modifies the `ipc_endpoint_subscribe()' function so that the caller may pass more than one topic. If any of the topics could not be subscribed, the function will leave the endpoint in the state it had before the invocation (that is, undo subscriptions that were made by the same call). --- diff --git a/docs/ipc.md b/docs/ipc.md index 6c6796c..b49baef 100644 --- a/docs/ipc.md +++ b/docs/ipc.md @@ -26,7 +26,7 @@ sent by one module cannot be read by the other. | [ipc_endpoint_publish()](#ipc_endpoint_publish) | Publish a message to a topic | | [ipc_endpoint_recv()](#ipc_endpoint_recv) | Receive a point-to-point or pub-sub message | | [ipc_endpoint_send()](#ipc_endpoint_send) | Send a point-to-point message to another endpoint | -| [ipc_endpoint_subscribe()](#ipc_endpoint_subscribe) | Subscribe an endpoint to a topic | +| [ipc_endpoint_subscribe()](#ipc_endpoint_subscribe) | Subscribe an endpoint to one or more topics | | [ipc_endpoint_unsubscribe()](#ipc_endpoint_unsubscribe) | Unsubscribe an endpoint from one or more topics | | [ipc_get_root()](#ipc_get_root) | Return the path to the IPC directory | | [ipc_msg_dump()](#ipc_msg_dump) | Dump the contents of an IPC message to standard output | @@ -334,23 +334,26 @@ In case of an error, an error message will be written to standard error. ## ipc_endpoint_subscribe() -Subscribe an endpoint to a topic +Subscribe an endpoint to one or more topics ### Synopsis - ipc_endpoint_subscribe "$endpoint" "$topic" + ipc_endpoint_subscribe "$endpoint" "${topics[@]}" ### Description -The `ipc_endpoint_subscribe()` function subscribes the endpoint referenced by `endpoint` to the topic -`topic`. If the call succeeded, the endpoint will receive any messages published on the topic `topic`. +The `ipc_endpoint_subscribe()` function subscribes the endpoint referenced by `endpoint` to +the topics listed in `topics`. If the call succeeded, the endpoint will receive any messages +published on any of the topics that were passed to this function. If any of the subscriptions +failed, successful subscriptions will be undone, restoring the state the endpoint had before +this function was invoked. ### Return value -| Return value | Meaning | -|--------------|---------------------------------------------------| -| 0 | Success | -| 1 | The endpoint could not be subscribed to the topic | +| Return value | Meaning | +|--------------|------------------------------------------------------------| +| 0 | All topics were successfully subscribed | +| 1 | The endpoint could not be subscribed to some of the topics | ### Standard input diff --git a/include/ipc.sh b/include/ipc.sh index 62f8dfe..194f48c 100644 --- a/include/ipc.sh +++ b/include/ipc.sh @@ -692,17 +692,29 @@ _ipc_endpoint_topic_get_subscribers_and_taps() { ipc_endpoint_subscribe() { local endpoint="$1" - local topic="$2" + local topics="$2" - if ! _ipc_endpoint_topic_create "$topic"; then - return 1 - fi + local topic + local -a succeeded + local -i error - if ! _ipc_endpoint_topic_subscribe "$endpoint" "$topic"; then - return 1 + succeeded=() + error=0 + + for topic in "${topics[@]}"; do + if ! _ipc_endpoint_topic_create "$topic" || + ! _ipc_endpoint_topic_subscribe "$endpoint" "$topic"; then + error=1 + break + fi + succeeded+=("$topic") + done + + if (( error == 1 )); then + ipc_endpoint_unsubscribe "$endpoint" "${succeeded[@]}" fi - return 0 + return "$error" } ipc_endpoint_unsubscribe() {