melzneni

sys2_service_unit

Feb 10th, 2020
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.17 KB | None | 0 0
  1. --sys2_service_unit
  2. os.loadAPI("sys/syslib")
  3.  
  4. --syslib_end
  5.  
  6. rednet.open("back")
  7.  
  8.  
  9. function identify()
  10.     local label = os.getComputerLabel()
  11.     if label == nil then
  12.         label = "<unnamed>"
  13.     end
  14.     rednet.broadcast("@root:identifyServiceUnit," .. label)
  15. end
  16.  
  17. print("<serviceUnit>")
  18.  
  19. identify()
  20. local rootId
  21. local bootUnitId
  22.  
  23. function main()
  24.     local id, msg = syslib.receiveRednet()
  25.     local tag, pts = syslib.getMsgData(msg)
  26.     if tag == "@serviceUnit" then
  27.         if pts[1] == "identifyToRoot" then
  28.             bootUnitId = id
  29.             rootId = pts[2]
  30.             identify()
  31.         elseif pts[1] == "setRootData" then
  32.             rootId = id
  33.             bootUnitId = pts[2]
  34.         elseif pts[1] == "action" then
  35.             if pts[2] == "setStartup" then
  36.                 print("setStartup")
  37.                 shell.run("delete", "startup")
  38.                 local f = fs.open("startup", "w")
  39.                 f.write("shell.run(\"delete\",\"unitHandler\")\n")
  40.                 f.write("shell.run(\"pastebin\",\"get\",\"" .. pts[3] .. "\",\"unitHandler\")\n")
  41.                 f.write("shell.run(\"unitHandler\")\n")
  42.                 f.close()
  43.             elseif pts[2] == "executePastebin" then
  44.                 shell.run("delete", "temp")
  45.                 shell.run("pastebin", "get", pts[3], "temp")
  46.                 shell.run("temp", rootId, bootUnitId)
  47.                 syslib.sendRednet(id, "@root:answer,executionTerminated")
  48.             elseif pts[2] == "setLabel" then
  49.                 syslib.log("root", "successfully renamed unit '" .. os.getComputerLabel() .. "' to '" .. pts[3] .. "'")
  50.                 os.setComputerLabel(pts[3])
  51.                 syslib.sendRednet(rootId, "@root:answer,renamedUnit," .. pts[3])
  52.             else print("unknown message>action: ", msg)
  53.             end
  54.         else print("unknown message: ", msg)
  55.         end
  56.     end
  57. end
  58.  
  59. while true do
  60.     local status, err = pcall(main)
  61.     if err == "Terminated" then
  62.         break
  63.     elseif not status then
  64.         print("error thrown: " .. err)
  65.         rednet.broadcast("@err:" .. os.getComputerID() .. "," .. os.getComputerLabel() .. ",serviceUnit")
  66.     end
  67. end
Add Comment
Please, Sign In to add comment