Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Processor Host
- local keepAlive = true
- local processor;
- local resource;
- local manager;
- local PROCESS_MINIMUM = 64
- function doHello()
- print("Sending Hello")
- local message = {command="helo"}
- rednet.broadcast(textutils.serialize(message))
- end
- function doRequest(amount)
- print(("Requesting %s: %d"):format(resource, amount))
- local message = {command = "request", resource = resource, amount = amount}
- 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(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 handleTerminate()
- os.pullEventRaw("terminate")
- local message = {command="bye"}
- rednet.send(manager, textutils.serialize(message))
- keepAlive = false
- end
- function processInventory()
- local inventory = processor.list()
- local count = 0
- for slot, item in pairs(inventory) do
- if slot then count = count + item.count end
- end
- if count < PROCESS_MINIMUM then
- doRequest(PROCESS_MINIMUM - count)
- end
- end
- function main(...)
- resource = ...
- if not resource then
- print("Please specify a requirement: processor cobblestone")
- return
- end
- peripheral.find("modem", rednet.open)
- processor = peripheral.wrap("top")
- doHello()
- handleCommunications()
- while keepAlive do
- parallel.waitForAny(processInventory, handleCommunications, handleTerminate)
- os.sleep(10)
- end
- end
- main(...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement