Guest User

startup

a guest
Feb 18th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.69 KB | None | 0 0
  1. rednet.open('top')
  2. print('This is the master server')
  3.  
  4. local function receiveMessage()
  5.     local id,msg,d=rednet.receive()
  6.     rednet.send(id,true)
  7.     return id,msg,d
  8. end
  9.  
  10. local function sendMessage(id,msg)
  11.     rednet.send(id,msg,5)
  12.     local tid,tmsg,td=rednet.receive(5)
  13.     if tid==id and msg then
  14.         return true
  15.     else
  16.         return false
  17.     end
  18. end
  19.  
  20. local function createFile(name)
  21.     if not fs.exists(name) then
  22.         f=fs.open(name,'w')
  23.         f.writeLine('{}')
  24.         f.close()
  25.     end
  26. end
  27.  
  28. local function loadFromFile(variable,file)
  29.     local file_path=fs.open(file,'r')
  30.     local values=textutils.unserialize(file_path.readLine())
  31.     for i,v in pairs(values) do
  32.         if i==variable then
  33.             file_path.close()
  34.             return v
  35.         end
  36.     end
  37.     file_path.close()
  38.     return nil
  39. end
  40.  
  41. local function saveToFile(variable,value,file)
  42.     print('Saving to file ',file)
  43.     local file=file or 'config'
  44.     createFile(file)
  45.     local file_path=fs.open(file,'r')
  46.     local values=textutils.unserialize(file_path.readLine())
  47.     values[variable]=value
  48.     file_path.close()
  49.    
  50.     local file_path=fs.open(file,'w')
  51.     file_path.writeLine(textutils.serialize(values))
  52.     file_path.close()
  53. end
  54.  
  55. local function checkId(id)
  56.     if fs.exists("ids") then
  57.         local f = fs.open("ids", "r")
  58.         for line in f.readLine do
  59.             if line:find(id) then
  60.                 return true
  61.             end
  62.         end
  63.         return false
  64.     end
  65. end
  66.  
  67. local function checkValidIds(id)
  68.     local file=fs.open('server_ids','r')
  69.    
  70.     for _,v in pairs(file.readLine()) do
  71.         if id==v then
  72.             return true
  73.         end
  74.     end
  75.     for _,v in ipairs(loadFromFile('router_ids')) do
  76.         if id==v then
  77.             return true
  78.         end
  79.     end
  80.     if not checkId(id) then
  81.         return false
  82.     end
  83.     return false
  84. end
  85.  
  86. local function saveId(id)
  87.     if not checkId(id) then
  88.         local f=fs.open('ids','a')
  89.         f.writeLine(id..'')
  90.         f.close()
  91.         return true
  92.     else
  93.         return false
  94.     end
  95. end
  96.  
  97. local function rerout(values)
  98.     local server_id=loadFromFile(values['action'],'server_ids')
  99.     if not server_id then
  100.         print('requested sub-server does not exist')
  101.         return false
  102.     else
  103.         print(values['id'],' to ',server_id)
  104.         local msg=textutils.serialize(values)
  105.         sendMessage(server_id,msg)
  106.     end
  107. end
  108.  
  109. local function actionHandler(msg)
  110.     local values=textutils.unserialize(msg)
  111.     print('unserialized')
  112.     print('Action is ',values['action'])
  113.     if values['action']=='register' then
  114.         saveId(values['id'])
  115.     elseif values['action']=='registerSubServer' then
  116.         saveToFile(values['value'],values['id'],'server_ids')
  117.         print('Registered Sub-Server')
  118.     elseif values['action']=='registerComputer' then
  119.         if not checkId(values['id']) then
  120.             saveId(values['id'])
  121.             print('Adding computer ID # '..tostring(values['id'])..' to the id list')
  122.         end
  123.         local msg=textutils.serialize({id=os.getComputerID(),action='computerAdded'})
  124.         sendMessage(values['id'],msg)
  125.     elseif values['action']=='registerRouter' then
  126.         if not loadFromFile('router_ids','config') then
  127.             saveToFile('router_ids',{values['id']},'config')
  128.         else
  129.             local exists=false
  130.             for _,v in ipairs(loadFromFile('router_ids','config')) do
  131.                 if v==values['id'] then
  132.                     exists=true
  133.                 end
  134.             end
  135.             if not exists then
  136.                 local tmp=loadFromFile('router_ids','config')
  137.                 table.insert(tmp,values['id'])
  138.                 saveToFile('router_ids',tmp,'config')
  139.             end
  140.         end
  141.     elseif values['action']=='return' then
  142.         if checkId(values['id']) then
  143.             sendMessage(values['id'],msg)
  144.         else
  145.             for _,v in ipairs(loadFromFile('router_ids','config')) do
  146.                 sendMessage(v,msg)
  147.             end
  148.         end
  149.     else
  150.         print('rerouting')
  151.         rerout(values)
  152.     end
  153. end
  154.  
  155. createFile('server_ids')
  156. createFile('ids')
  157. print('This computer\'s ID is: '..tostring(os.getComputerID()))
  158.  
  159. while true do
  160.     local id,msg=receiveMessage()
  161.     print('Got a message')
  162.     actionHandler(msg)
  163. end
Advertisement
Add Comment
Please, Sign In to add comment