]> git.corax.cc Git - foundry/commitdiff
include/msg: Add foundry base message type
authorMatthias Kruk <m@m10k.eu>
Sun, 11 Jul 2021 06:31:53 +0000 (15:31 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 11 Jul 2021 06:31:53 +0000 (15:31 +0900)
Because the current foundry implementation transmits all message types
directly on top of the ipc layer, distinguishing message types is not
trivial.
This commit solves adds a base message type for foundry messages,
including an attribute that contains the message type, making it
making it possible for message types to be easily distinguished.

include/msg.sh [new file with mode: 0644]
spec/base.json [new file with mode: 0644]

diff --git a/include/msg.sh b/include/msg.sh
new file mode 100644 (file)
index 0000000..664a225
--- /dev/null
@@ -0,0 +1,172 @@
+#!/bin/bash
+
+__init() {
+       local submodules
+       local deps
+
+       submodules=("foundry/msg/artifact"
+                   "foundry/msg/buildrequest"
+                   "foundry/msg/build"
+                   "foundry/msg/commit"
+                   "foundry/msg/distrequest"
+                   "foundry/msg/dist"
+                   "foundry/msg/mergerequest"
+                   "foundry/msg/merge"
+                   "foundry/msg/signrequest"
+                   "foundry/msg/sign"
+                   "foundry/msg/testrequest"
+                   "foundry/msg/test")
+
+       deps=("ipc"
+             "json")
+
+       if ! include "${submodules[@]}" "${deps[@]}"; then
+               return 1
+       fi
+
+       declare -gxir __foundry_msg_version=1
+
+       declare -gxir __foundry_msg_type_build=1
+       declare -gxir __foundry_msg_type_buildrequest=2
+       declare -gxir __foundry_msg_type_commit=3
+       declare -gxir __foundry_msg_type_dist=4
+       declare -gxir __foundry_msg_type_distrequest=5
+       declare -gxir __foundry_msg_type_merge=6
+       declare -gxir __foundry_msg_type_mergerequest=7
+       declare -gxir __foundry_msg_type_sign=8
+       declare -gxir __foundry_msg_type_signrequest=9
+       declare -gxir __foundry_msg_type_test=10
+       declare -gxir __foundry_msg_type_testrequest=11
+
+       declare -gxA __foundry_msg_typemap
+
+       __foundry_msg_typemap["build"]="$__foundry_msg_type_build"
+       __foundry_msg_typemap["buildrequest"]="$__foundry_msg_type_buildrequest"
+       __foundry_msg_typemap["commit"]="$__foundry_msg_type_commit"
+       __foundry_msg_typemap["dist"]="$__foundry_msg_type_dist"
+       __foundry_msg_typemap["distrequest"]="$__foundry_msg_type_distrequest"
+       __foundry_msg_typemap["merge"]="$__foundry_msg_type_merge"
+       __foundry_msg_typemap["mergerequest"]="$__foundry_msg_type_mergerequest"
+       __foundry_msg_typemap["sign"]="$__foundry_msg_type_sign"
+       __foundry_msg_typemap["signrequest"]="$__foundry_msg_type_signrequest"
+       __foundry_msg_typemap["test"]="$__foundry_msg_type_test"
+       __foundry_msg_typemap["testrequest"]="$__foundry_msg_type_testrequest"
+
+       return 0
+}
+
+foundry_msg_get_version() {
+       local msg="$1"
+
+       local version
+
+       if ! version=$(json_object_get "$msg" "version"); then
+               return 1
+       fi
+
+       echo "$version"
+       return 0
+}
+
+foundry_msg_get_type() {
+       local msg="$1"
+
+       local type
+
+       if ! type=$(json_object_get "$msg" "type"); then
+               return 1
+       fi
+
+       echo "$type"
+       return 0
+}
+
+foundry_msg_get_data() {
+       local msg="$1"
+
+       local data
+
+       if ! data=$(json_object_get "$msg" "data"); then
+               return 1
+       fi
+
+       echo "$data"
+       return 0
+}
+
+foundry_msg_get_data_field() {
+       local msg="$1"
+       local field="$2"
+
+       local value
+
+       if ! value=$(json_object_get "$msg" "data.$field"); then
+               return 1
+       fi
+
+       echo "$value"
+       return 0
+}
+
+_foundry_msg_version_supported() {
+       local basemsg="$1"
+
+       local version
+
+       if ! version=$(foundry_msg_get_version "$basemsg"); then
+               return 1
+       fi
+
+       if (( version != __foundry_msg_version )); then
+               return 1
+       fi
+
+       return 0
+}
+
+foundry_msg_from_ipc_msg() {
+       local ipc_msg="$1"
+
+       local basemsg
+       local type
+       local typeno
+       local data
+
+       if ! basemsg=$(ipc_msg_get_data "$ipc_msg"); then
+               return 255
+       fi
+
+       if ! _foundry_msg_version_supported "$basemsg"; then
+               return 255
+       fi
+
+       if ! type=$(foundry_msg_get_type "$basemsg") ||
+          ! data=$(foundry_msg_get_data "$basemsg"); then
+               return 255
+       fi
+
+       if ! array_contains "$type" "${!__foundry_msg_typemap[@]}"; then
+               return 255
+       fi
+
+       typeno="${__foundry_msg_typemap[$type]}"
+
+       echo "$data"
+       return "$typeno"
+}
+
+foundry_msg_new() {
+       local type="$1"
+       local data="$2"
+
+       local msg
+
+       if ! msg=$(json_object "version" "i:$__foundry_msg_version" \
+                              "type" "s:$type"                     \
+                              "data" "o:$data"); then
+               return 1
+       fi
+
+       echo "$msg"
+       return 0
+}
diff --git a/spec/base.json b/spec/base.json
new file mode 100644 (file)
index 0000000..2924d51
--- /dev/null
@@ -0,0 +1,28 @@
+{
+    "$schema": "https://json-schema.org/draft/2020-12/schema",
+    "$id": "https://m10k.eu/foundry/base.json",
+    "title": "Foundry Base Message",
+    "type": "object",
+
+    "properties": {
+       "version": {
+           "description": "The version of the foundry message format used",
+           "type": "integer"
+       },
+
+       "type": {
+           "description": "The message type encapsulated within the data field",
+           "type": "string"
+       },
+
+       "data": {
+           "description": "The encapsulated message",
+           "type": "object"
+       }
+    },
+
+    "required": [
+       "type",
+       "data"
+    ]
+}