]> git.corax.cc Git - toolbox/commitdiff
include/uipc: Add "topic" field to pubsub UIPC messages
authorMatthias Kruk <m@m10k.eu>
Sat, 17 Sep 2022 05:40:48 +0000 (14:40 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 17 Sep 2022 05:40:48 +0000 (14:40 +0900)
Messages that were sent with uipc_endpoint_publish() don't contain
information about the topic that they were published on, making it
impossible for the receiver to determine what topic a message was
published on if they are subscribed to more than one topic.

This commit adds a new field "topic" to UIPC messages and modifies
uipc_endpoint_publish() so that it sets the field appropriately. This
further adds the function uipc_msg_get_topic() that may be used to
determine the topic a UIPC message was published on.

include/uipc.sh

index e5e597a947f732c722c03a25920ae0f38b25da37..5cae4d45654056e52da646fc27dfd574c5894268 100644 (file)
@@ -117,6 +117,11 @@ _uipc_msg_new() {
        local source="$1"
        local destination="$2"
        local data="$3"
+       local topic="$4"
+
+       # For non-pubsub messages, the topic will be unset. This will
+       # cause the topic not to show up in the JSON object because
+       # json_object() skips empty fields
 
        local encoded_data
        local timestamp
@@ -134,6 +139,7 @@ _uipc_msg_new() {
                                     "destination" "$destination"   \
                                     "user"        "$USER"          \
                                     "timestamp"   "$timestamp"     \
+                                    "topic"       "$topic"         \
                                     "data"        "$encoded_data"); then
                log_error "Could not make message"
 
@@ -231,6 +237,19 @@ uipc_msg_get_data() {
        return 0
 }
 
+uipc_msg_get_topic() {
+       local msg="$1"
+
+       local topic
+
+       if ! topic=$(_uipc_msg_get "$msg" "topic"); then
+               return 1
+       fi
+
+       echo "$topic"
+       return 0
+}
+
 uipc_endpoint_open() {
        local name="$1"
 
@@ -325,10 +344,11 @@ uipc_endpoint_send() {
        local source="$1"
        local destination="$2"
        local data="$3"
+       local topic="$4"
 
        local msg
 
-       if ! msg=$(_uipc_msg_new "$source" "$destination" "$data"); then
+       if ! msg=$(_uipc_msg_new "$source" "$destination" "$data" "$topic"); then
                return 1
        fi
 
@@ -467,7 +487,7 @@ uipc_endpoint_publish() {
        fi
 
        while read -r subscriber; do
-               uipc_endpoint_send "$endpoint" "$subscriber" "$message"
+               uipc_endpoint_send "$endpoint" "$subscriber" "$message" "$topic"
        done < <(_uipc_endpoint_topic_get_subscribers "$topic")
 
        return 0