Orion09

Untitled

Mar 30th, 2025 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. -- Master Startup Script for a Distributed Lua Shell Cluster
  2. -- (Clients only execute the commands. The master waits for the first valid result.)
  3.  
  4. -- Function to open all attached modems
  5. local function openAllModems()
  6. local sides = {"left", "right", "top", "bottom", "front", "back"}
  7. local opened = {}
  8. for _, side in ipairs(sides) do
  9. if peripheral.getType(side) == "modem" then
  10. rednet.open(side)
  11. table.insert(opened, side)
  12. print("Opened rednet on modem at side: " .. side)
  13. end
  14. end
  15. if #opened == 0 then
  16. error("No modems found! Please attach at least one modem.")
  17. end
  18. end
  19.  
  20. openAllModems()
  21.  
  22. print("Supercomputer Lua Shell (Clients Only) - Only valid Lua commands are supported.")
  23. print("Master Computer ID is: " .. os.getComputerID())
  24. print("Listening on protocol 'supercomp' for client registrations...")
  25.  
  26. local clients = {} -- table to hold registered client IDs
  27.  
  28. -- Registration listener runs continuously to register clients
  29. local function registrationListener()
  30. while true do
  31. local senderID, message, protocol = rednet.receive("supercomp")
  32. if protocol == "supercomp" then
  33. if message == "register" then
  34. clients[senderID] = true
  35. rednet.send(senderID, "registered", "supercomp")
  36. print("Client " .. senderID .. " registered. Total clients: " .. tostring((function(t) local c=0; for _ in pairs(t) do c=c+1 end; return c end)(clients)))
  37. end
  38. end
  39. end
  40. end
  41.  
  42. -- Main command loop (master does not execute commands locally)
  43. local function commandInputLoop()
  44. while true do
  45. print("\nEnter Lua command (clients will execute this):")
  46. local cmd = read()
  47. if cmd and cmd ~= "" then
  48. -- Send the command to all registered clients.
  49. for cid, _ in pairs(clients) do
  50. rednet.send(cid, cmd, "supercomp")
  51. end
  52.  
  53. -- Wait for the first valid response from any client.
  54. local startTime = os.clock()
  55. local responder, result, proto = nil, nil, nil
  56. repeat
  57. local remaining = 10 - (os.clock() - startTime)
  58. if remaining <= 0 then break end
  59. responder, result, proto = rednet.receive("supercomp", remaining)
  60. -- Ignore registration and cancel messages.
  61. if result == "registered" or result == "cancel" then
  62. responder, result, proto = nil, nil, nil
  63. end
  64. until responder ~= nil
  65.  
  66. if responder and result then
  67. print("Result: " .. tostring(result))
  68. -- Send cancel signal to all other clients.
  69. for cid, _ in pairs(clients) do
  70. if cid ~= responder then
  71. rednet.send(cid, "cancel", "supercomp")
  72. end
  73. end
  74. else
  75. print("No response received within the timeout period.")
  76. end
  77. end
  78. end
  79. end
  80.  
  81. -- Run both tasks concurrently.
  82. parallel.waitForAll(registrationListener, commandInputLoop)
  83.  
Advertisement
Add Comment
Please, Sign In to add comment