Advertisement
Pinkishu

Untitled

Feb 12th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. local tArgs = {...}
  2. if #tArgs < 1 or (tArgs[1] ~= "m" and tArgs[1] ~= "s") then error("Usage channelMaster <mode>\nMode: m/s = master/slave") end
  3. local masterMode = (tArgs[1]=="m")
  4. local commChannel = rednet.CHANNEL_BROADCAST-1
  5. local masterModemCount = 0
  6. local slaves = {}
  7. local mySlaveChannel = os.computerID()*3
  8. local dynamicChannels = {}
  9. local dynMax = 0
  10. local rangeIndex = 0
  11. local openDynChannels = 0
  12.  
  13. local modems = {}
  14. for k,v in pairs(rs.getSides()) do
  15.     if peripheral.getType(v) == "modem" then
  16.         table.insert(modems,{per=peripheral.wrap(v),openChannels=0})
  17.  
  18.     end
  19. end
  20.  
  21. for k,v in pairs(modems) do
  22.     v.per.closeAll()
  23. end
  24.  
  25. if masterMode then
  26.     print("Master Mode\nListening on: "..commChannel.." & " .. rednet.CHANNEL_BROADCAST)
  27.     modems[1].per.open(commChannel)
  28.     modems[1].per.open(rednet.CHANNEL_BROADCAST)
  29.     modems[1].openChannels = 2
  30. else
  31.     print("Slave Mode\nListening on: "..commChannel.." & "..mySlaveChannel)
  32.     modems[1].per.open(commChannel)
  33.     modems[1].per.open(mySlaveChannel)
  34.     modems[1].openChannels = 2
  35. end
  36.  
  37. dynMax = (#modems * 128)-2
  38.  
  39. print("Found "..#modems.. " modems")
  40.  
  41. if masterMode then
  42.     print("Ready to search...") os.pullEvent("key")
  43.     term.clear()
  44.     term.setCursorPos(1,1)
  45.     print("Waiting for clients...")
  46.     modems[1].per.transmit(commChannel,commChannel,"RPPING")
  47.     while true do
  48.         local args = {os.pullEvent()}
  49.         local cmd = table.remove(args,1)
  50.         if cmd == "key" then
  51.             if args[1] == keys.enter then
  52.                 break
  53.             end
  54.         elseif cmd == "modem_message" then
  55.             local p = textutils.unserialize(args[4])
  56.             if type(p) == "table" and type(p.mType) == "string" and p.mType == "RPANSWER" and type(p.modemCount) == "number" and type(p.channelCount) == "number" then
  57.                 table.insert(slaves,{args[3],p.channelCount})
  58.                 masterModemCount = masterModemCount + p.modemCount
  59.             end
  60.         end
  61.     end
  62.     print("Found " .. #slaves .. " slaves with a total of "..masterModemCount .. " modems.") os.pullEvent("key")
  63. else
  64.     print("Listening")
  65.     while true do
  66.         local ev,side,chan,replchan,msg,dist = os.pullEvent("modem_message")
  67.         if chan == commChannel and msg == "RPPING" then
  68.             modems[1].per.transmit(commChannel,mySlaveChannel,textutils.serialize({mType = "RPANSWER",modemCount=#modems,channelCount=dynMax}))
  69.             break
  70.         end
  71.     end
  72. end
  73.  
  74. local maxDynChannelRange = masterModemCount + dynMax
  75.  
  76. function closeAllChannels()
  77.     for k,v in pairs(dynamicChannels) do
  78.         modems[v].per.close(k)
  79.  
  80.     end
  81.     for k,v in pairs(modems) do
  82.         v.openChannels = 0
  83.     end
  84.     modems[1].openChannels = 2
  85.     dynamicChannels = {}
  86.     openDynChannels = 0
  87. end
  88.  
  89. function openRange(rStart,rWidth)
  90.     print("Opening "..rStart .. "=>"..(rStart+rWidth-1))
  91.     local modemI = 1
  92.     for i=rStart,rStart+(rWidth-1),1 do
  93.  
  94.         if modems[modemI].openChannels == 128 then
  95.  
  96.             modemI = modemI+1
  97.             print("Changing to modem #"..modemI)
  98.         end
  99.         dynamicChannels[i] = modemI
  100.         modems[modemI].openChannels = modems[modemI].openChannels + 1
  101.         modems[modemI].per.open(i)
  102.     end
  103. end
  104.  
  105. function masterListenRange(nR)
  106.     closeAllChannels()
  107.     rangeIndex = nR
  108.     local cChan = (rangeIndex * maxDynChannelRange)
  109.    
  110.     openRange(cChan,dynMax)
  111.     cChan = cChan + dynMax
  112.     for k,v in pairs(slaves) do
  113.  
  114.         modems[1].per.transmit(commChannel,v[1],textutils.serialize({mType = "RPLRANGE",sChan = cChan,sWidth = v[2]}))
  115.         cChan = cChan + v[2]
  116.     end
  117. end
  118.  
  119. if masterMode then
  120.     sleep(2)
  121.     masterListenRange(0)
  122. end
  123.  
  124. function logMsg(chan,replchan,msg)
  125.     print(chan.."=>"..replchan.."//"..msg)
  126. end
  127. print("Listening...")
  128. while true do
  129.    
  130.     local ev,side,chan,replchan,msg,dist = os.pullEvent()
  131.    
  132.     if ev == "modem_message" then
  133.         local pp = textutils.unserialize(msg)
  134.         if ((not masterMode) and chan == commChannel and replchan == mySlaveChannel and type(pp) == "table" and pp.mType == "RPLRANGE") then
  135.             closeAllChannels()
  136.             openRange(pp.sChan,pp.sWidth)
  137.         else
  138.             if not masterMode then
  139.                 if chan ~= commChannel then
  140.                     print("INTERCEPT")
  141.                     logMsg(chan,replchan,msg)
  142.                     modems[1].per.transmit(commChannel,mySlaveChannel,textutils.serialize({mType = "RPINTERCEPT", ["chan"]=chan,["replchan"]=replchan,["msg"]=msg }))
  143.                 end
  144.             else
  145.                 if type(pp) == "table" and type(pp.mType) == "string" and pp.mType == "RPINTERCEPT" then
  146.                     logMsg(pp.chan,pp.replchan,pp.msg)
  147.                 else
  148.                     logMsg(chan,replchan,msg)
  149.                 end
  150.             end
  151.         end
  152.     elseif ev == "key" then
  153.         if side == keys.left then
  154.             masterListenRange(math.max(rangeIndex-1,0))
  155.         elseif side == keys.right then
  156.             masterListenRange(rangeIndex+1)
  157.         end
  158.     end
  159.  
  160. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement