]> git.corax.cc Git - toolbox/commitdiff
include/ipc: Add functions to ipc interface so it can be extended
authorMatthias Kruk <m@m10k.eu>
Mon, 9 Jan 2023 09:17:10 +0000 (18:17 +0900)
committerMatthias Kruk <m@m10k.eu>
Mon, 9 Jan 2023 09:17:10 +0000 (18:17 +0900)
The ipc module provides an interface, but it cannot meaningfully
be extended because the interface is missing functions that need to
be overridden by an extending module.

This commit makes several message-related functions public and adds
them to the interface that is provided by the ipc module. This
further adds the `ipc_get_root()' function, which replaces the
`__ipc_root' variable, allowing modules that extend the interface to
use a different root directory.

include/ipc.sh
test/ipc_spec.sh

index b930956293be269f641b15583cc89ec878cf89dd..a980d72b9dae94924dffa22a62d49ec7dfc5cf7a 100644 (file)
@@ -22,15 +22,36 @@ __init() {
        fi
 
        declare -gxr  __ipc_root="/var/lib/toolbox/ipc"
-       declare -gxr  __ipc_pubsub_root="$__ipc_root/pubsub"
-
        declare -gxir __ipc_version=1
 
-       interface "encode"                   \
-                 "decode"
+       interface "get_root"                 \
+                 "msg_new"                  \
+                 "msg_get"                  \
+                 "msg_dump"                 \
+                 "msg_get_version"          \
+                 "msg_get_source"           \
+                 "msg_get_destination"      \
+                 "msg_get_user"             \
+                 "msg_get_timestamp"        \
+                 "msg_get_data"             \
+                 "msg_get_topic"            \
+                 "encode"                   \
+                 "decode"                   \
+                 "endpoint_open"            \
+                 "endpoint_close"           \
+                 "endpoint_send"            \
+                 "endpoint_recv"            \
+                 "endpoint_subscribe"       \
+                 "endpoint_publish"         \
+                 "endpoint_foreach_message" \
+
        return 0
 }
 
+ipc_get_root() {
+       echo "$__ipc_root"
+}
+
 ipc_encode() {
        local decoded="$1"
 
@@ -97,7 +118,7 @@ _ipc_get() {
        return 0
 }
 
-_ipc_msg_get() {
+ipc_msg_get() {
        local envelope="$1"
        local field="$2"
 
@@ -240,7 +261,7 @@ ipc_msg_dump() {
        local signature_ok
 
        msg=$(_ipc_get "$envelope" "message")
-       version=$(_ipc_msg_get "$envelope" "version")
+       version=$(ipc_msg_get "$envelope" "version")
        signer_name=$(ipc_msg_get_signer_name "$envelope")
        signer_email=$(ipc_msg_get_signer_email "$envelope")
        signer_key=$(ipc_msg_get_signer_key "$envelope")
@@ -268,7 +289,7 @@ EOF
        return 0
 }
 
-_ipc_msg_new() {
+ipc_msg_new() {
        local source="$1"
        local destination="$2"
        local data="$3"
@@ -327,7 +348,7 @@ ipc_msg_get_version() {
 
        local version
 
-       if ! version=$(_ipc_msg_get "$msg" "version"); then
+       if ! version=$(ipc_msg_get "$msg" "version"); then
                return 1
        fi
 
@@ -340,7 +361,7 @@ ipc_msg_get_source() {
 
        local src
 
-       if ! src=$(_ipc_msg_get "$msg" "source"); then
+       if ! src=$(ipc_msg_get "$msg" "source"); then
                return 1
        fi
 
@@ -353,7 +374,7 @@ ipc_msg_get_destination() {
 
        local dst
 
-       if ! dst=$(_ipc_msg_get "$msg" "destination"); then
+       if ! dst=$(ipc_msg_get "$msg" "destination"); then
                return 1
        fi
 
@@ -366,7 +387,7 @@ ipc_msg_get_user() {
 
        local user
 
-       if ! user=$(_ipc_msg_get "$msg" "user"); then
+       if ! user=$(ipc_msg_get "$msg" "user"); then
                return 1
        fi
 
@@ -379,7 +400,7 @@ ipc_msg_get_timestamp() {
 
        local timestamp
 
-       if ! timestamp=$(_ipc_msg_get "$msg" "timestamp"); then
+       if ! timestamp=$(ipc_msg_get "$msg" "timestamp"); then
                return 1
        fi
 
@@ -393,7 +414,7 @@ ipc_msg_get_data() {
        local data
        local data_raw
 
-       if ! data=$(_ipc_msg_get "$msg" "data"); then
+       if ! data=$(ipc_msg_get "$msg" "data"); then
                return 1
        fi
 
@@ -410,7 +431,7 @@ ipc_msg_get_topic() {
 
        local topic
 
-       if ! topic=$(_ipc_msg_get "$msg" "topic"); then
+       if ! topic=$(ipc_msg_get "$msg" "topic"); then
                return 1
        fi
 
@@ -490,7 +511,7 @@ ipc_endpoint_open() {
                name="priv/$USER.$self.$$.$(date +"%s").$RANDOM"
        fi
 
-       endpoint="$__ipc_root/$name"
+       endpoint="$(ipc_get_root)/$name"
 
        if ! [ -d "$endpoint" ]; then
                if ! mkdir -p "$endpoint/subscriptions"; then
@@ -518,7 +539,7 @@ ipc_endpoint_close() {
        local endpoint
        local subscription
 
-       endpoint="$__ipc_root/$name"
+       endpoint="$(ipc_get_root)/$name"
 
        if ! queue_destroy "$endpoint/queue"; then
                return 1
@@ -543,7 +564,7 @@ _ipc_endpoint_put() {
 
        local queue
 
-       queue="$__ipc_root/$endpoint/queue"
+       queue="$(ipc_get_root)/$endpoint/queue"
 
        if ! queue_put "$queue" "$msg"; then
                return 1
@@ -560,7 +581,7 @@ ipc_endpoint_send() {
 
        local msg
 
-       if ! msg=$(_ipc_msg_new "$source" "$destination" "$data" "$topic"); then
+       if ! msg=$(ipc_msg_new "$source" "$destination" "$data" "$topic"); then
                return 1
        fi
 
@@ -578,7 +599,7 @@ ipc_endpoint_recv() {
        local queue
        local msg
 
-       queue="$__ipc_root/$endpoint/queue"
+       queue="$(ipc_get_root)/$endpoint/queue"
 
        if ! msg=$(queue_get "$queue" "$timeout"); then
                return 1
@@ -591,7 +612,7 @@ ipc_endpoint_recv() {
 _ipc_endpoint_topic_create() {
        local topic="$1"
 
-       if ! mkdir -p "$__ipc_pubsub_root/$topic"; then
+       if ! mkdir -p "$(ipc_get_root)/pubsub/$topic"; then
                return 1
        fi
 
@@ -605,14 +626,14 @@ _ipc_endpoint_topic_subscribe() {
        local topicdir
        local subscription
 
-       topicdir="$__ipc_pubsub_root/$topic"
+       topicdir="$(ipc_get_root)/pubsub/$topic"
        subscription="$topicdir/${endpoint//\//_}"
 
        if ! ln -sf "$endpoint" "$subscription"; then
                return 1
        fi
 
-       if ! ln -sfn "$topicdir" "$__ipc_root/$endpoint/subscriptions/$topic"; then
+       if ! ln -sfn "$topicdir" "$(ipc_get_root)/$endpoint/subscriptions/$topic"; then
                rm -f "$subscription"
                return 1
        fi
@@ -633,7 +654,7 @@ _ipc_endpoint_topic_get_subscribers() {
                fi
 
                echo "$subscriber"
-       done < <(find "$__ipc_pubsub_root/$topic" -mindepth 1 -maxdepth 1 -type l)
+       done < <(find "$(ipc_get_root)/pubsub/$topic" -mindepth 1 -maxdepth 1 -type l)
 
        return 0
 }
@@ -688,7 +709,7 @@ ipc_endpoint_foreach_message() {
 
        local queue
 
-       queue="$__ipc_root/$endpoint/queue"
+       queue="$(ipc_get_root)/$endpoint/queue"
 
        if ! queue_foreach "$queue" _ipc_endpoint_foreach_message_helper \
                           "$endpoint" "$func" "${args[@]}"; then
index 2ed19114fb19c914d92c26da3ad204229f934379..4381cdf0188e47079af06f5507e7bb555ceb60de 100644 (file)
@@ -66,6 +66,13 @@ cleanup() {
        rm -rf "/tmp/test.$$"
 }
 
+Describe "ipc_get_root()"
+  It "returns /var/lib/toolbox/ipc"
+    When call ipc_get_root
+    The stdout should equal "/var/lib/toolbox/ipc"
+  End
+End
+
 Describe "Encoding"
   It "ipc_encode() outputs base64"
     _test_encoding() {
@@ -237,11 +244,11 @@ Describe "Message"
   BeforeAll 'setup'
   AfterAll 'cleanup'
 
-  It "_ipc_msg_new() outputs base64 encoded data"
+  It "ipc_msg_new() outputs base64 encoded data"
     _test_ipc_msg_new_is_base64() {
            local msg
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi
 
@@ -256,11 +263,11 @@ Describe "Message"
     The status should equal 0
   End
 
-  It "_ipc_msg_new() outputs an encoded JSON object"
+  It "ipc_msg_new() outputs an encoded JSON object"
     _test_ipc_msg_new_is_json() {
            local msg
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi
 
@@ -277,11 +284,11 @@ Describe "Message"
     The stderr should not start with "parse error"
   End
 
-  It "_ipc_msg_new() generates valid toolbox.ipc.envelope objects"
+  It "ipc_msg_new() generates valid toolbox.ipc.envelope objects"
     _test_ipc_msg_new_json_schema_envelope() {
            local msg
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi
 
@@ -296,11 +303,11 @@ Describe "Message"
     The status should equal 0
   End
 
-  It "_ipc_msg_new() messages contain valid toolbox.ipc.message objects"
+  It "ipc_msg_new() messages contain valid toolbox.ipc.message objects"
     _test_ipc_msg_new_json_schema_message() {
            local msg
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi
 
@@ -316,11 +323,11 @@ Describe "Message"
     The status should equal 0
   End
 
-  It "_ipc_msg_new()/ipc_msg_get_version() sets/gets the correct version"
+  It "ipc_msg_new()/ipc_msg_get_version() sets/gets the correct version"
     _test_ipc_msg_new_version() {
            local msg
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi
 
@@ -332,12 +339,12 @@ Describe "Message"
     The stdout should equal "$__ipc_version"
   End
 
-  It "_ipc_msg_new()/ipc_msg_get_user() sets/gets the correct user"
+  It "ipc_msg_new()/ipc_msg_get_user() sets/gets the correct user"
 
     _test_ipc_msg_new_user() {
            local msg
 
-           msg=$(_ipc_msg_new "from" "to" "data")
+           msg=$(ipc_msg_new "from" "to" "data")
 
            ipc_msg_get_user "$msg"
     }
@@ -347,7 +354,7 @@ Describe "Message"
     The stdout should equal "$USER"
   End
 
-  It "_ipc_msg_new()/ipc_msg_get_timestamp() sets/gets the correct timestamp"
+  It "ipc_msg_new()/ipc_msg_get_timestamp() sets/gets the correct timestamp"
     _test_ipc_msg_new_timestamp() {
            local before
            local after
@@ -355,7 +362,7 @@ Describe "Message"
            local timestamp
 
            before=$(date +"%s")
-           msg=$(_ipc_msg_new "from" "to" "data")
+           msg=$(ipc_msg_new "from" "to" "data")
            after=$(date +"%s")
 
            timestamp=$(ipc_msg_get_timestamp "$msg")
@@ -375,11 +382,11 @@ Describe "Message"
     The status should equal 0
   End
 
-  It "_ipc_msg_new()/ipc_msg_get_source() sets/gets the correct source"
+  It "ipc_msg_new()/ipc_msg_get_source() sets/gets the correct source"
     _test_ipc_msg_new_source() {
            local msg
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi
 
@@ -391,11 +398,11 @@ Describe "Message"
     The stdout should equal "from"
   End
 
-  It "_ipc_msg_new()/ipc_msg_get_destination() sets/gets the correct destination"
+  It "ipc_msg_new()/ipc_msg_get_destination() sets/gets the correct destination"
     _test_ipc_msg_new_destination() {
            local msg
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi
 
@@ -407,11 +414,11 @@ Describe "Message"
     The stdout should equal "to"
   End
 
-  It "_ipc_msg_new()/ipc_msg_get_data() sets/gets the correct data"
+  It "ipc_msg_new()/ipc_msg_get_data() sets/gets the correct data"
     _test_ipc_msg_new_data() {
            local msg
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi
 
@@ -427,7 +434,7 @@ Describe "Message"
     _test_ipc_msg_get_signer_name() {
            local msg
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi
 
@@ -443,7 +450,7 @@ Describe "Message"
     _test_ipc_msg_get_signer_email() {
            local msg
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi
 
@@ -460,7 +467,7 @@ Describe "Message"
            local msg
            local key
 
-           if ! msg=$(_ipc_msg_new "from" "to" "data"); then
+           if ! msg=$(ipc_msg_new "from" "to" "data"); then
                    return 1
            fi