Rusettsten

DanetOS-MainServer

Nov 29th, 2020 (edited)
515
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Set functions first, then do main instructions
  2. PROTOCOL = "danetos3"
  3. turtleNum = 0
  4. idList = {}
  5. jobList = {}
  6. homeList = {}
  7.  
  8. function lookupHome(sender) --Looks up the home from the table
  9.     for x=1,turtleNum-1 do
  10.         if idList[x] == sender then
  11.             return (homeList[x])
  12.         end
  13.     end
  14.     print("ERROR! Could not find home for Computer " .. sender)
  15. end
  16.  
  17. function lookupJob(sender)
  18.     for x=1,turtleNum-1 do
  19.         if idList[x] == sender then
  20.             return (jobList[x])
  21.         end
  22.     end
  23.     print("ERROR! Could not find job for Computer " .. sender)
  24. end
  25.  
  26.  
  27. function receiveRequests()
  28.     sender,message = rednet.receive("danetos3")
  29.    
  30.     if message == "getHome" then --If a turtle asks for its home <3
  31.         home = lookupHome(tostring(sender))
  32.         rednet.send(sender,home,PROTOCOL)
  33.         print("Send home information to Computer " .. sender)
  34.     elseif message == "getJob" then --If a turtle asks for its job
  35.         job = lookupJob(tostring(sender))
  36.         rednet.send(sender,job,PROTOCOL)
  37.         print("Sent job information to Computer " .. sender)
  38.     end
  39. end
  40.  
  41. function getData()
  42.     local fileData = {} --Declare a table for the file data
  43.     for line in io.lines("danetos3/data.danet") do-- Start a loop to read the data until no data is left
  44.         table.insert(fileData,line)
  45.     end
  46.     print("SUCCESS! Data files read to table.")
  47.    
  48.     --Data parsing
  49.     turtleNum = fileData[1]
  50.     local readNum = turtleNum * 3
  51.     for x=1,readNum,3 do
  52.         idList[x] = fileData[x+1]
  53.         jobList[x] = fileData[x+2]
  54.         homeList[x] = fileData[x+3]
  55.     end
  56. end
  57.  
  58. --MAIN INSTRUCTIONS GO HERE
  59.  
  60. shell.run("clear")
  61. print("DanetOS3 MainServer!")
  62. getData() --Get data from file system
  63.  
  64. rednet.open("right")
  65. rednet.host(PROTOCOL,"MainServer")
  66.  
  67. while(true) do --Wait for clients
  68.     receiveRequests()
  69. end
  70.  
RAW Paste Data