Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Storage host
- local keepAlive = true
- local manager
- function doHello()
- print("Sending Hello")
- local message = {command="helo"}
- rednet.broadcast(textutils.serialize(message))
- end
- function doRegister(resource)
- print(("Registering storage for %s"):format(resource))
- local message = {command = "storage", resource = resource}
- rednet.send(manager, textutils.serialize(message))
- end
- function handleCommunications()
- local sender, message = rednet.receive()
- print(("Received message from %d: %s"):format(sender, message))
- local actualMessage = textutils.unserialize(message)
- if actualMessage.command == "request" then
- handleRequest(actualMessage.amount)
- elseif actualMessage.command == "hello" then
- handleHello(sender)
- end
- end
- function handleHello(sender)
- print(("Received hello from %d"):format(sender))
- manager = sender
- local message = {command = "ack"}
- rednet.send(manager, textutils.serialize(message))
- end
- function handleRequest(amount)
- print(("Handle request for %d"):format(amount))
- for i = 1, amount-1, 1 do
- redstone.setOutput("top", true)
- os.sleep(0.2)
- redstone.setOutput("top", false)
- end
- local message = {command = "ack"}
- rednet.send(manager, textutils.serialize(message))
- end
- function handleTerminate()
- local event, key, is_held = os.pullEvent("key")
- if keys.getName(key) ~= "f12" then
- return
- end
- redstone.setOutput("top", false)
- local message = {command="bye"}
- rednet.send(manager, textutils.serialize(message))
- keepAlive = false
- end
- function main(...)
- resource = ...
- if not resource then
- print("Please specify a resource: storage cobblestone")
- return
- end
- peripheral.find("modem", rednet.open)
- doHello()
- handleCommunications()
- doRegister(resource)
- while keepAlive do
- parallel.waitForAny(handleCommunications, handleTerminate)
- end
- end
- main(...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement