NeOzay

relay

Aug 3rd, 2021 (edited)
555
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local event = require("event")
  2. local component = require("component")
  3. local os = require("os")
  4. local relay = {}
  5.  
  6. local function formatMessage(...)
  7.     local mdl = {}
  8.     local allData = { ... }
  9.     mdl.eventType = allData[1]
  10.     mdl.receiverAddress = allData[2]
  11.     mdl.senderAddress = allData[3]
  12.     mdl.port = allData[4]
  13.     mdl.distance = allData[5]
  14.     mdl.data = {}
  15.  
  16.     for i = 6, #allData do
  17.         table.insert(mdl.data, allData[i])
  18.     end
  19.     return mdl
  20. end
  21.  
  22. ---improve modem
  23. function relay.Modem(modem)
  24.     local mdl = modem
  25.  
  26.     function mdl.broadcastPingMessage(port, ...)
  27.         mdl.open(port + 1)
  28.         --print(mdl.isOpen(port+1))
  29.         for i = 1, 3 do
  30.             mdl.broadcast(port, ...)
  31.             --print("send")
  32.             local _, _, _, _, _, message = event.pull(3, "modem_message")
  33.  
  34.             if message == "receive" then
  35.                 break
  36.             end
  37.         end
  38.         mdl.close(port + 1)
  39.     end
  40.  
  41.     function mdl.sendPingMessage(address, port, ...)
  42.         mdl.open(port + 1)
  43.         for i = 1, 3 do
  44.             mdl.send(address, port, ...)
  45.             --print("send")
  46.             local _, _, senderAddress, _, _, message = event.pull(3, "modem_message")
  47.  
  48.             if address == senderAddress and message == "receive" then
  49.                 break
  50.             end
  51.         end
  52.         mdl.close(port + 1)
  53.     end
  54.  
  55.     return mdl
  56. end
  57.  
  58. ---improve tunnel
  59. function relay.Tunnel(tunnel)
  60.     local mdl = tunnel
  61.  
  62.     function mdl.sendPingMessage(...)
  63.         for i = 1, 3 do
  64.             mdl.send(...)
  65.             --print("send")
  66.             local _, _, _, _, _, message = event.pull(3, "modem_message")
  67.             if message == "receive" then
  68.                 break
  69.             end
  70.         end
  71.     end
  72.     return mdl
  73. end
  74.  
  75. function relay:setComponentModem(modem)
  76.     self.modem = modem
  77. end
  78.  
  79. function relay:setComponentTunnel(tunnel)
  80.     self.tunnel = tunnel
  81. end
  82.  
  83. function relay:receivePingMessage()
  84.     local data = formatMessage(event.pull("modem_message"))
  85.     os.sleep(0.5)
  86.     if data.port == 0 then
  87.         self.tunnel.send("receive")
  88.     else
  89.         --print("send")
  90.         self.modem.send(data.senderAddress, data.port + 1, "receive")
  91.     end
  92.     --print("receive message from " .. data.senderAddress)
  93.     return data
  94. end
  95.  
  96. return relay
  97.  
RAW Paste Data