Advertisement
Tatantyler

Rednet Network Boot System - Client

Sep 2nd, 2012
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.40 KB | None | 0 0
  1. local timeout = 10
  2. local apis = {}
  3. local side = "top"
  4. local version = 1.0
  5. local bootFile = nil
  6. local bootFiles = {}
  7. local server, message, distance = nil
  8. local verbose = false
  9. local options_bootServer, options_default = nil
  10.  
  11. rednet.open(side)
  12.  
  13. function loadAPIs()
  14.     if #apis == 0 then
  15.         print("No APIs need to be loaded.")
  16.         return
  17.     end
  18.     local apiList = fs.open("apiList", "w")
  19.     print("Loading APIs...")
  20.     for index, value in ipairs(apis) do
  21.         io.write("Loading API: "..value.."...")
  22.         os.sleep(fs.getSize(fs.combine("apis/", value)) / 500 + 0.5)
  23.         io.write(tostring(os.loadAPI(fs.combine("apis/", value))).."\n")
  24.         apiList.writeLine(value)
  25.         os.sleep(0.5)
  26.     end
  27.     apiList.close()
  28. end
  29.  
  30.  
  31. function doBoot()
  32.     rednet.send(server, "bootrequest:"..bootFile)
  33.     while true do
  34.         server, message, distance = rednet.receive(timeout)
  35.         if verbose then
  36.             print("DEBUG:"..server..":"..message)
  37.         end
  38.         if string.sub(message, 1, 5) == "boot:" then -- boot file code
  39.             print("Boot file recieved!")
  40.             local bootFile = string.sub(message, 6)
  41.             local bootFileHandle = fs.open("shell", "w")
  42.             bootFileHandle.write(bootFile)
  43.             bootFileHandle.close()
  44.             loadAPIs()
  45.             print("Running shell...")
  46.             os.sleep(2)
  47.             rednet.close(side)
  48.             term.clear()
  49.             term.setCursorPos(1,1)
  50.             os.sleep(1)
  51.             shell.run("shell")
  52.             return
  53.         end
  54.         if string.sub(message, 1,4) == "api:" then
  55.             local apiName = string.sub(message,5,string.find(message,":",5)-1)
  56.             local apiContent = string.sub(message,string.find(message,":",5)+1)
  57.             print("Custom API recieved: "..apiName)
  58.             table.insert(apis, apiName)
  59.             if not fs.exists("apis") then
  60.                 fs.makeDir("apis")
  61.             end
  62.             local apiFileHandle = fs.open(fs.combine("apis/",apiName), "w")
  63.             apiFileHandle.write(apiContent)
  64.             apiFileHandle.close()
  65.         end
  66.         if string.sub(message, 1,8) == "program:" then
  67.             local programName = string.sub(message,9,string.find(message,":",9)-1)
  68.             local programContent = string.sub(message,string.find(message,":",9)+1)
  69.             print("Program recieved: "..programName)
  70.             local programFileHandle = fs.open(programName, "w")
  71.             programFileHandle.write(programContent)
  72.             programFileHandle.close()
  73.         end
  74.     end
  75. end
  76.  
  77. function lastBootFiles()
  78.     os.sleep(2)
  79.     local apiFile = fs.open("apiList", "r")
  80.     local apiLine = nil
  81.     while true do
  82.         apiLine = apiFile.readLine()
  83.         if apiLine == nil then
  84.             break
  85.         else
  86.             print("Found API: "..apiLine)
  87.             table.insert(apis, apiLine)
  88.         end
  89.     end
  90.     apiFile.close()
  91.     loadAPIs()
  92.     print("Running shell...")
  93.     os.sleep(2)
  94.     rednet.close(side)
  95.     term.clear()
  96.     term.setCursorPos(1,1)
  97.     os.sleep(1)
  98.     shell.run("shell")
  99.     assert(1==2)
  100. end
  101.  
  102. print("Network Boot Client V"..tostring(version))
  103. print("Options:")
  104. print("1. Default boot")
  105. print("2. Select a boot file")
  106. print("3. Use last boot file/APIs")
  107. print("4. Options")
  108. print("5. Shutdown")
  109.  
  110. if fs.exists("bootClient.options") then
  111.     local optionsHandle = fs.open("bootClient.options", "r")
  112.     options_bootServer = tonumber(optionsHandle.readLine())
  113.     options_default = optionsHandle.readLine()
  114. else
  115.     optionsHandle = fs.open("bootClient.options", "w")
  116.     optionsHandle.writeLine("-1")
  117.     optionsHandle.writeLine("default")
  118.     optionsHandle.close()
  119. end
  120.  
  121. while true do
  122.     event, key = os.pullEvent("key")
  123.     if key == 2 then
  124.         bootFile = options_default
  125.         break
  126.     elseif key == 3 then
  127.     --[[
  128.         rednet.broadcast("list")
  129.         while true do
  130.             server, message, distance = rednet.receive(timeout)
  131.             if message == nil then
  132.                 print("Could not contact boot server.")
  133.                 os.sleep(1)
  134.                 os.shutdown()
  135.             elseif string.sub(message,1,5) == "list:" then
  136.                 bootFiles = {}
  137.                 bootFiles = textutils.unserialize(string.sub(message,6))
  138.                 break
  139.             end
  140.         end
  141.         print("BOOT FILES:")
  142.         for index, value in ipairs(bootFiles) do
  143.             print(index..". "..value)
  144.         end
  145.         ]]--
  146.         print(" ")
  147.         print("Type in a boot file: ")
  148.         bootFile = io.read()
  149.         break
  150.     elseif key == 4 then
  151.         print("Loading files...")
  152.         lastBootFiles()
  153.     elseif key == 5 then
  154.         optionsHandle = fs.open("bootClient.options", "w")
  155.         print("Configure this client:")
  156.         print("Boot server (Set to -1 to automatically find a boot server on startup):")
  157.         local options_bootServer = io.read()
  158.         print("Default file (This is the file to load when you select option 1 in the menu):")
  159.         local options_default = io.read()
  160.         optionsHandle.writeLine(options_bootServer)
  161.         optionsHandle.writeLine(options_default)
  162.         optionsHandle.close()
  163.         print("Configured. Rebooting...")
  164.         os.sleep(2)
  165.         os.reboot()
  166.     elseif key == 6 then
  167.         print("Shutting down...")
  168.         os.sleep(2)
  169.         os.shutdown()
  170.     end
  171. end
  172.  
  173. if options_bootServer < 0 then
  174.     rednet.broadcast("authrequired")
  175. else
  176.     rednet.send(options_bootServer, "authrequired")
  177. end
  178. server, message, distance = rednet.receive(timeout)
  179. if server ~= nil then
  180.     print("Boot server found. ID:"..server)
  181. else
  182.     print("CRITICAL ERROR: A boot server could be found!")
  183.     os.sleep(2)
  184.     os.shutdown()
  185. end
  186.  
  187. if message == "authReply:true" then
  188.     print("The boot server requires a password to boot.")
  189.     print("Type it in now:")
  190.     local password = io.read()
  191.     rednet.send(server, "authenticate:"..password)
  192.     server, message, distance = rednet.receive(timeout)
  193.     if message == "authReply:true" then
  194.         print("Password correct!")
  195.         doBoot()
  196.     else
  197.         print("Incorrect password.")
  198.         print("Shutting down...")
  199.         os.sleep(2)
  200.         os.shutdown()
  201.     end
  202. else
  203.     print("The boot server does not require a password to boot.")
  204. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement