SHARE
TWEET

link.lua

a guest Oct 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require "component"
  2. local event = require "event"
  3.  
  4. local link = {
  5.   ["Link"] = {},
  6.   ["nextHope"] = {},
  7.   ["callback"] = {},
  8.   ["addLink"] =
  9.   function (self,localAddress, remoteAddress )
  10.     if self.Link[ remoteAddress ] then
  11.       table.insert( self.Link[ remoteAddress ] localAddress )
  12.     else
  13.       self.Link[ remoteAddress ] = { localAddress }
  14.     end
  15.     if self.Link[ localAddress ] then
  16.       table.insert( self.Link[ localAddress ], remoteAddress )
  17.     else
  18.       self.Link[ localAddress ] = { remoteAddress }
  19.     end
  20.   end,
  21.   ["discoverLink"] =
  22.   function( self )
  23.     for address,_ in pairs( component.list("modem",true) ) do
  24.       local modem = component.proxy( address )
  25.       if modem.isWireless() then
  26.         modem.setStrength( 256 )
  27.       end
  28.       for i,v in pairs( self.PORT ) do
  29.         modem.open( v )
  30.       end
  31.       modem.broadcast( self.PORT.discovery_request, "discovery_request", address )
  32.     end
  33.     for address,_ in pairs( component.list("tunnel",true) ) do
  34.       local tunnel = component.proxy( "tunnel" )
  35.       tunnel.send( self.PORT.discovery_request, "discovery_request", address )
  36.     end
  37.   end,
  38.   ["discoverRoute"] =
  39.   function( self )
  40.    
  41.   end,
  42.   ["addDatagram"] =
  43.   function ( self, name, port, callback )
  44.     if self.port.by_port[ port ] then return false
  45.     self.port.by_port[ port ] = name
  46.     self.callback[ name ] = callback
  47.     return true
  48.   end
  49.   ["listen"] =
  50.   function( self )
  51.     local e = {10,event.pull("modem_message")}
  52.     name = e[1]
  53.     if name ~= "modem_message" then return end
  54.     receiver = e[2]
  55.     sender = e[3]
  56.     port = e[4]
  57.     local isTunnel = component.proxy(receiver).type == "tunnel"
  58.    
  59.     if isTunnel then
  60.       port = e[6]
  61.     end
  62.    
  63.     args = {table.unpack( e, 6 )}
  64.     if isTunnel then args = {table.unpack( e, 7 )}
  65.    
  66.     local validDatagram = false
  67.    
  68.     for i,v in pairs( self.port.by_Port ) do
  69.       if i == port and args[1] == v then
  70.         validDatagram = true
  71.         break
  72.       end
  73.     end
  74.    
  75.     if not validDatagram then return end
  76.    
  77.     self.callback[ args[1] ]( table.unpack( args, 2 ) )
  78.    
  79.     if port == self.PORT["link_discovery_request"] then
  80.       if args[1] ~= "link_discovery_request" or sender ~= args[2] then return end
  81.       self:addLink( receiver, sender )
  82.       component.invoke( receiver ,"send", sender, self.PORT.discovery_response, "link_discovery_response", receiver )
  83.       return
  84.     end
  85.     if port == self.PORT["link_discovery_response"] then
  86.       if args[1] ~= "link_discovery_response" or sender ~= args[2] then return end
  87.       self:addLink( receiver, sender )
  88.       return
  89.     end
  90.   end,
  91.   ["send"] =
  92.   function( self, addr, port, ... )
  93.     local next_hope = self.NextHope[ addr ] or addr
  94.     local interface = Link[ next_hope ]
  95.   end,
  96.   ["port"] =
  97.   {
  98.     ["by_Name"] =
  99.     {
  100.       ["link_discovery_request"] = 25652,
  101.       ["link_discovery_response"] = 65256
  102.     },
  103.     ["by_Port"] = {}
  104.    
  105.   }
  106. }
  107.  
  108. return link
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top