-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1103 from javierbrk/fix/multiwriter_empty_data
Adds shared-state rpcd data,error output format and shared-state-async rpcd reimplementation
- Loading branch information
Showing
4 changed files
with
101 additions
and
119 deletions.
There are no files selected for viewing
104 changes: 41 additions & 63 deletions
104
packages/shared-state-async/files/usr/libexec/rpcd/shared-state-async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,41 @@ | ||
#!/usr/bin/env lua | ||
|
||
--[[ | ||
Shared State Async | ||
Copyright (c) 2024 Javier Jorge <[email protected]> | ||
Copyright (c) 2024 Instituto Nacional de Tecnolog..a Industrial | ||
Copyright (C) 2024 Asociacion Civil Altermundi <[email protected]> | ||
This is free software, licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 | ||
]] | ||
-- | ||
local utils = require('lime.utils') | ||
local json = require 'luci.jsonc' | ||
|
||
local function get(msg) | ||
local error = os.execute("shared-state-async get " .. | ||
msg.data_type .. " 2>/dev/null ") | ||
-- if os.execute dont fail will print the output and rpcd wont execute | ||
-- the following lines. If there is an error the above code wont print | ||
-- anything and this code will return the error code. | ||
utils.printJson({ | ||
error = error | ||
}) | ||
end | ||
|
||
local function sync(msg) | ||
local error = os.execute("shared-state-async sync " .. | ||
msg.data_type .. " " .. table.concat(msg.peers_ip or {}, " ") .. " 2>/dev/null ") | ||
utils.printJson({ | ||
error = error | ||
}) | ||
end | ||
|
||
--{"data_type":"data","peers_ip":["10.0.0.1","10.0.0.2"]} | ||
--{"data_type":"data","peers_ip":["10.0.0.1"]} | ||
local methods = { | ||
get = { | ||
data_type = 'value' | ||
}, | ||
sync = { | ||
data_type = 'value', | ||
peers_ip = 'value' | ||
} | ||
} | ||
|
||
if arg[1] == 'list' then | ||
utils.printJson(methods) | ||
end | ||
|
||
if arg[1] == 'call' then | ||
local msg = utils.rpcd_readline() | ||
msg = json.parse(msg) | ||
if arg[2] == 'get' then | ||
get(msg) | ||
elseif arg[2] == 'sync' then | ||
sync(msg) | ||
else | ||
utils.printJson({ | ||
error = "Method not found" | ||
}) | ||
end | ||
end | ||
#!/bin/sh | ||
|
||
# Shared State Async | ||
# Copyright (c) 2024 Javier Jorge <[email protected]> | ||
# Copyright (c) 2024 Instituto Nacional de Tecnologia Industrial | ||
# Copyright (C) 2024 Asociacion Civil Altermundi <[email protected]> | ||
# SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
|
||
sinc_args="" | ||
|
||
case "$1" in | ||
list) | ||
echo '{ "sync": { "data_type": "str", "peers_ip": "str" }, "get": { "data_type": "str" } }' | ||
;; | ||
call) | ||
# source jshn shell library | ||
. /usr/share/libubox/jshn.sh | ||
read msg | ||
json_load $msg | ||
json_get_vars data_type | ||
json_get_vars peers_ip | ||
case "$2" in | ||
get) | ||
if output=$(shared-state-async get $data_type 2>/dev/null) | ||
then | ||
echo {\"data\": $output , \"error\" :$?} | ||
else | ||
echo {\"data\": {} , \"error\": $? } | ||
fi | ||
;; | ||
sync) | ||
shared-state-async sync $data_type ${peers_ip//,/ } > /dev/null 2>&1 | ||
echo {\"data\": {} , \"error\": $? } | ||
;; | ||
*) | ||
echo '{\"data\" {} ,\"error\" = "Method not found"}' | ||
;; | ||
esac | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 28 additions & 25 deletions
53
packages/shared-state/files/usr/libexec/rpcd/shared-state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,48 @@ | ||
#!/usr/bin/env lua | ||
--[[ | ||
Shared State | ||
#!/usr/bin/env lua | ||
|
||
Copyright (c) 2023 Javier Jorge <[email protected]> | ||
Copyright (c) 2023 Instituto Nacional de Tecnología Industrial | ||
Copyright (C) 2023 Asociación Civil Altermundi <[email protected]> | ||
--! Shared State | ||
--! Copyright (c) 2023 Javier Jorge <[email protected]> | ||
--! Copyright (c) 2023 Instituto Nacional de Tecnología Industrial | ||
--! Copyright (C) 2023 Asociación Civil Altermundi <[email protected]> | ||
--! SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
This is free software, licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 | ||
]] -- | ||
local ubus = require "ubus" | ||
local utils = require('lime.utils') | ||
local shared_state = require("shared-state") | ||
local json = require 'luci.jsonc' | ||
|
||
require("nixio.util") | ||
|
||
local function getFromSharedState(msg) | ||
local sharedState = shared_state.SharedState:new(msg.data_type, | ||
nixio.syslog) | ||
local response_template = {data = {}, error = 500} | ||
|
||
local function showData(sharedState) | ||
local resultTable = sharedState:get() | ||
local response ={} | ||
for k,v in pairs(resultTable) do | ||
response[k]=v.data | ||
if next(resultTable) == nill then | ||
response_template.error = 404 | ||
else | ||
for k, v in pairs(resultTable) do | ||
response_template.data[k] = v.data | ||
end | ||
response_template.error = 0 | ||
end | ||
utils.printJson(response) | ||
utils.printJson(response_template) | ||
end | ||
|
||
local function getFromSharedState(msg) | ||
local sharedState = shared_state.SharedState:new(msg.data_type, | ||
nixio.syslog) | ||
showData(sharedState) | ||
end | ||
|
||
local function getFromSharedStateMultiWriter(msg) | ||
local sharedState = shared_state.SharedStateMultiWriter:new(msg.data_type, | ||
nixio.syslog) | ||
local resultTable = sharedState:get() | ||
local response ={} | ||
for k,v in pairs(resultTable) do | ||
response[k]=v.data | ||
end | ||
utils.printJson(response) | ||
local sharedState = shared_state.SharedStateMultiWriter:new(msg.data_type, | ||
nixio.syslog) | ||
showData(sharedState) | ||
end | ||
|
||
local function insertIntoSharedStateMultiWriter(msg) | ||
local sharedState = shared_state.SharedStateMultiWriter:new(msg.data_type, | ||
nixio.syslog) | ||
local sharedState = shared_state.SharedStateMultiWriter:new(msg.data_type, | ||
nixio.syslog) | ||
local inputTable = msg.json or {} | ||
sharedState:insert(inputTable) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters