Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --sys2_boot_unit
- os.loadAPI("sys/syslib")
- --syslib end
- rednet.open("back")
- function searchForUnits()
- local units = {}
- if peripheral.isPresent("left") and peripheral.getType("left") == "computer" then
- units[peripheral.call("left", "getID")] = "left"
- end
- if peripheral.isPresent("right") and peripheral.getType("right") == "computer" then
- units[peripheral.call("right", "getID")] = "right"
- end
- if peripheral.isPresent("top") and peripheral.getType("top") == "computer" then
- units[peripheral.call("top", "getID")] = "top"
- end
- if peripheral.isPresent("bottom") and peripheral.getType("bottom") == "computer" then
- units[peripheral.call("bottom", "getID")] = "bottom"
- end
- return units
- end
- local connectedUnits = searchForUnits()
- function identify()
- local label = os.getComputerLabel()
- if label == nil then
- label = "<unnamed>"
- end
- local txtUnits = ""
- for id, side in pairs(connectedUnits) do
- if txtUnits ~= "" then
- txtUnits = txtUnits .. "|"
- end
- txtUnits = txtUnits .. id
- end
- rednet.broadcast("@root:identifyBootUnit," .. label .. "," .. txtUnits)
- end
- print("<bootUnit>")
- identify()
- local rootId
- function main()
- local id, msg = syslib.receiveRednet()
- local tag, pts = syslib.getMsgData(msg)
- if tag == "@bootUnit" then
- if pts[1] == "initialise" then
- rootId = id
- identify()
- local connectedUnits = searchForUnits()
- for id, side in pairs(connectedUnits) do
- syslib.sendRednet(id, "@serviceUnit:identifyToRoot," .. rootId)
- end
- elseif pts[1] == "reboot" then
- if pts[2] == nil then
- local connectedUnits = searchForUnits()
- for id, side in pairs(connectedUnits) do
- peripheral.call(side, "reboot")
- end
- os.reboot()
- else
- for id, side in pairs(connectedUnits) do
- if id == tonumber(pts[2]) then
- syslib.log("root", "rebooting unit " .. id .. " (label '" .. pts[3] .. "')")
- peripheral.call(side, "reboot")
- end
- end
- end
- elseif pts[1] == "action" then
- if pts[2] == "setStartup" then
- shell.run("delete", "startup")
- local f = fs.open("startup", "w")
- f.write("shell.run(\"delete\",\"unitHandler\")\n")
- f.write("shell.run(\"pastebin\",\"get\",\"" .. pts[3] .. "\",\"unitHandler\")\n")
- f.write("shell.run(\"unitHandler\")\n")
- f.close()
- else print("unknown message>action: ", msg)
- end
- else print("unknown message: ", msg)
- end
- end
- end
- local cErr = 0
- while true do
- local status, err = pcall(main)
- if err == "Terminated" then
- break
- elseif not status then
- print("error thrown: " .. err)
- sleep(3)
- cErr = cErr + 1
- if cErr == 3 then
- os.reboot()
- end
- rednet.broadcast("@err:" .. os.getComputerID() .. "," .. os.getComputerLabel() .. ",bootUnit")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement