Abstract out request processing functionality

This commit is contained in:
osmarks 2018-05-06 08:28:22 +01:00
parent 01bbdc3a92
commit 84ce642973
4 changed files with 20 additions and 14 deletions

View File

@ -36,7 +36,7 @@ Commands:
w [name] - withdraw all items whose names contain [name]
w [qty] [name] - withdraw [qty] items whose names contain [name]
c - Craft item, using the turtle's inventory as a grid (turtle.craft)
ac - Atempt to craft item, using Dragon autocrafting. Takes an internal name.
ac [item] - Atempt to craft item, using Dragon autocrafting. Takes an internal name.
d - Dump all items into storage
d [slot] - Dump items in [slot] into storage
r - Force connected storage server to reindex

View File

@ -68,15 +68,16 @@ local function craft(i)
for _, tsk in pairs(tsks) do
craftOne(tsk)
end
return "OK"
else
return "ERROR"
end
end
while true do
local id, msg = rednet.receive "dragon"
if msg and msg.cmd and msg.cmd == "craft" and msg.item then
craft(msg.item)
rednet.send(id, "OK", "dragon")
util.processMessage(function(m)
if m.cmd and m.item and m.cmd == "craft" then
return craft(m.item)
end
end)
end

View File

@ -133,15 +133,12 @@ end
function processRequests()
while true do
local id, msg = rednet.receive "dragon"
if msg and msg.cmd and msg.uid then
util.processMessage(function(msg)
local ok, r = pcall(processRequest, msg)
if not ok then r = "ERROR" end
rednet.send(id, { msg = r, uid = msg.uid }, "dragon")
end
end
return r
end)
end
updateIndex()

View File

@ -103,4 +103,12 @@ local function split(str, sSeparator, nMax, bRegexp)
return aRecord
end
return { conf = conf, query = query, fetch = fetch, dump = dump, collate = collate, satisfied = satisfied, split = split }
local function processMessage(f)
local id, msg = rednet.receive "dragon"
if msg and msg.uid then
local r = f(msg)
rednet.send(id, { uid = msg.uid, msg = r }, "dragon")
end
end
return { conf = conf, query = query, fetch = fetch, dump = dump, collate = collate, satisfied = satisfied, split = split, process = process }