April_The_Sergal

nodeManager

Nov 25th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.29 KB | Gaming | 0 0
  1. local nodes = {}
  2. local nodeIDs = {}
  3.  
  4. local function initNodes()
  5.     nodes = {}
  6.     nodeIDs = {}
  7. end
  8.  
  9. local function addNode(hostName, computerID)
  10.     if not nodes[hostName] then
  11.         nodes[hostName] = {
  12.             type = hostName:match("^(%a+)_"),
  13.             id = tonumber(hostName:match("_(%d+)$")),
  14.             status = "UNKNOWN",
  15.             stayOnline = false,
  16.             storage = 0
  17.         }
  18.         nodeIDs[hostName] = computerID
  19.     end
  20. end
  21.  
  22. local function validateNodeStates()
  23.     for hostName, node in pairs(nodes) do
  24.         if node.stayOnline and node.status ~= "RUNNING" then
  25.             print("Node " .. hostName .. " should be running. Toggling...")
  26.             rednet.send(nodeIDs[hostName], { type = "toggle" }, PROTOCOL)
  27.             node.status = "PENDING"
  28.         elseif not node.stayOnline and node.status == "RUNNING" then
  29.             print("Node " .. hostName .. " should be stopped. Toggling...")
  30.             rednet.send(nodeIDs[hostName], { type = "toggle" }, PROTOCOL)
  31.             node.status = "PENDING"
  32.         end
  33.     end
  34.     sleep(5) -- Delay between validations
  35. end
  36.  
  37. local function getNodes()
  38.     return nodes
  39. end
  40.  
  41. return {
  42.     initNodes = initNodes,
  43.     addNode = addNode,
  44.     validateNodeStates = validateNodeStates,
  45.     getNodes = getNodes,
  46. }
Advertisement
Add Comment
Please, Sign In to add comment