Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local MyNet,MyTab,MyID
- function loadTable(filename)
- MyNet=''
- MyTab={}
- MyID=os.getComputerID()
- local rTable={['*']=MyID}
- local file=fs.open(filename,'r')
- if not file then error('Can not open file '..filename) end
- local line=file.readLine()
- while line do
- local path,id=line:match('^%s-([_%w%.]+)%s+(%w+)')
- if path then
- local t=rTable
- for k in path:gmatch('([_%w]+)%.') do
- write(k..'.')
- if not t[k] then t[k]={}
- elseif type(t[k])=='number' then
- t[k]={[0]=t[k]}
- end
- t=t[k]
- end
- local k=path:match('([_%w]+)$')
- if id=='self' then
- MyNet=path
- if type(t[k])=='table' then t[k][0]=MyID
- else t[k]={[0]=MyID} end
- MyTab=t[k]
- else t[k]=tonumber(id) end
- print(k,'=',id)
- end
- line=file.readLine()
- end
- file.close()
- return rTable
- end
- function trace(t,ip)
- ip=ip..'.'
- for k in ip:gmatch('([_*%w]+)%.') do
- if not t[k] then return tonumber(k) end
- if type(t[k])=='table' then t=t[k]
- else return t[k] end
- end
- return t[0]
- end
- function open()
- for n,sSide in ipairs( rs.getSides() ) do
- if rednet.isOpen( sSide ) then
- return true
- end
- end
- for n,sSide in ipairs( rs.getSides() ) do
- if peripheral.getType( sSide )=='modem' then
- rednet.open(sSide)
- return true
- end
- end
- return false
- end
- if not open() then
- print('Modem is not found')
- return
- end
- rTable=loadTable('rtable')
- local sID,dID,mess,t,addr,command,data
- local commands={}
- commands.getip=function()
- addr=MyNet..'.'..sID
- for k,v in pairs(MyTab) do
- if v==sID then addr=MyNet..'.'..k break end
- end
- rednet.send(sID,'T='..t..'|D='..addr..'|S='..MyNet..'|setip')
- -- print(sID,' ','T='..t..'|D='..addr..'|S='..MyNet..'|setip')
- end
- commands.ping=function()
- dID=trace(rTable,addr)
- if dID then
- rednet.send(dID,'T='..t..'|D='..addr..'|S='..MyNet..'|pong')
- -- print(trace(rTable,addr),' T='..t..'|D='..addr..'|S='..MyNet..'|pong')
- end
- end
- commands.trace=function(path)
- dID=trace(rTable,path)
- if dID then
- rednet.send(dID,'T='..t..'|D=*|S='..addr..'|ping')
- -- print(dID,' T='..t..'|D=*|S='..addr..'|ping')
- end
- end
- while true do
- sID,mess=rednet.receive()
- -- print(sID,' ',mess)
- t,addr,mess=mess:match('T=(%d+)|D=([_*%w%.]+)|(.+)')
- if t then
- t=tonumber(t)
- if t>0 then
- dID=trace(rTable,addr)
- -- print(addr,' ',dID)
- if dID then
- if dID==MyID then
- addr,command,data=mess:match('S=([_*%w%.]+)|%s-([_%w]+)=?(.*)')
- if commands[command] then commands[command](data) end
- else
- if t>30 then t=30 end
- t=t-1
- rednet.send(dID,'T='..t..'|D='..addr..'|'..mess)
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement