Add remotedbserver.lua
This commit is contained in:
36
remotedbserver.lua
Normal file
36
remotedbserver.lua
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
if not fs.exists("data.db") then
|
||||||
|
local file = fs.open("data.db", "w")
|
||||||
|
file.write("{}")
|
||||||
|
file.close()
|
||||||
|
end
|
||||||
|
local file = fs.open("data.db","r+")
|
||||||
|
local eplib = require("entrypointlib")
|
||||||
|
if not file then error("failed to open db file") end
|
||||||
|
|
||||||
|
local function readTable()
|
||||||
|
file.seek("set")
|
||||||
|
return textutils.unserialise(file.readAll() or "{}") or {}
|
||||||
|
end
|
||||||
|
|
||||||
|
local function writeTable(t)
|
||||||
|
file.seek("set")
|
||||||
|
file.write(textutils.serialise(t))
|
||||||
|
file.flush()
|
||||||
|
end
|
||||||
|
|
||||||
|
local function receive()
|
||||||
|
while true do
|
||||||
|
sleep(0)
|
||||||
|
local _,msg,id = os.pullEvent("network_packet")
|
||||||
|
if msg.proto == "getTable" then
|
||||||
|
eplib.send({proto = "resp", table = readTable()}, id)
|
||||||
|
print("sent table to "..tostring(id))
|
||||||
|
elseif msg.proto == "setTable" then
|
||||||
|
writeTable(msg.table)
|
||||||
|
print("updated table from "..tostring(id))
|
||||||
|
eplib.send({proto="OK"},id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
parallel.waitForAny(eplib.loop,receive)
|
||||||
Reference in New Issue
Block a user