ZNZNCOOP

route

May 15th, 2014
208
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local MyNet,MyTab,MyID
  2. function loadTable(filename)
  3.   MyNet=''
  4.   MyTab={}
  5.   MyID=os.getComputerID()
  6.   local rTable={['*']=MyID}
  7.   local file=fs.open(filename,'r')
  8.   if not file then error('Can not open file '..filename) end
  9.   local line=file.readLine()
  10.   while line do
  11.     local path,id=line:match('^%s-([_%w%.]+)%s+(%w+)')
  12.     if path then
  13.       local t=rTable
  14.       for k in path:gmatch('([_%w]+)%.') do
  15.         write(k..'.')
  16.         if not t[k] then t[k]={}
  17.         elseif type(t[k])=='number' then
  18.           t[k]={[0]=t[k]}
  19.         end
  20.         t=t[k]
  21.       end
  22.       local k=path:match('([_%w]+)$')
  23.       if id=='self' then
  24.         MyNet=path
  25.         if type(t[k])=='table' then t[k][0]=MyID
  26.         else t[k]={[0]=MyID} end
  27.         MyTab=t[k]
  28.       else t[k]=tonumber(id) end
  29.       print(k,'=',id)
  30.     end
  31.     line=file.readLine()
  32.   end
  33.   file.close()
  34.   return rTable
  35. end
  36.  
  37. function trace(t,ip)
  38.   ip=ip..'.'
  39.   for k in ip:gmatch('([_*%w]+)%.') do
  40.     if not t[k] then return tonumber(k) end
  41.     if type(t[k])=='table' then t=t[k]
  42.     else return t[k] end
  43.   end
  44.   return t[0]
  45. end
  46.  
  47. function open()
  48.     for n,sSide in ipairs( rs.getSides() ) do
  49.         if rednet.isOpen( sSide ) then
  50.           return true
  51.         end
  52.     end
  53.     for n,sSide in ipairs( rs.getSides() ) do
  54.         if peripheral.getType( sSide )=='modem' then
  55.           rednet.open(sSide)
  56.           return true
  57.         end
  58.     end
  59.     return false
  60. end
  61.  
  62. if not open() then
  63.   print('Modem is not found')
  64.   return
  65. end
  66.  
  67.  rTable=loadTable('rtable')
  68. local sID,dID,mess,t,addr,command,data
  69. local commands={}
  70. commands.getip=function()
  71.     addr=MyNet..'.'..sID
  72.     for k,v in pairs(MyTab) do
  73.       if v==sID then addr=MyNet..'.'..k break end
  74.     end
  75.     rednet.send(sID,'T='..t..'|D='..addr..'|S='..MyNet..'|setip')
  76. --    print(sID,' ','T='..t..'|D='..addr..'|S='..MyNet..'|setip')
  77. end
  78.  
  79. commands.ping=function()
  80.     dID=trace(rTable,addr)
  81.     if dID then
  82.     rednet.send(dID,'T='..t..'|D='..addr..'|S='..MyNet..'|pong')
  83. --    print(trace(rTable,addr),' T='..t..'|D='..addr..'|S='..MyNet..'|pong')
  84.     end
  85. end
  86.  
  87. commands.trace=function(path)
  88.     dID=trace(rTable,path)
  89.     if dID then
  90.     rednet.send(dID,'T='..t..'|D=*|S='..addr..'|ping')
  91. --    print(dID,' T='..t..'|D=*|S='..addr..'|ping')
  92.     end
  93. end
  94.  
  95. while true do
  96.   sID,mess=rednet.receive()
  97. --  print(sID,' ',mess)
  98.   t,addr,mess=mess:match('T=(%d+)|D=([_*%w%.]+)|(.+)')
  99.   if t then
  100.     t=tonumber(t)
  101.     if t>0 then
  102.       dID=trace(rTable,addr)
  103. --      print(addr,' ',dID)
  104.       if dID then
  105.         if dID==MyID then
  106.           addr,command,data=mess:match('S=([_*%w%.]+)|%s-([_%w]+)=?(.*)')
  107.           if commands[command] then commands[command](data) end
  108.         else
  109.           if t>30 then t=30 end
  110.           t=t-1
  111.           rednet.send(dID,'T='..t..'|D='..addr..'|'..mess)
  112.         end
  113.       end
  114.     end
  115.   end
  116. end
RAW Paste Data