Advertisement
1lann

repeat

Apr 13th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1. rednet.CHANNEL_REPEAT = 65533
  2.  
  3. -- Find modems
  4. local tModems = {}
  5. for n,sModem in ipairs( peripheral.getNames() ) do
  6.     if peripheral.getType( sModem ) == "modem" then
  7.         table.insert( tModems, sModem )
  8.     end
  9. end
  10. if #tModems == 0 then
  11.     print( "No modems found." )
  12.     return
  13. elseif #tModems == 1 then
  14.     print( "1 modem found." )
  15. else
  16.     print( #tModems .. " modems found." )
  17. end
  18.  
  19. function open( nChannel )
  20.     for n=1,#tModems do
  21.         local sModem = tModems[n]
  22.         peripheral.call( sModem, "open", nChannel )
  23.     end
  24. end
  25.  
  26. function close( nChannel )
  27.     for n=1,#tModems do
  28.         local sModem = tModems[n]
  29.         peripheral.call( sModem, "close", nChannel )
  30.     end
  31. end
  32.  
  33. -- Open channels
  34. open( rednet.CHANNEL_REPEAT )
  35.  
  36. -- Main loop (terminate to break)
  37. local ok, error = pcall( function()
  38.     local tReceivedMessages = {}
  39.     local tReceivedMessageTimeouts = {}
  40.     local nTransmittedMessages = 0
  41.  
  42.     while true do
  43.         local sEvent, sModem, nChannel, nReplyChannel, tMessage = os.pullEvent()
  44.         if sEvent == "modem_message" then
  45.             -- Got a modem message, rebroadcast it if it's a rednet thing
  46.             if nChannel == rednet.CHANNEL_REPEAT then
  47.                 if type( tMessage ) == "table" and tMessage.nMessageID and tMessage.nRecipient then
  48.                     if not tReceivedMessages[ tMessage.nMessageID ] then
  49.                         -- Ensure we only repeat a message once
  50.                         tReceivedMessages[ tMessage.nMessageID ] = true
  51.                         tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = tMessage.nMessageID
  52.  
  53.                         -- Send on all other open modems, to the target and to other repeaters
  54.                         for n=1,#tModems do
  55.                             local sOtherModem = tModems[n]
  56.                             peripheral.call( sOtherModem, "transmit", rednet.CHANNEL_REPEAT, nReplyChannel, tMessage )
  57.                             peripheral.call( sOtherModem, "transmit", tMessage.nRecipient, nReplyChannel, tMessage )
  58.                         end
  59.  
  60.                         -- Log the event
  61.                         nTransmittedMessages = nTransmittedMessages + 1
  62.                     end
  63.                 end
  64.             end
  65.  
  66.         elseif sEvent == "timer" then
  67.             -- Got a timer event, use it to clear the message history
  68.             local nTimer = sModem
  69.             local nMessageID = tReceivedMessageTimeouts[ nTimer ]
  70.             if nMessageID then
  71.                 tReceivedMessageTimeouts[ nTimer ] = nil
  72.                 tReceivedMessages[ nMessageID ] = nil
  73.             end
  74.  
  75.         end
  76.     end
  77. end )
  78. if not ok then
  79.     printError( error )
  80. end
  81.  
  82. -- Close channels
  83. close( rednet.CHANNEL_REPEAT )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement