MtnMCG

cluster node

Sep 6th, 2024 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. -- node.lua
  2.  
  3. local modem = peripheral.find("modem")
  4. if not modem then error("No modem found") end
  5. modem.open(1) -- Open channel 1 for communication
  6.  
  7. local running = false
  8. local currentProgram = nil
  9. local accessMode = false
  10.  
  11. local function waitForResponse(timeout)
  12. local timer = os.startTimer(timeout)
  13. while true do
  14. local event, p1, p2, p3, p4, p5 = os.pullEvent()
  15. if event == "modem_message" then
  16. return p4
  17. elseif event == "timer" and p1 == timer then
  18. return nil
  19. end
  20. end
  21. end
  22.  
  23. local function networkFS(operation, ...)
  24. modem.transmit(1, 1, {type="FS_OP", operation=operation, args={...}})
  25. local response = waitForResponse(5)
  26. if response and response.success then
  27. return table.unpack(response.result)
  28. else
  29. return nil
  30. end
  31. end
  32.  
  33. local sandboxEnv = {
  34. -- Standard Lua functions
  35. assert = assert,
  36. error = error,
  37. ipairs = ipairs,
  38. next = next,
  39. pairs = pairs,
  40. pcall = pcall,
  41. select = select,
  42. tonumber = tonumber,
  43. tostring = tostring,
  44. type = type,
  45. unpack = unpack,
  46. _VERSION = _VERSION,
  47. xpcall = xpcall,
  48.  
  49. -- Tables
  50. table = table,
  51.  
  52. -- Strings
  53. string = string,
  54.  
  55. -- Math
  56. math = math,
  57.  
  58. -- Redirected file system operations
  59. fs = {
  60. list = function(...) return networkFS("list", ...) end,
  61. exists = function(...) return networkFS("exists", ...) end,
  62. isDir = function(...) return networkFS("isDir", ...) end,
  63. isReadOnly = function(...) return networkFS("isReadOnly", ...) end,
  64. getName = function(...) return networkFS("getName", ...) end,
  65. getDrive = function(...) return networkFS("getDrive", ...) end,
  66. getSize = function(...) return networkFS("getSize", ...) end,
  67. getFreeSpace = function(...) return networkFS("getFreeSpace", ...) end,
  68. makeDir = function(...) return networkFS("makeDir", ...) end,
  69. move = function(...) return networkFS("move", ...) end,
  70. copy = function(...) return networkFS("copy", ...) end,
  71. delete = function(...) return networkFS("delete", ...) end,
  72. combine = function(...) return networkFS("combine", ...) end,
  73. open = function(path, mode)
  74. local handle = networkFS("open", path, mode)
  75. if not handle then return nil end
  76. return {
  77. close = function() networkFS("close", handle) end,
  78. write = function(data) networkFS("write", handle, data) end,
  79. writeLine = function(data) networkFS("writeLine", handle, data) end,
  80. read = function(count) return networkFS("read", handle, count) end,
  81. readLine = function() return networkFS("readLine", handle) end,
  82. readAll = function() return networkFS("readAll", handle) end,
  83. flush = function() networkFS("flush", handle) end,
  84. }
  85. end,
  86. },
  87.  
  88. -- Other necessary CC functions
  89. sleep = sleep,
  90. write = write,
  91. print = print,
  92. printError = printError,
  93. read = read,
  94. os = {
  95. clock = os.clock,
  96. date = os.date,
  97. time = os.time,
  98. startTimer = os.startTimer,
  99. cancelTimer = os.cancelTimer,
  100. },
  101. peripheral = peripheral,
  102. term = term,
  103. }
  104.  
  105. local function redirectIO()
  106. local oldTerm = term.current()
  107. local redirectTerm = {}
  108.  
  109. function redirectTerm.write(text)
  110. oldTerm.write(text)
  111. modem.transmit(1, 1, {type="OUTPUT", output=text})
  112. end
  113.  
  114. function redirectTerm.clear()
  115. oldTerm.clear()
  116. modem.transmit(1, 1, {type="OUTPUT", output="\n--- Screen Cleared ---\n"})
  117. end
  118.  
  119. function redirectTerm.read(replaceChar, history)
  120. modem.transmit(1, 1, {type="INPUT_REQUEST"})
  121. local event, _, _, _, message = os.pullEvent("modem_message")
  122. while message.type ~= "INPUT" do
  123. event, _, _, _, message = os.pullEvent("modem_message")
  124. end
  125. return message.input
  126. end
  127.  
  128. setmetatable(redirectTerm, {__index = oldTerm})
  129. term.redirect(redirectTerm)
  130. end
  131.  
  132. local function runProgram(programCode)
  133. local func, err = load(programCode, "=program", "t", sandboxEnv)
  134. if not func then
  135. return false, "Error loading program: " .. err
  136. end
  137.  
  138. redirectIO()
  139. local success, result = pcall(func)
  140. term.redirect(term.native())
  141.  
  142. if not success then
  143. return false, "Error running program: " .. result
  144. end
  145.  
  146. return true, "Program completed successfully"
  147. end
  148.  
  149. while true do
  150. local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
  151.  
  152. if message.type == "SCAN" then
  153. modem.transmit(replyChannel, channel, {type="NODE", id=os.getComputerID()})
  154. elseif message.type == "RUN" and message.nodeId == os.getComputerID() then
  155. running = true
  156. currentProgram = message.program
  157. local success, result = runProgram(message.program)
  158. if success then
  159. modem.transmit(replyChannel, channel, {type="RESULT", result=result})
  160. else
  161. modem.transmit(replyChannel, channel, {type="ERROR", error=result})
  162. end
  163. running = false
  164. currentProgram = nil
  165. elseif message.type == "STOP" and message.nodeId == os.getComputerID() then
  166. running = false
  167. currentProgram = nil
  168. term.redirect(term.native())
  169. modem.transmit(replyChannel, channel, {type="STOPPED", id=os.getComputerID()})
  170. end
  171. end
Advertisement
Add Comment
Please, Sign In to add comment