From c70a0a93374eae11f20b90aec539b0c190eb6faa Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 17 Sep 2022 14:22:07 +0900 Subject: [PATCH] include/ipc: Add "topic" field to pubsub IPC messages 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 | 26 +++++++++++++++++++++++--- spec/ipc_message.schema.json | 5 +++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/include/ipc.sh b/include/ipc.sh index 46605ed..79b5bd0 100644 --- a/include/ipc.sh +++ b/include/ipc.sh @@ -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 diff --git a/spec/ipc_message.schema.json b/spec/ipc_message.schema.json index 06379d6..1ee8f2c 100644 --- a/spec/ipc_message.schema.json +++ b/spec/ipc_message.schema.json @@ -21,6 +21,11 @@ "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" -- 2.47.3