Orion09

Untitled

Mar 30th, 2025 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. -- Client Startup Script for a Distributed Lua Shell Cluster
  2. -- This script registers with the master and then listens for commands.
  3. -- When a command is received (other than "cancel"), it executes it as Lua code.
  4. -- The print function is overridden to capture output so that, for example,
  5. -- print(9*9) returns "81" rather than a default return value.
  6. -- IMPORTANT: Set the master computer's ID to 36.
  7.  
  8. local masterID = 36
  9.  
  10. -- Function to open all attached modems.
  11. local function openAllModems()
  12. local sides = {"left", "right", "top", "bottom", "front", "back"}
  13. local opened = {}
  14. for _, side in ipairs(sides) do
  15. if peripheral.getType(side) == "modem" then
  16. rednet.open(side)
  17. table.insert(opened, side)
  18. print("Opened rednet on modem at side: " .. side)
  19. end
  20. end
  21. if #opened == 0 then
  22. error("No modems found! Please attach at least one modem.")
  23. end
  24. end
  25.  
  26. openAllModems()
  27.  
  28. print("Client Computer ID is: " .. os.getComputerID())
  29. print("Attempting to register with master (" .. masterID .. ") on protocol 'supercomp'...")
  30.  
  31. rednet.send(masterID, "register", "supercomp")
  32. local senderID, message, protocol = rednet.receive("supercomp", 10)
  33. if message == "registered" then
  34. print("Successfully registered with master (" .. masterID .. ").")
  35. else
  36. print("Failed to register with master. Received: " .. tostring(message))
  37. end
  38.  
  39. while true do
  40. local senderID, command, protocol = rednet.receive("supercomp")
  41. if command == "cancel" then
  42. -- Ignore cancel messages.
  43. else
  44. print("Received command: " .. tostring(command))
  45. local result, finished = nil, false
  46.  
  47. local function executeCommand()
  48. -- Capture printed output in a table.
  49. local capturedOutput = {}
  50. local function capturePrint(...)
  51. local args = {...}
  52. local parts = {}
  53. for i, v in ipairs(args) do
  54. table.insert(parts, tostring(v))
  55. end
  56. table.insert(capturedOutput, table.concat(parts, "\t"))
  57. end
  58.  
  59. -- Create a custom environment that uses our capturePrint for print.
  60. local env = {}
  61. for k, v in pairs(_G) do
  62. env[k] = v
  63. end
  64. env.print = capturePrint
  65.  
  66. -- Try compiling the command as an expression first, then as a statement.
  67. local func, err = load("return " .. command, "command", "t", env)
  68. if not func then
  69. func, err = load(command, "command", "t", env)
  70. end
  71.  
  72. if func then
  73. local success, res = pcall(func)
  74. if success then
  75. if #capturedOutput > 0 then
  76. result = table.concat(capturedOutput, "\n")
  77. else
  78. result = (res ~= nil and res or "nil")
  79. end
  80. else
  81. result = "Error: " .. tostring(res)
  82. end
  83. else
  84. result = "Error: " .. tostring(err)
  85. end
  86. finished = true
  87. end
  88.  
  89. local function waitForCancel()
  90. local s, msg, p = rednet.receive("supercomp", 10)
  91. if msg == "cancel" then
  92. finished = false
  93. result = nil
  94. end
  95. end
  96.  
  97. -- Run the command execution and cancellation listener concurrently.
  98. parallel.waitForAny(executeCommand, waitForCancel)
  99. if finished and result then
  100. rednet.send(masterID, tostring(result), "supercomp")
  101. end
  102. end
  103. end
  104.  
Advertisement
Add Comment
Please, Sign In to add comment