Guest User

redProxy

a guest
Oct 30th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1.  
  2. CHANNEL_BROADCAST = 65535
  3. CHANNEL_REPEAT = 65533
  4. proxyid = 999
  5. local tReceivedMessages = {}
  6. local tReceivedMessageTimeouts = {}
  7. local tHostnames = {}
  8. function setID(id)
  9.     proxyid = id
  10. end
  11.  
  12. local function isOpen( sModem )
  13.     if sModem then
  14.         -- Check if a specific modem is open
  15.         if type( sModem ) ~= "string" then
  16.             error( "expected string", 2 )
  17.         end
  18.         if peripheral.getType( sModem ) == "modem" then
  19.             return peripheral.call( sModem, "isOpen", os.getComputerID() ) and peripheral.call( sModem, "isOpen", CHANNEL_BROADCAST )
  20.         end
  21.     else
  22.         -- Check if any modem is open
  23.         for n,sModem in ipairs( peripheral.getNames() ) do
  24.             if isOpen( sModem ) then
  25.                 return true
  26.             end
  27.         end
  28.     end
  29.     return false
  30. end
  31.  
  32. function send( nRecipient, message, sProtocol )
  33.     -- Generate a (probably) unique message ID
  34.     -- We could do other things to guarantee uniqueness, but we really don't need to
  35.     -- Store it to ensure we don't get our own messages back
  36.     local nMessageID = math.random( 1, 2147483647 )
  37.     tReceivedMessages[ nMessageID ] = true
  38.     tReceivedMessageTimeouts[ os.startTimer( 30 ) ] = nMessageID
  39.  
  40.     -- Create the message
  41.     local nReplyChannel = proxyid
  42.     local tMessage = {
  43.         nMessageID = nMessageID,
  44.         nRecipient = nRecipient,
  45.         message = message,
  46.         sProtocol = sProtocol,
  47.     }
  48.  
  49.     if nRecipient == os.getComputerID() then
  50.         -- Loopback to ourselves
  51.         os.queueEvent( "rednet_message", nReplyChannel, message, sProtocol )
  52.  
  53.     else
  54.         -- Send on all open modems, to the target and to repeaters
  55.         local sent = false
  56.         for n,sModem in ipairs( peripheral.getNames() ) do
  57.             if isOpen( sModem ) then
  58.                 peripheral.call( sModem, "transmit", nRecipient, nReplyChannel, tMessage );
  59.                 peripheral.call( sModem, "transmit", CHANNEL_REPEAT, nReplyChannel, tMessage );
  60.                 sent = true
  61.             end
  62.         end
  63.     end
  64. end
  65.  
  66. function broadcast( message, sProtocol )
  67.     send( CHANNEL_BROADCAST, message, sProtocol )
  68. end
Advertisement
Add Comment
Please, Sign In to add comment