Advertisement
KaoSDlanor

background rednet

Nov 12th, 2012
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.68 KB | None | 0 0
  1. local tBanned={}
  2. myNet={}
  3.  
  4. local function co() --the coroutine that manages the messages and forwards them, queues events etc
  5.     while true do
  6.         local input={os.pullEventRaw()}
  7.         if input[1]=='rednet_message' then
  8.             table.remove(input,1)
  9.             local t=textutils.unserialize(input[2])
  10.             if t and t[1]=='myNet' and not tBanned[t[3]] then
  11.                 tBanned[t[3]]=os.startTimer(2)
  12.                 rednet.broadcast(input[2])
  13.                 if not t[4] or t[4]==os.computerID() then
  14.                     os.queueEvent('myNet',t[2],t[3])
  15.                 end
  16.             end
  17.         elseif input[1]=='timer' then
  18.             for k,v in pairs(tBanned) do
  19.                 if v==input[2] then
  20.                     tBanned[k]=nil
  21.                     break
  22.                 end
  23.             end
  24.         end
  25.     end
  26. end
  27.  
  28. function myNet.send(message,id)
  29.     rednet.broadcast(textutils.serialize({'myNet',os.computerID(),message,id}))
  30. end
  31.  
  32. function myNet.receive(timeout)
  33.     local timed=false
  34.     if type(timeout)=='number' then timed=os.startTimer(timeout) end
  35.     while true do
  36.         local evts={os.pullEvent()}
  37.         if evts[1]=='timer' and evts[2]==timed then
  38.             return nil
  39.         elseif evts[1]=='myNet' then
  40.             return unpack(evts,2)
  41.         end
  42.     end
  43. end
  44.  
  45. if not fs.exists('bGo') then --1st boot that relaunches shell
  46.     term.clear()
  47.     term.setCursorPos(1,1)
  48.     local oFile=io.open('bGo','w')
  49.     oFile:write('true')
  50.     oFile:close()
  51.     local tThreads={coroutine.wrap(co),coroutine.wrap(function() os.run({},'rom/programs/shell') end)}
  52.     local evts={}
  53.     while true do
  54.         for iNum,fThread in pairs(tThreads) do
  55.             fThread(unpack(evts))
  56.         end
  57.         evts={os.pullEventRaw()}
  58.     end
  59. else --2nd boot where shell has been relaunched, it launches startup2 in case you need another startup file on the PC
  60.     fs.delete('bGo')
  61.     if fs.exists('startup2') then
  62.         shell.run('startup2')
  63.     end
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement