Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Args
- local args = { ... }
- local repoDir = args[1] or "/lua"
- local channel = tonumber(args[2]) or 99
- -- Libraries
- local setup = require("/lua/lib/setupUtils")
- local monUtils = require("/lua/lib/monitorUtils")
- local write = monUtils.write
- local drawBox = monUtils.drawBox
- -- Peripherals
- local wrappedPers = setup.getPers({
- "monitor",
- "modem"
- })
- local monitor = setup.setupMonitor(
- wrappedPers.monitor[1], 0.5
- )
- local modem = wrappedPers.modem[1]
- -- Setup
- local devices = {}
- local blacklist = {"lua/state"}
- -- Windows
- local winHeader = setup.setupWindow(
- monitor, 1, 1, monitor.x, 6
- )
- local winFooter = setup.setupWindow(
- monitor, 1, (monitor.y - 3), monitor.x, 4
- )
- local winMain = setup.setupWindow(
- monitor, 1, 7, monitor.x, (monitor.y - (6 + 4))
- )
- -- Main
- function start()
- term.clear()
- term.setCursorPos(1, 1)
- print("\n# Network Sync Started")
- print("- Type: Master")
- print("\n# Sync Config:")
- print("- Respository Directory:", repoDir)
- print("- Channel:", channel)
- print("\n# Sync Controls:")
- print("- Up - Pushes respository update to all devices")
- print("- Del - Restarts all wrapped programs")
- print("- End - Restarts all devices\n")
- backupToDisk()
- modem.open(channel)
- drawHeader("Nothing")
- drawFooter()
- drawMain()
- parallel.waitForAny(await, networkPoller)
- end
- function await()
- while(true) do
- local event, p1, p2, p3, p4, p5 = os.pullEvent()
- local isDelKey = (event == "key" and p1 == 211)
- local isEndKey = (event == "key" and p1 == 207)
- local isUpKey = (event == "key" and p1 == 200)
- local isTouch = (event == "monitor_touch")
- local isModemMessage = (event == "modem_message")
- if(isDelKey) then
- globalRestart()
- elseif(isEndKey) then
- globalHardRestart()
- elseif(isUpKey) then
- globalPush()
- elseif(isTouch) then
- local x = p2
- local y = p3 - winHeader.y
- local half = winMain.x / 2
- if(y == 4 and x < half) then
- globalPush()
- elseif(y == 4 and x > half) then
- globalRestart()
- end
- elseif(isModemMessage) then
- local body = p4
- if(body.type == "syncPull") then
- push()
- elseif(body.type == "syncPush") then
- drawHeader("Push Recieved")
- print("\n---")
- print("\n# Push Recieved")
- updateRepo(body.files)
- globalPush()
- end
- end
- end
- end
- function networkPoller()
- while(true) do
- devices = {}
- modem.transmit(channel, channel,
- { type = "syncNetworkPoll" }
- )
- parallel.waitForAny(
- function()
- sleep(10)
- end,
- function()
- while(true) do
- local event, p1, p2, p3, p4, p5 = os.pullEvent()
- local isModemMessage = (event == "modem_message")
- if(isModemMessage) then
- local body = p4
- if(body.type == "syncNetworkPollRes") then
- table.insert(devices, body.deviceData)
- end
- end
- end
- end
- )
- drawMain()
- end
- end
- function globalRestart()
- drawHeader("Global Restart")
- print("\n---")
- print("\n# Performing Global Restart")
- modem.transmit(channel, channel,
- { type = "syncGlobalRestart" }
- )
- end
- function globalHardRestart()
- drawHeader("Global Hard Restart")
- print("\n---")
- print("\n# Performing Global Hard Restart")
- modem.transmit(channel, channel,
- { type = "syncGlobalHardRestart" }
- )
- sleep(1)
- os.reboot()
- end
- function push()
- drawHeader("Pull Response")
- print("\n---")
- print("\n# Performing Pull Response")
- local files = serialiseFiles()
- modem.transmit(channel, channel,
- {
- type = "syncPullRes",
- files = files
- }
- )
- end
- function updateRepo(files)
- clearRepo()
- print("- Updating Local Repository")
- for i, file in pairs(files) do
- local openedFile = fs.open(file.path, "w")
- openedFile.write(file.contents)
- openedFile.close()
- end
- end
- function clearRepo()
- backupToDisk()
- print("- Clearing Repository")
- local files = listAllFiles(repoDir)
- for i, file in pairs(files) do
- fs.delete(file.path)
- end
- end
- function globalPush()
- drawHeader("Global Push")
- print("\n---")
- print("\n# Peforming Global Push")
- local files = serialiseFiles()
- modem.transmit(channel, channel,
- { type = "syncGlobalPush", files = files}
- )
- end
- function backupToDisk()
- print("- Backing up to Disk")
- fs.delete("/disk/backup")
- fs.copy(repoDir, "/disk/backup")
- end
- function serialiseFiles()
- local files = listAllFiles(repoDir)
- print("- Ignoring", #blacklist, "file(s)")
- print("- Serialised", #files, "file(s)")
- return files
- end
- function listAllFiles(_path, _files)
- local path = _path or ""
- local files = _files or {}
- for i, file in ipairs(fs.list(path)) do
- local path = fs.combine(path, file)
- if(fs.isDir(path)) then
- listAllFiles(path, files)
- else
- local exclude = false
- for i, name in pairs(blacklist) do
- if(string.match(path, name)) then exclude = true end
- end
- if(not exclude) then
- local openedFile = fs.open(path, "r")
- local contents = openedFile.readAll()
- openedFile.close()
- table.insert(files, {
- path = path,
- contents = contents
- })
- end
- end
- end
- return files
- end
- function drawHeader(lastAction)
- winHeader.bg = colors.orange
- winHeader.setBackgroundColor(winHeader.bg)
- drawBox(winHeader,
- 1, 1, winHeader.x, winHeader.y,
- true
- )
- drawBox(winHeader,
- 1, winHeader.y, winHeader.x, winHeader.y,
- true, colors.white
- )
- write(winHeader, "Network Sync", 0, 2, "centre")
- write(winHeader, "Last Action: " .. lastAction, 0, 4, "centre")
- end
- function drawFooter()
- winFooter.bg = colors.orange
- winFooter.setBackgroundColor(winFooter.bg)
- drawBox(winFooter,
- 1, 1, winFooter.x, winFooter.y,
- true
- )
- drawBox(winFooter,
- 1, 1, winFooter.x, 1,
- true, colors.white
- )
- write(winFooter, "Channel: " .. channel, 2, 3, "right")
- end
- function drawMain()
- winMain.bg = colors.red
- winMain.setBackgroundColor(winMain.bg)
- drawBox(winMain,
- 1, 1, winMain.x, winMain.y,
- true
- )
- write(winMain, "Options:", 0, 2, "centre")
- write(winMain, "Global Push", 2, 4, "left")
- write(winMain, "Global Restart", 2, 4, "right")
- write(winMain, "Devices: #" .. #devices, 0, 6, "centre")
- for i, device in pairs(devices) do
- if((7 + i) > (winMain.y - 1)) then break end
- write(winMain,
- i .. " - [ " .. device.program .. " ]",
- 2, (7 + i), "left"
- )
- end
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement