Advertisement
osmarks

ApioNet

Aug 4th, 2020 (edited)
1,701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component, computer = component, computer
  2. if require then component = require "component" computer = require "computer" end
  3. local netcards = {}
  4. for addr in component.list "modem" do table.insert(netcards, component.proxy(addr)) end
  5. local tunnels = {}
  6. for addr in component.list "tunnel" do table.insert(tunnels, component.proxy(addr)) end
  7. local computer_id = component.list "computer"()
  8. local computer_peripheral = component.proxy(computer_id)
  9. local computer_sid = computer_id:sub(1, 8)
  10. local eeprom = component.proxy(component.list "eeprom"())
  11.  
  12. computer_peripheral.beep(600)
  13.  
  14. local recents = {}
  15.  
  16. local PORT = 4096
  17. local DBG_PORT = 4097
  18.  
  19. for _, card in pairs(netcards) do
  20.     card.open(PORT)
  21.     card.open(DBG_PORT)
  22.     card.setWakeMessage("poweron", true)
  23.     if card.setStrength then card.setStrength(math.huge) end
  24.     computer_peripheral.beep(1500)
  25. end
  26. for _, tun in pairs(tunnels) do tun.setWakeMessage("poweron", true) computer_peripheral.beep(1200) end
  27.  
  28. computer_peripheral.beep(400)
  29.  
  30. while true do
  31.     local ev, _, _, port, distance, mtxt, mid = computer.pullSignal(5)
  32.     if ev == "modem_message" and type(mid) == "string" and mtxt ~= nil and (port == PORT or port == 0) and not recents[mid] then
  33.         recents[mid] = computer.uptime() + 120
  34.         for _, card in pairs(netcards) do
  35.             pcall(card.broadcast, PORT, mtxt, mid, computer_sid)
  36.         end
  37.         for _, tun in pairs(tunnels) do
  38.             pcall(tun.send, mtxt, mid, computer_sid)
  39.         end
  40.     end
  41.     if ev == "modem_message" and type(mtxt) == "string" and port == DBG_PORT and distance < 8 then
  42.         if mtxt == "ping" then
  43.             computer_peripheral.beep(1000)
  44.             card.broadcast(DBG_PORT, computer_sid)
  45.         elseif mtxt == "flash" and type(mid) == "string" then
  46.             computer_peripheral.beep(800)
  47.             eeprom.set(mid)
  48.         end
  49.     end
  50.     local uptime = computer.uptime()
  51.     for mid, deadline in pairs(recents) do
  52.         if uptime > deadline then
  53.             recents[mid] = nil
  54.         end
  55.     end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement