]> git.corax.cc Git - toolbox/commitdiff
include/ipc: Add "topic" field to pubsub IPC messages
authorMatthias Kruk <m@m10k.eu>
Sat, 17 Sep 2022 05:22:07 +0000 (14:22 +0900)
committerMatthias Kruk <m@m10k.eu>
Sat, 17 Sep 2022 05:38:10 +0000 (14:38 +0900)
Messages that were sent with ipc_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 IPC messages and modifies
ipc_endpoint_publish() so that it sets the field appropriately. This
further adds the function ipc_msg_get_topic() that may be used to
determine the topic an IPC message was published on.

include/ipc.sh
spec/ipc_message.schema.json

index 46605ed0db66b2505f6dc4d2ba068f039ab28ac3..79b5bd0ce1067fc8354149270bcfce515c24f1de 100644 (file)
@@ -1,7 +1,7 @@
 #!/bin/bash
 
 # ipc.sh - Toolbox module for message-based IPC
-# Copyright (C) 2021 Matthias Kruk
+# Copyright (C) 2021-2022 Matthias Kruk
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -279,6 +279,11 @@ _ipc_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
@@ -299,6 +304,7 @@ _ipc_msg_new() {
                                     "destination" "$destination"   \
                                     "user"        "$USER"          \
                                     "timestamp"   "$timestamp"     \
+                                    "topic"       "$topic"         \
                                     "data"        "$encoded_data"); then
                log_error "Could not make message"
 
@@ -406,6 +412,19 @@ ipc_msg_get_data() {
        return 0
 }
 
+ipc_msg_get_topic() {
+       local msg="$1"
+
+       local topic
+
+       if ! topic=$(_ipc_msg_get "$msg" "topic"); then
+               return 1
+       fi
+
+       echo "$topic"
+       return 0
+}
+
 ipc_msg_get_signature() {
        local msg="$1"
 
@@ -560,10 +579,11 @@ ipc_endpoint_send() {
        local source="$1"
        local destination="$2"
        local data="$3"
+       local topic="$4"
 
        local msg
 
-       if ! msg=$(_ipc_msg_new "$source" "$destination" "$data"); then
+       if ! msg=$(_ipc_msg_new "$source" "$destination" "$data" "$topic"); then
                return 1
        fi
 
@@ -706,7 +726,7 @@ ipc_endpoint_publish() {
        fi
 
        while read -r subscriber; do
-               ipc_endpoint_send "$endpoint" "$subscriber" "$message"
+               ipc_endpoint_send "$endpoint" "$subscriber" "$message" "$topic"
        done < <(_ipc_endpoint_topic_get_subscribers "$topic")
 
        return 0
index 06379d699c1d3144d8706d751a0fbd8a46860f49..1ee8f2c83e739a29b282197903aa8f294f01e584 100644 (file)
            "type": "string"
        },
 
+       "topic": {
+           "description": "The topic that the message was published on",
+           "type": "string"
+       },
+
        "timestamp": {
            "description": "The UNIX timestamp when the message was sent",
            "type": "integer"