Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/lime-system/files/usr/lib/lua/lime/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,21 @@ function utils.is_valid_mac(string)
end
end

function utils.deepcompare(t1,t2)
Comment thread
spiccinini marked this conversation as resolved.
if t1 == t2 then return true end
local ty1 = type(t1)
local ty2 = type(t2)
if ty1 ~= ty2 then return false end
if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
for k1, v1 in pairs(t1) do
local v2 = t2[k1]
if v2 == nil or not utils.deepcompare(v1, v2) then return false end
end
for k2, v2 in pairs(t2) do
local v1 = t1[k2]
if v1 == nil or not utils.deepcompare(v1, v2) then return false end
end
return true
end

return utils
57 changes: 57 additions & 0 deletions packages/shared-state/files/usr/bin/shared-state-multiwriter
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/lua
--! Minimalistic CRDT-like shared state structure suitable for mesh networks
--! which handles conflicts in the same entry using last modified timestamp.
--!
--! Copyright (C) 2019 Gioacchino Mazzurco <gio@altermundi.net>
--!
--! This program is free software: you can redistribute it and/or modify
--! it under the terms of the GNU Affero General Public License version 3 as
--! published by the Free Software Foundation.
--!
--! This program is distributed in the hope that it will be useful,
--! but WITHOUT ANY WARRANTY; without even the implied warranty of
--! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
--! GNU Affero General Public License for more details.
--!
--! You should have received a copy of the GNU Affero General Public License
--! along with this program. If not, see <http://www.gnu.org/licenses/>.

if type(arg[2]) ~= "string" or arg[2]:len() < 1 then
print (arg[0], "needs CRDT name to be specified as second argument")
os.exit(-22)
end

local JSON = require("luci.jsonc")
local shared_state = require("shared-state")
require("nixio.util")

nixio.openlog("shared-state")
local logmask = "info"
if os.getenv("DEBUG") then
logmask = "debug"
end
nixio.setlogmask(logmask)

local sharedState = shared_state.SharedStateMultiWriter:new(arg[2], nixio.syslog)

if arg[1] == "insert" then
local inputTable = JSON.parse(io.stdin:read("*all")) or {}
sharedState:insert(inputTable)
elseif arg[1] == "get" then
local resultTable = sharedState:get()
print(JSON.stringify(resultTable))
elseif arg[1] == "sync" then
local urls = {}
if arg[3] ~= nil then for i=3,#arg do table.insert(urls, arg[i]) end end
sharedState:sync(urls)
elseif arg[1] == "reqsync" then
local inputJson = JSON.parse(io.stdin:read("*all"))
sharedState:merge(inputJson)
print(sharedState:toJsonString())
else
print(arg[0], "needs an operation name to be specified as first argument")
nixio.closelog()
os.exit(-22)
end

nixio.closelog()
Loading