]> git.corax.cc Git - toolbox/commitdiff
include/ipc: Allow passing multiple topics to ipc_endpoint_subscribe()
authorMatthias Kruk <m@m10k.eu>
Sat, 15 Apr 2023 07:05:21 +0000 (16:05 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 15 Apr 2023 07:05:21 +0000 (16:05 +0900)
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).

docs/ipc.md
include/ipc.sh

index 6c6796c98445f128c4c717815a3a54a9ea9b9570..b49baefb51a36981ee805adfb4508e2294c78673 100644 (file)
@@ -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
 
index 62f8dfe7c54c11d56c8ac065c64ebf05700fcb07..194f48cde3caf20d7143cf65695f9ba3247a58a9 100644 (file)
@@ -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() {