eytixis

Transceiver API (Opencomputers mod - OpenOS)

Sep 10th, 2020
769
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- TRANSCEIVER API
  2.  
  3. -- note: use only 1 modem or 1 tunnel for the API's best performance
  4. -- Not to be used as a junction between tunnels and network cards. Use TunnelLinker instead.
  5.  
  6. local event = require("event")
  7. local component = require("component")
  8. local transceiver = {}
  9. local ports = {}
  10. local device
  11.  
  12. function transceiver.reset()
  13.     for k,v in pairs(component.list()) do
  14.         if v == "tunnel" or v == "modem" then
  15.             device = component.proxy(k)
  16.         end
  17.     end
  18.     transceiver.type = device.type
  19. end
  20.  
  21. function isOpenPort(pr)
  22.     for k,v in pairs(ports) do
  23.         if (v == pr) then
  24.             return true
  25.         end
  26.     end
  27.     return false
  28. end
  29.  
  30.  
  31. function transceiver.receive()
  32.     if (device.type == "modem") then
  33.         local m = {event.pull("modem_message")}
  34.         table.remove(m,1)--event
  35.         table.remove(m,1)--receiver
  36.         table.remove(m,1)--sender
  37.         local port = m[1]
  38.         table.remove(m,1)--port
  39.         table.remove(m,1)--range
  40.  
  41.         if (device.type == "tunnel") then
  42.             port = m[1]
  43.             table.remove(m,1)
  44.         end
  45.         return port, m
  46.     end
  47.     if (device.type == "tunnel") then
  48.         local m = nil
  49.         local port = nil
  50.         repeat
  51.             m = {event.pull("modem_message")}
  52.             table.remove(m,1)--event
  53.             table.remove(m,1)--receiver
  54.             table.remove(m,1)--sender
  55.             table.remove(m,1)--port
  56.             table.remove(m,1)--range
  57.             port = m[1]
  58.             table.remove(m,1)--tport
  59.         until(isOpenPort(port))
  60.         return port, m
  61.     end
  62. end
  63.  
  64. function transceiver.send(port, ...)
  65.     transceiver.sendT(port, {...})
  66. end
  67.  
  68. function transceiver.sendT(port,data)
  69.     if (device.type == "tunnel") then
  70.         table.insert(data, port, 1)
  71.         device.send(table.unpack(data))
  72.     elseif (device.type == "modem") then
  73.         device.broadcast(port, table.unpack(data))
  74.     end
  75. end
  76.  
  77. function transceiver.open(port)
  78.     if (device.type == "tunnel") then
  79.         table.insert(ports, #ports+1, port)
  80.     elseif (device.type == "modem") then
  81.         device.open(port)
  82.     end
  83. end
  84.  
  85. function transceiver.close(port)
  86.     if (device.type == "tunnel") then
  87.         for k, v in pairs(ports) do
  88.             if (v == port) then
  89.                 table.remove(ports, k)
  90.             end
  91.         end
  92.     elseif (device.type == "modem") then
  93.         device.close(port)
  94.     end
  95. end
  96.  
  97. function transceiver.isOpen(p)
  98.     if (device.type == "tunnel") then
  99.         return isOpenPort(p)
  100.     elseif (device.type == "modem") then
  101.         return device.isOpen(p)
  102.     end
  103. end
  104.  
  105.  
  106.  
  107. transceiver.reset()
  108. return transceiver
RAW Paste Data