Advertisement
Tatantyler

Rednet Network Boot System - Server

Sep 2nd, 2012
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. local bootFileDir = "boot/"
  2. local programFileDir = "programs/"
  3. local apiFileDir = "apis/"
  4. local side = "top"
  5. --local bootFiles = {}
  6. local apis = {}
  7. local apinames = {}
  8. local authenticated = {}
  9. local debugMode = true
  10. local debugModeVerbose = false
  11. local logMessages = true
  12. local authenticationRequired = true
  13. local password = "test"
  14.  
  15. rednet.open(side)
  16.  
  17. function debugPrint(message)
  18.     if debugMode then
  19.         print(message)
  20.     end
  21.     if logMessages then
  22.         local fileHandle = fs.open("bootserv.log", "a")
  23.         fileHandle.writeLine("["..textutils.formatTime(os.time(), true).."]:"..message)
  24.         fileHandle.close()
  25.     end
  26. end
  27.  
  28. function returnFileExt(file)
  29.     local dotLocation = string.find(file, ".")
  30.     if dotLocation == nil then
  31.         return nil
  32.     else
  33.         return string.sub(file, dotLocation+1)
  34.     end
  35. end
  36.  
  37. if debugModeVerbose then
  38.     for index, value in ipairs(fs.list("boot/")) do
  39.         debugPrint(index..". "..value)
  40.     end
  41. end
  42.  
  43. --[[
  44. for index, value in ipairs(fs.list("boot/")) do
  45.     if string.find(value, ":") == nil then
  46.         table.insert(bootFiles, value)
  47.         if debugModeVerbose then
  48.             print("BOOT FILE FOUND: "..value)
  49.         end
  50.     end
  51. end
  52.  
  53. ]]--
  54.  
  55. while true do
  56.     senderID, message, distance = rednet.receive()
  57.     if debugModeVerbose then
  58.         print("DEBUG REDNET MESSAGE:"..senderID..":"..message)
  59.     end
  60.     if message == "authrequired" then
  61.         rednet.send(senderID,"authReply:"..tostring(authenticationRequired))
  62.     end
  63.     if string.sub(message,1,13) == "authenticate:" then
  64.         if string.sub(message,14) == password then
  65.             table.insert(authenticated, senderID)
  66.             debugPrint("Computer "..senderID.." successfully authenticated.")
  67.             rednet.send(senderID,"authReply:true")
  68.         else
  69.             debugPrint("Computer "..senderID.." failed to authenticate.")
  70.             rednet.send(senderID,"authReply:false")
  71.         end
  72.     end
  73.     --[[
  74.     if message == "list" then
  75.         debugPrint("Sending computer "..senderID.." the list of boot files...")
  76.         rednet.send(senderID, "list:"..textutils.serialize(bootFiles))
  77.     end
  78.     ]]--
  79.        
  80.     if string.sub(message,1,12) == "bootrequest:" then
  81.         for index, value in ipairs(authenticated) do
  82.             if value == senderID then
  83.                 isAuthenticated = true
  84.                 break
  85.             else
  86.                 isAuthenticated = false
  87.             end
  88.         end
  89.         if isAuthenticated then
  90.             local bootFile = string.sub(message,13)
  91.             local bootFileHandle = fs.open(fs.combine(bootFileDir,bootFile), "r")
  92.             local apiFile = fs.combine(bootFileDir, bootFile..".apis")
  93.             local programFile = fs.combine(bootFileDir, bootFile..".progs")
  94.             local apiFileHandle = fs.open(apiFile, "r")
  95.             local programFileHandle = fs.open(programFile, "r")
  96.             local apiFileLines = {}
  97.             local programFileLines = {}
  98.             local apiFileLine = nil
  99.             local programFileLine = nil
  100.            
  101.            
  102.             debugPrint("Boot request recieved.")
  103.             debugPrint("FILE STATUS:")
  104.             debugPrint("bootFile: "..bootFile.." Exists: "..tostring(fs.exists(fs.combine(bootFileDir, bootFile))))
  105.             debugPrint("apiFile: "..apiFile.." Exists: "..tostring(fs.exists(apiFile)))
  106.             debugPrint("programFile: "..programFile.." Exists: "..tostring(fs.exists(programFile)))
  107.            
  108.             if fs.exists(apiFile) then
  109.                 while true do
  110.                     local apiFileLine = apiFileHandle.readLine()
  111.                     if apiFileLine == nil then
  112.                         break
  113.                     else
  114.                         table.insert(apiFileLines, apiFileLine)
  115.                     end
  116.                 end
  117.             end
  118.            
  119.             debugPrint("API list retrieved.")
  120.             debugPrint("APIs:")
  121.             for index, value in ipairs(apiFileLines) do
  122.                 debugPrint(index..". "..value)
  123.             end
  124.             debugPrint(" ")
  125.            
  126.             apiFileHandle.close()
  127.            
  128.             if fs.exists(programFile) then
  129.                 while true do
  130.                     local programFileLine = programFileHandle.readLine()
  131.                     if programFileLine == nil then
  132.                         programFileHandle.close()
  133.                         break
  134.                     else
  135.                         table.insert(programFileLines, programFileLine)
  136.                     end
  137.                 end
  138.             end
  139.            
  140.             debugPrint("Program list retrieved:")
  141.             debugPrint("Programs:")
  142.             for index, value in ipairs(programFileLines) do
  143.                 debugPrint(index..". "..value)
  144.             end
  145.             debugPrint(" ")
  146.            
  147.             programFileHandle.close()
  148.            
  149.             if not fs.exists(apiFile) then
  150.                 apiFileLines = nil
  151.             end
  152.            
  153.             if not fs.exists(programFile) then
  154.                 programFileLines = nil
  155.             end
  156.            
  157.             for index, value in ipairs(apiFileLines) do
  158.                 debugPrint("Opening API: "..value)
  159.                 apiFileHandle = fs.open(fs.combine(apiFileDir,value), "r")
  160.                 debugPrint("Sending API: "..value)
  161.                 rednet.send(senderID, "api:"..value..":"..apiFileHandle.readAll())
  162.                 apiFileHandle.close()
  163.                 os.sleep(0.5)
  164.             end
  165.            
  166.             for index, value in ipairs(programFileLines) do
  167.                 debugPrint("Opening program: "..value)
  168.                 programFileHandle = fs.open(fs.combine(programFileDir, value), "r")
  169.                 debugPrint("Sending Program: "..value)
  170.                 rednet.send(senderID, "program:"..value..":"..programFileHandle.readAll())
  171.                 programFileHandle.close()
  172.                 os.sleep(0.5)
  173.             end
  174.            
  175.             debugPrint("APIs and programs sent...")
  176.             debugPrint("Sending boot file...")
  177.            
  178.             os.sleep(2)
  179.            
  180.             if bootFileHandle == nil or not fs.exists(fs.combine(bootFileDir, bootFile)) then
  181.                 print("CRITICAL ERROR: bootFileHandle == nil!")
  182.                 print("Does the boot file exist? > "..tostring(fs.exists(fs.combine(bootFileDir,bootFile))))
  183.                 break
  184.             end
  185.            
  186.             rednet.send(senderID, "boot:"..bootFileHandle.readAll())
  187.            
  188.             debugPrint("Computer "..senderID.." booted from file ".. bootFile.." at "..textutils.formatTime(os.time(), true))
  189.         end
  190.     end
  191.    
  192. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement