|
| 1 | +#!/usr/bin/lua |
| 2 | +--! Minimalistic CRDT-like shared state structure suitable for mesh networks |
| 3 | +--! which handles conflicts in the same entry using last modified timestamp. |
| 4 | +--! |
| 5 | +--! Copyright (C) 2019 Gioacchino Mazzurco <gio@altermundi.net> |
| 6 | +--! |
| 7 | +--! This program is free software: you can redistribute it and/or modify |
| 8 | +--! it under the terms of the GNU Affero General Public License version 3 as |
| 9 | +--! published by the Free Software Foundation. |
| 10 | +--! |
| 11 | +--! This program is distributed in the hope that it will be useful, |
| 12 | +--! but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +--! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +--! GNU Affero General Public License for more details. |
| 15 | +--! |
| 16 | +--! You should have received a copy of the GNU Affero General Public License |
| 17 | +--! along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +if type(arg[2]) ~= "string" or arg[2]:len() < 1 then |
| 20 | + print (arg[0], "needs CRDT name to be specified as second argument") |
| 21 | + os.exit(-22) |
| 22 | +end |
| 23 | + |
| 24 | +local JSON = require("luci.jsonc") |
| 25 | +local shared_state = require("shared-state") |
| 26 | +require("nixio.util") |
| 27 | + |
| 28 | +nixio.openlog("shared-state") |
| 29 | +local logmask = "info" |
| 30 | +if os.getenv("DEBUG") then |
| 31 | + logmask = "debug" |
| 32 | +end |
| 33 | +nixio.setlogmask(logmask) |
| 34 | + |
| 35 | +local sharedState = shared_state.SharedStateMultiWriter:new(arg[2], nixio.syslog) |
| 36 | + |
| 37 | +if arg[1] == "insert" then |
| 38 | + local inputTable = JSON.parse(io.stdin:read("*all")) or {} |
| 39 | + sharedState:insert(inputTable) |
| 40 | +elseif arg[1] == "get" then |
| 41 | + local resultTable = sharedState:get() |
| 42 | + print(JSON.stringify(resultTable)) |
| 43 | +elseif arg[1] == "sync" then |
| 44 | + local urls = {} |
| 45 | + if arg[3] ~= nil then for i=3,#arg do table.insert(urls, arg[i]) end end |
| 46 | + sharedState:sync(urls) |
| 47 | +elseif arg[1] == "reqsync" then |
| 48 | + local inputJson = JSON.parse(io.stdin:read("*all")) |
| 49 | + sharedState:merge(inputJson) |
| 50 | + print(sharedState:toJsonString()) |
| 51 | +else |
| 52 | + print(arg[0], "needs an operation name to be specified as first argument") |
| 53 | + nixio.closelog() |
| 54 | + os.exit(-22) |
| 55 | +end |
| 56 | + |
| 57 | +nixio.closelog() |
0 commit comments