Advertisement
IronicPickle

syncMaster.lua

Feb 27th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.57 KB | None | 0 0
  1. -- Args
  2. local args = { ... }
  3. local repoDir = args[1] or "/lua"
  4. local channel = tonumber(args[2]) or 99
  5.  
  6. -- Libraries
  7. local setup = require("/lua/lib/setupUtils")
  8. local monUtils = require("/lua/lib/monitorUtils")
  9. local write = monUtils.write
  10. local drawBox = monUtils.drawBox
  11.  
  12. -- Peripherals
  13. local wrappedPers = setup.getPers({
  14.     "monitor",
  15.     "modem"
  16. })
  17. local monitor = setup.setupMonitor(
  18.     wrappedPers.monitor[1], 0.5
  19. )
  20. local modem = wrappedPers.modem[1]
  21.  
  22. -- Setup
  23. local devices = {}
  24. local blacklist = {"lua/state"}
  25.  
  26. -- Windows
  27. local winHeader = setup.setupWindow(
  28.     monitor, 1, 1, monitor.x, 6
  29. )
  30. local winFooter = setup.setupWindow(
  31.     monitor, 1, (monitor.y - 3), monitor.x, 4
  32. )
  33. local winMain = setup.setupWindow(
  34.     monitor, 1, 7, monitor.x, (monitor.y - (6 + 4))
  35. )
  36.  
  37. -- Main
  38. function start()
  39.     term.clear()
  40.     term.setCursorPos(1, 1)
  41.     print("\n# Network Sync Started")
  42.     print("- Type: Master")
  43.     print("\n# Sync Config:")
  44.     print("- Respository Directory:", repoDir)
  45.     print("- Channel:", channel)
  46.     print("\n# Sync Controls:")
  47.     print("- Up - Pushes respository update to all devices")
  48.     print("- Del - Restarts all wrapped programs")
  49.     print("- End - Restarts all devices\n")
  50.    
  51.     backupToDisk()
  52.    
  53.     modem.open(channel)
  54.    
  55.     drawHeader("Nothing")
  56.     drawFooter()
  57.     drawMain()
  58.    
  59.     parallel.waitForAny(await, networkPoller)
  60. end
  61.  
  62. function await()
  63.     while(true) do
  64.         local event, p1, p2, p3, p4, p5 = os.pullEvent()
  65.        
  66.         local isDelKey = (event == "key" and p1 == 211)
  67.         local isEndKey = (event == "key" and p1 == 207)
  68.         local isUpKey = (event == "key" and p1 == 200)
  69.        
  70.         local isTouch = (event == "monitor_touch")
  71.        
  72.         local isModemMessage = (event == "modem_message")
  73.        
  74.         if(isDelKey) then
  75.             globalRestart()
  76.         elseif(isEndKey) then
  77.             globalHardRestart()
  78.         elseif(isUpKey) then
  79.             globalPush()
  80.         elseif(isTouch) then
  81.             local x = p2
  82.             local y = p3 - winHeader.y
  83.             local half = winMain.x / 2
  84.            
  85.             if(y == 4 and x < half) then
  86.                 globalPush()
  87.             elseif(y == 4 and x > half) then
  88.                 globalRestart()
  89.             end
  90.         elseif(isModemMessage) then
  91.             local body = p4
  92.             if(body.type == "syncPull") then
  93.                 push()
  94.             elseif(body.type == "syncPush")  then
  95.                 drawHeader("Push Recieved")
  96.                 print("\n---")
  97.                 print("\n# Push Recieved")
  98.                 updateRepo(body.files)
  99.                 globalPush()
  100.             end
  101.         end
  102.     end
  103. end
  104.  
  105. function networkPoller()
  106.     while(true) do
  107.         devices = {}
  108.         modem.transmit(channel, channel,
  109.             { type = "syncNetworkPoll" }
  110.         )
  111.         parallel.waitForAny(
  112.             function()
  113.                 sleep(10)
  114.             end,
  115.             function()
  116.                 while(true) do
  117.                     local event, p1, p2, p3, p4, p5 = os.pullEvent()
  118.                    
  119.                     local isModemMessage = (event == "modem_message")
  120.                    
  121.                     if(isModemMessage) then
  122.                         local body = p4
  123.                        
  124.                         if(body.type == "syncNetworkPollRes") then
  125.                             table.insert(devices, body.deviceData)
  126.                         end
  127.                     end
  128.                 end
  129.             end
  130.         )
  131.         drawMain()
  132.     end
  133. end
  134.  
  135. function globalRestart()
  136.     drawHeader("Global Restart")
  137.     print("\n---")
  138.     print("\n# Performing Global Restart")
  139.     modem.transmit(channel, channel,
  140.         { type = "syncGlobalRestart" }
  141.     )
  142. end
  143.  
  144. function globalHardRestart()
  145.     drawHeader("Global Hard Restart")
  146.     print("\n---")
  147.     print("\n# Performing Global Hard Restart")
  148.     modem.transmit(channel, channel,
  149.         { type = "syncGlobalHardRestart" }
  150.     )
  151.     sleep(1)
  152.     os.reboot()
  153. end
  154.  
  155. function push()
  156.     drawHeader("Pull Response")
  157.     print("\n---")
  158.     print("\n# Performing Pull Response")
  159.     local files = serialiseFiles()
  160.     modem.transmit(channel, channel,
  161.         {
  162.             type = "syncPullRes",
  163.             files = files
  164.         }
  165.     )
  166. end
  167.  
  168. function updateRepo(files)
  169.     clearRepo()
  170.     print("- Updating Local Repository")
  171.     for i, file in pairs(files) do
  172.         local openedFile = fs.open(file.path, "w")
  173.         openedFile.write(file.contents)
  174.         openedFile.close()
  175.     end
  176. end
  177.  
  178. function clearRepo()
  179.     backupToDisk()
  180.     print("- Clearing Repository")
  181.     local files = listAllFiles(repoDir)
  182.     for i, file in pairs(files) do
  183.         fs.delete(file.path)
  184.     end
  185. end
  186.  
  187. function globalPush()
  188.     drawHeader("Global Push")
  189.     print("\n---")
  190.     print("\n# Peforming Global Push")
  191.     local files = serialiseFiles()
  192.     modem.transmit(channel, channel,
  193.         { type = "syncGlobalPush", files = files}
  194.     )
  195. end
  196.  
  197. function backupToDisk()
  198.     print("- Backing up to Disk")
  199.     fs.delete("/disk/backup")
  200.     fs.copy(repoDir, "/disk/backup")
  201. end
  202.  
  203. function serialiseFiles()
  204.     local files = listAllFiles(repoDir)
  205.     print("- Ignoring", #blacklist, "file(s)")
  206.     print("- Serialised", #files, "file(s)")
  207.     return files
  208. end
  209.  
  210. function listAllFiles(_path, _files)
  211.     local path = _path or ""
  212.     local files = _files or {}
  213.     for i, file in ipairs(fs.list(path)) do
  214.         local path = fs.combine(path, file)
  215.         if(fs.isDir(path)) then
  216.             listAllFiles(path, files)
  217.         else
  218.             local exclude = false
  219.             for i, name in pairs(blacklist) do
  220.                 if(string.match(path, name)) then exclude = true end
  221.             end
  222.             if(not exclude) then
  223.                 local openedFile = fs.open(path, "r")
  224.                 local contents = openedFile.readAll()
  225.                 openedFile.close()
  226.                 table.insert(files, {
  227.                     path = path,
  228.                     contents = contents
  229.                 })
  230.             end
  231.         end
  232.     end
  233.     return files
  234. end
  235.  
  236. function drawHeader(lastAction)
  237.     winHeader.bg = colors.orange
  238.     winHeader.setBackgroundColor(winHeader.bg)
  239.    
  240.     drawBox(winHeader,
  241.         1, 1, winHeader.x, winHeader.y,
  242.         true
  243.     )
  244.     drawBox(winHeader,
  245.         1, winHeader.y, winHeader.x, winHeader.y,
  246.         true, colors.white
  247.     )
  248.    
  249.     write(winHeader, "Network Sync", 0, 2, "centre")
  250.     write(winHeader, "Last Action: " .. lastAction, 0, 4, "centre")
  251. end
  252.  
  253. function drawFooter()
  254.     winFooter.bg = colors.orange
  255.     winFooter.setBackgroundColor(winFooter.bg)
  256.    
  257.     drawBox(winFooter,
  258.         1, 1, winFooter.x, winFooter.y,
  259.         true
  260.     )
  261.     drawBox(winFooter,
  262.         1, 1, winFooter.x, 1,
  263.         true, colors.white
  264.     )
  265.    
  266.     write(winFooter, "Channel: " .. channel, 2, 3, "right")
  267. end
  268.  
  269. function drawMain()
  270.     winMain.bg = colors.red
  271.     winMain.setBackgroundColor(winMain.bg)
  272.    
  273.     drawBox(winMain,
  274.         1, 1, winMain.x, winMain.y,
  275.         true
  276.     )
  277.    
  278.     write(winMain, "Options:", 0, 2, "centre")
  279.     write(winMain, "Global Push", 2, 4, "left")
  280.     write(winMain, "Global Restart", 2, 4, "right")
  281.  
  282.     write(winMain, "Devices: #" .. #devices, 0, 6, "centre")
  283.     for i, device in pairs(devices) do
  284.         if((7 + i) > (winMain.y - 1)) then break end
  285.         write(winMain,
  286.             i .. " - [ " .. device.program .. " ]",
  287.             2, (7 + i), "left"
  288.         )
  289.     end
  290.  
  291. end
  292.  
  293. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement