Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open('right')
- local tQueue={}
- local intDone=true
- local function register()
- while true do
- local input={os.pullEvent()}
- if input[1]=='rednet_message' and input[3]=='gather' and (not leader) then
- rednet.send(input[2],'join')
- os.startTimer(1)
- while true do
- local evt={os.pullEvent()}
- if evt[1]=='timer' then
- break
- elseif evt[1]=='rednet_message' and evt[2]==input[2] and evt[3]=='accepted' then
- leader=input[2]
- print('registered to '..leader)
- break
- end
- end
- end
- end
- end
- local function listen()
- while true do
- local input={rednet.receive()}
- if type(textutils.unserialize(input[2]))=='table' then
- input[2]=textutils.unserialize(input[2])
- if leader and input[1]==leader and input[2][1]==os.computerID() then
- print(input[2][2])
- tQueue[#tQueue+1]=input[2][2]
- end
- end
- end
- end
- local function exec()
- while true do
- if tQueue[1] then
- if intDone==true then
- intDone=false
- rednet.send(leader,'busy')
- end
- if fs.exists('running') then fs.delete('running') end
- local oFile=io.open('running','w')
- oFile:write(table.remove(tQueue,1))
- oFile:close()
- shell.run('running')
- else
- sleep(0.5)
- end
- end
- end
- local tThreads={coroutine.create(register);
- coroutine.create(listen);
- coroutine.create(exec)}
- local tEvents={}
- while true do
- for k,v in pairs(tThreads) do
- coroutine.resume(v,unpack(tEvents))
- end
- tEvents=nil
- tEvents={os.pullEvent()}
- end
Advertisement
Add Comment
Please, Sign In to add comment