Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local bootFileDir = "boot/"
- local programFileDir = "programs/"
- local apiFileDir = "apis/"
- local side = "top"
- --local bootFiles = {}
- local apis = {}
- local apinames = {}
- local authenticated = {}
- local debugMode = true
- local debugModeVerbose = false
- local logMessages = true
- local authenticationRequired = true
- local password = "test"
- rednet.open(side)
- function debugPrint(message)
- if debugMode then
- print(message)
- end
- if logMessages then
- local fileHandle = fs.open("bootserv.log", "a")
- fileHandle.writeLine("["..textutils.formatTime(os.time(), true).."]:"..message)
- fileHandle.close()
- end
- end
- function returnFileExt(file)
- local dotLocation = string.find(file, ".")
- if dotLocation == nil then
- return nil
- else
- return string.sub(file, dotLocation+1)
- end
- end
- if debugModeVerbose then
- for index, value in ipairs(fs.list("boot/")) do
- debugPrint(index..". "..value)
- end
- end
- --[[
- for index, value in ipairs(fs.list("boot/")) do
- if string.find(value, ":") == nil then
- table.insert(bootFiles, value)
- if debugModeVerbose then
- print("BOOT FILE FOUND: "..value)
- end
- end
- end
- ]]--
- while true do
- senderID, message, distance = rednet.receive()
- if debugModeVerbose then
- print("DEBUG REDNET MESSAGE:"..senderID..":"..message)
- end
- if message == "authrequired" then
- rednet.send(senderID,"authReply:"..tostring(authenticationRequired))
- end
- if string.sub(message,1,13) == "authenticate:" then
- if string.sub(message,14) == password then
- table.insert(authenticated, senderID)
- debugPrint("Computer "..senderID.." successfully authenticated.")
- rednet.send(senderID,"authReply:true")
- else
- debugPrint("Computer "..senderID.." failed to authenticate.")
- rednet.send(senderID,"authReply:false")
- end
- end
- --[[
- if message == "list" then
- debugPrint("Sending computer "..senderID.." the list of boot files...")
- rednet.send(senderID, "list:"..textutils.serialize(bootFiles))
- end
- ]]--
- if string.sub(message,1,12) == "bootrequest:" then
- for index, value in ipairs(authenticated) do
- if value == senderID then
- isAuthenticated = true
- break
- else
- isAuthenticated = false
- end
- end
- if isAuthenticated then
- local bootFile = string.sub(message,13)
- local bootFileHandle = fs.open(fs.combine(bootFileDir,bootFile), "r")
- local apiFile = fs.combine(bootFileDir, bootFile..".apis")
- local programFile = fs.combine(bootFileDir, bootFile..".progs")
- local apiFileHandle = fs.open(apiFile, "r")
- local programFileHandle = fs.open(programFile, "r")
- local apiFileLines = {}
- local programFileLines = {}
- local apiFileLine = nil
- local programFileLine = nil
- debugPrint("Boot request recieved.")
- debugPrint("FILE STATUS:")
- debugPrint("bootFile: "..bootFile.." Exists: "..tostring(fs.exists(fs.combine(bootFileDir, bootFile))))
- debugPrint("apiFile: "..apiFile.." Exists: "..tostring(fs.exists(apiFile)))
- debugPrint("programFile: "..programFile.." Exists: "..tostring(fs.exists(programFile)))
- if fs.exists(apiFile) then
- while true do
- local apiFileLine = apiFileHandle.readLine()
- if apiFileLine == nil then
- break
- else
- table.insert(apiFileLines, apiFileLine)
- end
- end
- end
- debugPrint("API list retrieved.")
- debugPrint("APIs:")
- for index, value in ipairs(apiFileLines) do
- debugPrint(index..". "..value)
- end
- debugPrint(" ")
- apiFileHandle.close()
- if fs.exists(programFile) then
- while true do
- local programFileLine = programFileHandle.readLine()
- if programFileLine == nil then
- programFileHandle.close()
- break
- else
- table.insert(programFileLines, programFileLine)
- end
- end
- end
- debugPrint("Program list retrieved:")
- debugPrint("Programs:")
- for index, value in ipairs(programFileLines) do
- debugPrint(index..". "..value)
- end
- debugPrint(" ")
- programFileHandle.close()
- if not fs.exists(apiFile) then
- apiFileLines = nil
- end
- if not fs.exists(programFile) then
- programFileLines = nil
- end
- for index, value in ipairs(apiFileLines) do
- debugPrint("Opening API: "..value)
- apiFileHandle = fs.open(fs.combine(apiFileDir,value), "r")
- debugPrint("Sending API: "..value)
- rednet.send(senderID, "api:"..value..":"..apiFileHandle.readAll())
- apiFileHandle.close()
- os.sleep(0.5)
- end
- for index, value in ipairs(programFileLines) do
- debugPrint("Opening program: "..value)
- programFileHandle = fs.open(fs.combine(programFileDir, value), "r")
- debugPrint("Sending Program: "..value)
- rednet.send(senderID, "program:"..value..":"..programFileHandle.readAll())
- programFileHandle.close()
- os.sleep(0.5)
- end
- debugPrint("APIs and programs sent...")
- debugPrint("Sending boot file...")
- os.sleep(2)
- if bootFileHandle == nil or not fs.exists(fs.combine(bootFileDir, bootFile)) then
- print("CRITICAL ERROR: bootFileHandle == nil!")
- print("Does the boot file exist? > "..tostring(fs.exists(fs.combine(bootFileDir,bootFile))))
- break
- end
- rednet.send(senderID, "boot:"..bootFileHandle.readAll())
- debugPrint("Computer "..senderID.." booted from file ".. bootFile.." at "..textutils.formatTime(os.time(), true))
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement