Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ============================================================
- -- cct_agent.lua - Network agent for CC:T Remote
- --
- -- Run this on every computer EXCEPT the gateway.
- -- The gateway (running cct_remote daemon) will discover
- -- this computer via rednet and proxy commands to it.
- --
- -- Requirements: a modem on any side (wired or wireless)
- -- Usage: cct_agent (or put in startup file)
- -- ============================================================
- local REDNET_PROTOCOL = "cct_remote"
- -- ── Colour helpers ──────────────────────────────────────────
- local function colour(c)
- if not term.isColour() then return end
- term.setTextColour(c)
- end
- local function reset() colour(colours.white) end
- local function printC(c, ...)
- colour(c) ; print(...) ; reset()
- end
- -- ── Output capture ──────────────────────────────────────────
- local function captureOutput(fn)
- local lines = {}
- local curLine = ""
- local fakeTerm = {}
- for k, v in pairs(term.current()) do fakeTerm[k] = v end
- fakeTerm.write = function(text)
- curLine = curLine .. tostring(text)
- end
- fakeTerm.blit = function(text)
- curLine = curLine .. tostring(text)
- end
- fakeTerm.setCursorPos = function(x, y)
- if curLine ~= "" then
- lines[#lines + 1] = curLine
- curLine = ""
- end
- end
- fakeTerm.scroll = function()
- if curLine ~= "" then
- lines[#lines + 1] = curLine
- curLine = ""
- end
- end
- fakeTerm.clear = function() end
- fakeTerm.clearLine = function() end
- fakeTerm.setTextColour = function() end
- fakeTerm.setTextColor = function() end
- fakeTerm.setBackgroundColour = function() end
- fakeTerm.setBackgroundColor = function() end
- local prev = term.redirect(fakeTerm)
- local ok, err = pcall(fn)
- term.redirect(prev)
- if curLine ~= "" then lines[#lines + 1] = curLine end
- return ok, table.concat(lines, "\n"), tostring(err)
- end
- -- ── Modem open ──────────────────────────────────────────────
- local function openModem()
- local sides = { "top","bottom","left","right","front","back" }
- for _, side in ipairs(sides) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- return side
- end
- end
- for _, name in ipairs(peripheral.getNames()) do
- if peripheral.getType(name) == "modem" then
- rednet.open(name)
- return name
- end
- end
- return nil
- end
- -- ── Run a command, return ok + output ───────────────────────
- local function runCommand(code)
- local fn = load(code, "remote", "t", _ENV)
- if fn then
- local ok, out, err = captureOutput(fn)
- return ok, ok and (out ~= "" and out or "OK") or err
- else
- local ok, out, err = captureOutput(function()
- if not shell.run(code) then error("shell.run failed") end
- end)
- return ok, ok and (out ~= "" and out or "OK") or err
- end
- end
- -- ── Main ────────────────────────────────────────────────────
- local modemSide = openModem()
- if not modemSide then
- printError("No modem found. Attach a modem and restart.")
- return
- end
- local myId = os.getComputerID()
- local myLabel = os.getComputerLabel() or ("Agent " .. myId)
- printC(colours.yellow, "=== CC:T Agent ===")
- printC(colours.lime, "ID: " .. myId)
- printC(colours.lime, "Label: " .. myLabel)
- printC(colours.lime, "Modem: " .. modemSide)
- printC(colours.yellow, "Waiting for gateway... (Ctrl+T to stop)")
- -- Announce ourselves so gateways already running pick us up
- rednet.broadcast({ type = "hello", label = myLabel }, REDNET_PROTOCOL)
- while true do
- local senderId, msg, protocol = rednet.receive(REDNET_PROTOCOL)
- if type(msg) ~= "table" then goto continue end
- -- Gateway is pinging to discover agents
- if msg.type == "ping" then
- rednet.send(senderId, { type = "pong", label = myLabel }, REDNET_PROTOCOL)
- printC(colours.cyan, "Ping from gateway " .. senderId)
- -- Gateway is sending a command to run
- elseif msg.type == "run" and msg.code then
- printC(colours.yellow, "CMD from " .. senderId .. ": " .. msg.code)
- local ok, data = runCommand(msg.code)
- rednet.send(senderId, {
- type = "response",
- msgId = msg.msgId or "",
- ok = ok,
- data = data,
- }, REDNET_PROTOCOL)
- printC(ok and colours.lime or colours.red,
- (ok and "[ok] " or "[err] ") .. data)
- end
- ::continue::
- end
Advertisement
Add Comment
Please, Sign In to add comment