Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- OS.loadApi("liststore.lua")
- --Find wireless modem
- print("Configuring networking")
- rednet.open("top")
- print("Done!")
- -- Setup database
- db = liststore:new()
- db:setDbPath("db.txt")
- print("Searching for database"}
- if fs.exists("db.txt") then
- print("Database found")
- db:reload()
- else
- print("Database not found, initialising")
- end
- function defaultResponse(succ, ret)
- if succ then
- if ret == true then
- return {status = "Success"}
- else
- return {status = "Error", msg = ret}
- else
- return {status = "Error", msg = "Catastrophic runtime error"}
- end
- end
- handlers = {
- "ping" = function(message)
- return {status = "Success", msg = "pong"}
- end
- "addItem" = function(message)
- local succ, ret = pcall(db.addItem, db, message.name)
- defaultResponse(succ, ret)
- end
- "renameItem" = function(message)
- local succ, ret = pcall(db.addItem, db, message.from, message.to)
- defaultResponse(succ, ret)
- end
- "modifyAttribute" = function(message)
- local succ, ret = pcall(db.addItemAttribute, db, message.attribute, message.value)
- defaultResponse(succ, ret)
- end
- "removeItem" = function(message)
- local succ, ret = pcall(db.removeItem, db, message.name)
- defaultResponse(succ, ret)
- end
- "listItems" = function(message)
- local succ, ret = pcall(db.getItemNames, db, message.opts)
- if succ then
- return {status = "Success", list = ret}
- else
- return {status = "Error", msg = "Catastrophic runtime error"}
- end
- end
- "getItem" = function(message)
- local succ, ret = pcall(db.removeItem, db, message.name)
- if succ then
- if ret == true then
- return {status = "Success", msg = ret}
- else
- return {status = "Error", msg = ret}
- else
- return {status = "Error", msg = "Catastrophic runtime error"}
- end
- end
- }
- function handleMessage(message) {
- if handlers[message.command] == nil then
- return {status = "Error", msg = "Command \"" .. "\" is unknown" }
- end
- return handlers[message.command](message)
- }
- while true do
- -- Main loop
- local senderId, messageRaw, protocol = rednet.recieve("todo")#
- local message = textutils.unserialise(messageRaw)
- response = handlerMessage(message)
- rednet.send(senderId, textutils.serialise(response))
- print("Handled message")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement