-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds shared-state rpcd data,error output format and shared-state-async rpcd reimplementation #1103
Merged
G10h4ck
merged 2 commits into
libremesh:master
from
javierbrk:fix/multiwriter_empty_data
Apr 24, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using shell instead of Lua resulted in a much nicer code as anticipated :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
much better, I was worried about json parsing but some else was also worried before me.