Advertisement
Guest User

uc.lua

a guest
Feb 19th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. component = require("component") local computer = require("computer")
  2.  
  3. -- The driver
  4. driver = component.proxy(component.list("modem")())
  5. me = driver.address
  6.  
  7. update = nil
  8. running = true
  9. netport = 666
  10.  
  11. -- Code
  12. tb = string.char -- To byte
  13. fb = string.byte -- From byte
  14.  
  15. requests = {}
  16. nodes = {}
  17. nearby = {}
  18. buffer = {}
  19. ttl = 32 -- 32 hops
  20.  
  21. function _send(s)
  22.   driver.broadcast(netport,s)
  23. end
  24.  
  25. function _lsend(s)
  26.   -- send through cable!
  27. end
  28.  
  29. function find(tab,who)
  30.   for i,v in ipairs(tab) do
  31.     if (v==who) then return i end
  32.   end
  33.   return 0
  34. end
  35.  
  36. function handle()
  37.   local e,interface,origin,oport,dist,data = computer.pullSignal(update)
  38.   if (e=="modem_message") then
  39.     local c = data:sub(1,1)
  40.     if (dist==0) then
  41.        -- CABLE LOCAL
  42.       local destl = fb(data:sub(2,2)) or 0
  43. print("DEST ADD LEN: "..destl)
  44.       local desta = data:sub(3,3+destl)
  45. print("Dest addr: "..desta)
  46.       if c=="C" then
  47.         -- Connect to
  48.         _send("R"..tb(ttl)..tb(destl)..desta..tb(me)..me)
  49.         requests[#requests+1] = desta
  50.         _lsend("CONNECTING")
  51.       elseif c=="V" then
  52.         -- Verify
  53.         a = find(nodes,desta)
  54.         if (a>0) then
  55.           -- We are conencted!
  56.           _lsend("CONNECTED")
  57.         else
  58.           _lsend("NOT CONNECTED")
  59.         end
  60.       elseif c=="D" then
  61.         -- Send data
  62.         a = find(nodes,desta)
  63.         if (a>0) then
  64.           _send("D"..tb(ttl)..tb(destl)..desta..tb(me)..me)  -- MAYBE TTL THAT GOT RECEIVED FROM REPLY!????
  65.         end
  66.       elseif c=="R" then
  67.         -- Receive data (uC sends data buffer to client)
  68.         for i,v in pairs(buffer) do
  69.           _lsend(tb(i)..i..v)
  70.         end
  71.     end
  72.     -- NEARBY NODES
  73.     if c=="H" then
  74.       -- New node nearby
  75.     elseif c=="I" then
  76.       -- We got a nearby host! Save him
  77.     elseif c=="Q" then
  78.       -- A host left us
  79.    
  80.     -- FAR NETWORK
  81.     elseif c=="E" then
  82.       -- Routed data
  83.     elseif c=="R" then
  84.       -- Route request
  85.     elseif c=="X" then
  86.       -- Route reply!
  87.     end
  88.   end
  89.   if (e=="key_down") then
  90.     running = false
  91.   end
  92. end
  93.  
  94. while running do
  95.   ok,err = pcall(handle)
  96.   if (not ok) then
  97.     computer.beep(2000,0.5)
  98.   end
  99.   print(err)
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement