Advertisement
Corbinhol

Turbine Server

Feb 20th, 2022 (edited)
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.32 KB | None | 0 0
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by holco.
  4. --- DateTime: 2/19/2022 11:28 PM
  5. ---
  6.  
  7. --Turbine Server
  8. --Version 0.1
  9.  
  10. os.execute("clear");
  11. local component = require("component");
  12. local thread = require("thread");
  13. local serialization = require("serialization")
  14. local consoleGpu;
  15. local consoleScreen;
  16. local modem = component.modem;
  17. local event = require("event");
  18. local filesystem = require("filesystem");
  19. local term = require("term");
  20. local computer = require("computer");
  21.  
  22. local serverDataPattern = {};
  23. local serverData = {};
  24. local stayConnected = {};
  25. local servers = {};
  26. local serversPattern = {};
  27. local handlerThreads = {};
  28. local displayThreads = {};
  29. local gpus = {};
  30. local run = true;
  31.  
  32. serversPattern["address"] = 0;
  33. serversPattern["screenAddress"] = 0;
  34. serversPattern["gpuAddress"] = "";
  35.  
  36. serverDataPattern["connectionStatus"] = "Disconnected";
  37. serverDataPattern["turbineStatus"] = "Offline";
  38. serverDataPattern["energyOutput"] = 0;
  39. serverDataPattern["energyBuffer"] = 0;
  40. serverDataPattern["mainPowerConnection"] = "Disconnected";
  41. serverDataPattern["fuelLoaded"] = 0;
  42. serverDataPattern["breakerStatus"] = "Disconnected";
  43. serverDataPattern["checkCloche"] = true;
  44.  
  45.  
  46. local gpuLoopThread;
  47. local port = 60;
  48. modem.open(port)
  49. --Startup Server
  50. function log(inputType, log)
  51.  
  52. end
  53.  
  54. function split(inputstr, sep)
  55.     if sep == nil then
  56.         sep = "%s"
  57.     end
  58.     local t={}
  59.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  60.         table.insert(t, str)
  61.     end
  62.     return t
  63. end
  64.  
  65.  
  66. local function shallowCopy(original)
  67.     local copy = {}
  68.     for key, value in pairs(original) do
  69.         copy[key] = value
  70.     end
  71.     return copy
  72. end
  73.  
  74.  
  75. function connectionHandler(address, client)
  76.     local timeout = 5;
  77.     --displayStats(client, tempServerData["connectionStatus"], tempServerData["turbineStatus"], tempServerData["energyOutput"], tempServerData["energyBuffer"], tempServerData["mainPowerConnection"], tempServerData["fuelLoaded"], tempServerData["breakerStatus"], tempServerData["checkCloche"])
  78.     --tempServerData = serverData[address];
  79.     print("Started Connection Handler.")
  80.     while stayConnected[address] do
  81.         if serverData[address]["connectionStatus"] == "Starting" then
  82.             local _, _, _, _, _, _ = event.pull("modem_message", nil, address, nil, nil, "started");
  83.             serverData[address]["connectionStatus"] = "Connected";
  84.             --print("Started");
  85.         elseif serverData[address]["connectionStatus"] == "Stopping" then
  86.             local _, _, _, _, _, msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8 = event.pull("modem_message", nil, address, nil, nil, "stopped");
  87.             serverData[address]["connectionStatus"] = "Connected";
  88.         else
  89.             modem.send(address, port, "getInfo");
  90.             local _, _, _, _, _, msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8 = event.pull(2, "modem_message", nil, address, nil, nil, "info");
  91.             --print(msg1);
  92.             if msg1 == nil and serverData[address]["connectionStatus"] ~= "Starting" and serverData[address]["connectionStatus"] ~= "Stopping" and timeout == 0 then
  93.                 serverData[address] = shallowCopy(serverDataPattern);
  94.             elseif msg1 == nil and serverData[address]["connectionStatus"] ~= "Starting" and serverData[address]["connectionStatus"] ~= "Stopping" and timeout > 0 then
  95.                 timeout = timeout - 1;
  96.             elseif serverData[address]["connectionStatus"] ~= "Starting" and serverData[address]["connectionStatus"] ~= "Stopping" then
  97.                 log("Recieved Data...Updating Current Data");
  98.                 serverData[address]["connectionStatus"] = "Connected";
  99.                 serverData[address]["turbineStatus"] = msg2;
  100.                 serverData[address]["energyOutput"] = msg3;
  101.                 serverData[address]["energyBuffer"] = msg4;
  102.                 serverData[address]["mainPowerConnection"] = msg5;
  103.                 serverData[address]["fuelLoaded"] = msg6;
  104.                 serverData[address]["breakerStatus"] = msg7;
  105.                 serverData[address]["checkCloche"] = msg8;
  106.                 tempServerData = serverData[address];
  107.                 displayStats(client, tempServerData["connectionStatus"], tempServerData["turbineStatus"], tempServerData["energyOutput"], tempServerData["energyBuffer"], tempServerData["mainPowerConnection"], tempServerData["fuelLoaded"], tempServerData["breakerStatus"], tempServerData["checkCloche"])
  108.                 timeout = 5;
  109.             end
  110.         end
  111.     end
  112.     serverData[address] = shallowCopy(serverDataPattern);
  113.     modem.send(address, port, "disconnecting")
  114.     log("Disconnecting From Server [Reason: Force Disconnected]");
  115. end
  116.  
  117. function addNewClient(serverName)
  118.     print("Rebinding All External Card Docks...");
  119.     for address, componentType in component.list("carddock") do
  120.         component.proxy(address).bindComponent();
  121.     end
  122.     log("Server", "Attempting connection with new client \"" .. serverName .. "\"");
  123.     print("Attempting connection with new client \"" .. serverName .. "\"");
  124.     local _, _, from, _, _, message, name;
  125.     modem.broadcast(port, "whois " .. serverName);
  126.  
  127.     _, _, from, _, _, message, name = event.pull(5, "modem_message", nil, nil, nil, nil, "I AM!!!", serverName);
  128.     if name == nil then
  129.         log("Server", "Attempted Connection Failed...Unable to find ")
  130.         print("Unable to find " .. serverName)
  131.     elseif name == serverName then
  132.         log("Server", "Found Server " .. serverName);
  133.         print("Found Server " .. serverName .. " Attempting Connection...");
  134.         print("Please Enter Screen Address For Displaying Data")
  135.         local tempScreenAddress;
  136.         local run = true;
  137.         while run do
  138.             tempScreenAddress = io.read();
  139.             if component.get(tempScreenAddress, "screen") == nil then
  140.                 print("Error...Screen Does Not Exist");
  141.             else
  142.                 tempScreenAddress = component.get(tempScreenAddress, "screen");
  143.                 run = false;
  144.             end
  145.         end
  146.  
  147.         print("Please Enter the Graphics card that will be controlling the Screen");
  148.         local tempGpuAddress;
  149.         run = true;
  150.         while run do
  151.             tempGpuAddress = io.read();
  152.             if component.get(tempGpuAddress, "gpu") == nil then
  153.                 print("Error...Gpu Does Not Exist");
  154.             else
  155.                 tempGpuAddress = component.get(tempGpuAddress, "gpu");
  156.                 run = false;
  157.             end
  158.         end
  159.  
  160.         print("Running");
  161.         servers[serverName] = shallowCopy(serversPattern);
  162.         servers[serverName]["address"] = from;
  163.         servers[serverName]["screenAddress"] = tempScreenAddress;
  164.         servers[serverName]["gpuAddress"] = tempGpuAddress;
  165.         local serversFile = io.open("servers.dat", "w");
  166.         serversFile:write(serialization.serialize(servers));
  167.         serversFile:close();
  168.         createHandler(serverName);
  169.     end
  170. end
  171.  
  172. function API()
  173.     while true do
  174.         local _, _, from, _, _, msg1, msg2, msg3, msg4 = event.pull("modem_message", nil, nil, nil, nil, "API");
  175.         local out;
  176.         print("working")
  177.         if msg2 == "cmd" then
  178.             executeCmd(msg3)
  179.             modem.send(from, port, true);
  180.         elseif msg2 == "getdata" then
  181.             modem.send(from, port, getData(msg3, msg4));
  182.         elseif msg2 == "whois PowerServer" then
  183.             modem.send(from, port, "I AM POWERSERVER!")
  184.         else
  185.             modem.send(from, port, "command not found");
  186.         end
  187.     end
  188. end
  189.  
  190. function createHandler(serverName)
  191.     local clientAddress = servers[serverName]["address"];
  192.     if servers[serverName] == nil then
  193.         print("Server Does Not Exist, or has not been added to the server yet");
  194.     elseif handlerThreads[clientAddress] ~= nil then
  195.         print("Client Handler already exists for this client");
  196.     else
  197.         print("Attempting To Create Handler For " .. serverName);
  198.         serverData[servers[serverName]["address"]] = shallowCopy(serverDataPattern);
  199.         --local test = serverData[servers[serverName]["address"]];
  200.         --print(test["connectionStatus"])
  201.         stayConnected[servers[serverName]["address"]] = true;
  202.         handlerThreads[serverName] = thread.create(connectionHandler, servers[serverName]["address"], serverName);
  203.         print("Attempting to start display for client");
  204.         displayThreads[serverName] = thread.create(printClientData, serverName);
  205.     end
  206. end
  207.  
  208. function removeClient(client)
  209.     local tempServerAddress = servers[client]["address"];
  210.     print("Removing " .. client .. " from System...");
  211.     if servers[client] ~= nil then
  212.         killHandler(client);
  213.         servers[client] = nil;
  214.         local serversFile = io.open("servers.dat", "w");
  215.         serversFile:write(serialization.serialize(servers));
  216.         serversFile:close();
  217.         print(client .. " successfully removed!")
  218.     else
  219.         print("Client Not Found On System.")
  220.     end
  221. end
  222. function getData(server, data)
  223.     local serAddress = servers[server]["address"];
  224.     if servers[server] == nil then
  225.         return("Server Does Not Exist.");
  226.     elseif serverData[serAddress][data] == nil then
  227.         return("Data does not exist");
  228.     else
  229.         return(data .. ": " .. serverData[serAddress][data])
  230.     end
  231. end
  232.  
  233. function sendRequest(target, request)
  234.     if servers[target] == nil then
  235.         print("Client Not Found...");
  236.     else
  237.         local serAddress = servers[target]["address"]
  238.         modem.send(serAddress, port, "cmd", request);
  239.         _, _, _, _, _, response = event.pull(10, "modem_message", nil, serAddress, nil, nil, "recieved");
  240.         if response ~= nil then
  241.             print("Message Recieved!...Waiting for server to finish...");
  242.             _, _, _, _, _, _, report = event.pull(10, "modem_message", nil, serAddress, nil, nil, "report");
  243.             print(report);
  244.         else
  245.             print("No Response Recieved...");
  246.         end
  247.     end
  248. end
  249.  
  250. function executeCmd(input)
  251.     local cmds = split(input, " ");
  252.     local tempServerData;
  253.     if servers[cmds[2]] ~= nil then tempServerData = serverData[servers[cmds[2]]["address"]]; end
  254.     if cmds[1] == "addclient" then --Add New Client [Usage: addclient [client]]
  255.         if cmds[2] == nil then print("Please Insert client name."); else addNewClient(cmds[2]) end
  256.     elseif cmds[1] == "removeclient" then --Remove New Client [Usage: removeclient [client]]
  257.         if cmds[2] == nil then print("Please Insert Existing Client Name...") else removeClient(cmds[2]); end
  258.     elseif cmds[1] == "shutdown" then --Shutdown System
  259.         if cmds[2] == nil then print("Error: No Client Name Inputed.")
  260.         elseif servers[cmds[2]] == nil then
  261.             print("Server Does not Exist");
  262.         elseif tempServerData["connectionStatus"] == "Starting" or tempServerData["connectionStatus"] == "Stopping" then
  263.             print("Please wait for " .. cmds[2] .. " to finish " .. tempServerData["connectionStatus"]);
  264.         else
  265.             shutdown(cmds[2], true);
  266.         end
  267.     elseif cmds[1] == "exit" then --Exits The Terminal
  268.         run = false;
  269.     elseif cmds[1] == "getinfo" then --Get info from server
  270.         if cmds[2] == nil then print("Please Insert Existing Client Name...") else print(getData(cmds[2], cmds[3])); end
  271.     elseif cmds[1] == "clear" then --clears screen
  272.         os.execute("clear");
  273.     elseif cmds[1] ==  "sendrequest" then --Send RAW request to client
  274.         if cmds[2] == nil then print("Error: No Client Name Inputed.")
  275.         elseif servers[cmds[2]] == nil then
  276.             print("Server Does not Exist");
  277.         elseif tempServerData["connectionStatus"] == "Starting" or tempServerData["connectionStatus"] == "Stopping" then
  278.             print("Please wait for " .. cmds[2] .. " to finish " .. tempServerData["connectionStatus"]);
  279.         else
  280.             if cmds[2] == nil then print("Please Insert Existing Client Name...") else sendRequest(cmds[2], cmds[3]); end
  281.         end
  282.     elseif cmds[1] == "getdata" then --Gets data from client
  283.         if cmds[2] == nil then print("Error: No Client Name Inputed.")
  284.         elseif servers[cmds[2]] == nil then
  285.             print("Server Does not Exist");
  286.         elseif tempServerData["connectionStatus"] == "Starting" or tempServerData["connectionStatus"] == "Stopping" then
  287.             print("Please wait for " .. cmds[2] .. " to finish " .. tempServerData["connectionStatus"]);
  288.         else
  289.             if servers[cmds[2]] == nil then
  290.                 print("Server does not exist")
  291.             else
  292.                 print("Requesting Data...")
  293.                 modem.send(servers[cmds[2]]["address"], port, "getData", cmds[3]);
  294.                 print("Waiting for response...")
  295.                 local _, _, _, _, _, _, response = event.pull(2, "modem_message", nil, servers[cmds[2]]["address"], nil, nil, "heres data");
  296.                 print(response);
  297.             end
  298.         end
  299.     elseif cmds[1] == "start" then
  300.         if cmds[2] == nil then print("Error: No Client Name Inputed.")
  301.         elseif servers[cmds[2]] == nil then
  302.             print("Server Does not Exist");
  303.         elseif tempServerData["connectionStatus"] == "Starting" then
  304.             print(cmds[2] .. " is already starting.");
  305.         elseif tempServerData["connectionStatus"] == "Stopping" then
  306.             print("Please wait for " .. cmds[2] .. " to finish stopping.");
  307.         else
  308.             startSystem(cmds[2]);
  309.         end
  310.     elseif cmds[1] == "stop" then
  311.         if cmds[2] == nil then print("Error: No Client Name Inputed.")
  312.         elseif servers[cmds[2]] == nil then
  313.             print("Server Does not Exist");
  314.         elseif tempServerData["connectionStatus"] == "Stopping" then
  315.             print(cmds[2] .. " is already stopping.");
  316.         elseif tempServerData["connectionStatus"] == "Starting" then
  317.             print("Please wait for " .. cmds[2] .. " to finish starting.");
  318.         else
  319.             stopSystem(cmds[2]);
  320.         end
  321.     elseif cmds[1] == "wakeup" then
  322.         if cmds[2] == nil then print("Error: No Client Name Inputed.")
  323.         elseif servers[cmds[2]] == nil then
  324.             print("Server Does not Exist");
  325.         elseif tempServerData["connectionStatus"] == "Starting" or tempServerData["connectionStatus"] == "Stopping" then
  326.             print("Please wait for " .. cmds[2] .. " to finish " .. tempServerData["connectionStatus"]);
  327.         else
  328.             wakeUpClient(cmds[2]);
  329.         end
  330.     elseif cmds[1] == "disconnect" then
  331.         if cmds[2] == nil then print("Error: No Client Name Inputed.")
  332.         elseif servers[cmds[2]] == nil then
  333.             print("Server Does not Exist");
  334.         elseif tempServerData["connectionStatus"] == "Starting" or tempServerData["connectionStatus"] == "Stopping" then
  335.             print("Please wait for " .. cmds[2] .. " to finish " .. tempServerData["connectionStatus"]);
  336.         else
  337.             killHandler(cmds[2]);
  338.             print(cmds[2] .. "Disconnected");
  339.         end
  340.     elseif cmds[1] == "connect" then
  341.         if cmds[2] == nil then print("Error: No Client Name Inputed.")
  342.         elseif servers[cmds[2]] == nil then
  343.             print("Server Does not Exist");
  344.         elseif tempServerData["connectionStatus"] == "Starting" or tempServerData["connectionStatus"] == "Stopping" then
  345.             print("Please wait for " .. cmds[2] .. " to finish " .. tempServerData["connectionStatus"]);
  346.         else
  347.             createHandler(cmds[2]);
  348.         end
  349.     else
  350.         print("Command Not Found!")
  351.     end
  352. end
  353.  
  354. function startSystem(client)
  355.     if servers[client] ~= nil then
  356.         print("Are you sure you want to start " .. client ..  " [y/n]");
  357.         local ans = io.read();
  358.         if ans == "y" then
  359.             for t=0, 3, 1 do
  360.                 modem.send(servers[client]["address"], port, "cmd", "startSystems");
  361.                 local _, _, _, _, _, msg = event.pull(5, "modem_message", nil, servers[client]["address"], nil, nil, "recieved");
  362.                 print("Client Recieved Command");
  363.                 local _, _, _, _, _, input = event.pull(5, "modem_message", nil, servers[client]["address"], nil, nil, "starting");
  364.                 if input == nil then
  365.                     print("Error Communicating With " .. client);
  366.                 else
  367.                     print("System starting...Please wait a few minutes for system to start up.");
  368.                     serverData[servers[client]["address"]]["connectionStatus"] = "Starting";
  369.                     break;
  370.                 end
  371.             end
  372.         end
  373.     end
  374. end
  375.  
  376. function stopSystem(client, ans);
  377.     if ans == nil then ans = false; end
  378.     if servers[client] ~= nil then
  379.         if ans == false then
  380.             print("Are you sure you want to stop " .. client ..  " [y/n]");
  381.             ans = io.read();
  382.             if ans == "y" then ans = true; else ans = false; end
  383.         end
  384.         if ans == true then
  385.             modem.send(servers[client]["address"], port, "cmd", "stopSystems");
  386.             local _, _, _, _, _, msg = event.pull(5, "modem_message", nil, servers[client]["address"], nil, nil, "stopping")
  387.             if msg == nil then
  388.                 print("Error Communicating With " .. client);
  389.             else
  390.                 print("System beginning to stop...Please wait a few minutes for system to finish.");
  391.                 serverData[servers[client]["address"]]["connectionStatus"] = "Stopping";
  392.             end
  393.         else
  394.             print("Aborting Stop...")
  395.         end
  396.     end
  397. end
  398.  
  399. function wakeUpClient(client)
  400.     local from, message, name
  401.     local clientAddress = servers[client]["address"];
  402.     if serverData[clientAddress] ~= nil then
  403.         print("Server is already connected");
  404.     else
  405.         print("Checking to see if client is already awake");
  406.         for i=1, 2, 1 do
  407.             modem.send(clientAddress, port, "whois " .. client);
  408.             _, _, from, _, _, message, name = event.pull(2, "modem_message", nil, clientAddress, nil, nil, "I AM!!!");
  409.             if message == "I AM!!!" then break; end
  410.         end
  411.         if message == "I AM!!!" then print("Client appears to already be awake...creating handler.");
  412.         else
  413.             print("Client appears to be asleep...waking up.");
  414.             modem.send(clientAddress, port, "Wake Up " .. client);
  415.             os.sleep(1);
  416.             for i=1, 3, 1 do
  417.                 modem.send(clientAddress, port, "whois " .. client);
  418.                 _, _, from, _, _, message, name = event.pull(2, "modem_message", nil, clientAddress, nil, nil, "I AM!!!");
  419.                 if message == "I AM!!!" then break; end
  420.             end
  421.             if message == "I AM!!!" then
  422.                 print("Client has started up...creating handler.");
  423.             else print("Failed to start up " .. client);
  424.             end
  425.         end
  426.  
  427.         if message == "I AM!!!" then
  428.             createHandler(client);
  429.         end
  430.     end
  431. end
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438. function colorText(gpuName, x, y, text, color)
  439.     gpus[gpuName].setForeground(color)
  440.     gpus[gpuName].set(x, y, text)
  441.     gpus[gpuName].setForeground(0xFFFFFF)
  442. end
  443.  
  444. function displayStats(serverName, connectionStatus, turbineStatus, energyOutput, energyBuffer, mainPowerConnection, fuelLoaded, breakerStatus, checkCloche);
  445.     gpus[serverName].set(2, 1, "----------------------------");
  446.     gpus[serverName].set(2, 2, serverName .. ":");
  447.     gpus[serverName].set(2, 3, "----------------------------");
  448.     gpus[serverName].set(2, 5, "Connection Status: ");
  449.     gpus[serverName].set(2, 6, "Turbine Status: ");
  450.     gpus[serverName].set(2, 7, "Main Power Status: ");
  451.     gpus[serverName].set(2, 8, "Energy Output: ");
  452.     gpus[serverName].set(2, 9, "Energy Buffer: ");
  453.     gpus[serverName].set(2, 10, "Fuel Loaded: ");
  454.     gpus[serverName].set(2, 11, "Breaker Status: ");
  455.     gpus[serverName].set(2, 12, "Cloche Checking: ");
  456.  
  457.     if turbineStatus == "Online" then
  458.         colorText(serverName, string.len(serverName) + 4, 2, "Running" .. "       ", 0x00FF00);
  459.     else
  460.         colorText(serverName, string.len(serverName) + 4, 2, "Stopped" .. "       ", 0xFF0000);
  461.     end
  462.  
  463.     if connectionStatus == "Disconnected" then
  464.         colorText(serverName, 21, 5, connectionStatus .. "     ", 0xFF0000);
  465.     elseif connectionStatus == "Starting" or connectionStatus == "Stopping" then
  466.         colorText(serverName, 21, 5, connectionStatus .. "     ", 0xFFFF00);
  467.     else
  468.         colorText(serverName, 21, 5, connectionStatus .. "     ", 0x00FF00);
  469.     end
  470.     if turbineStatus == "Offline" then
  471.         colorText(serverName, 18, 6, turbineStatus .. "     ", 0xFF0000);
  472.     else
  473.         colorText(serverName, 18, 6, turbineStatus .. "     ", 0x00FF00);
  474.     end
  475.     if mainPowerConnection == "Disconnected" then
  476.         colorText(serverName, 21, 7, mainPowerConnection .. "     ", 0xFF0000);
  477.     else
  478.         colorText(serverName, 21, 7, mainPowerConnection .. "     ", 0x00FF00);
  479.     end
  480.     if energyOutput == 0 then
  481.         colorText(serverName, 17, 8, tostring(math.floor(energyOutput)) .. "            ", 0xFF0000);
  482.     else
  483.         colorText(serverName, 17, 8, tostring(math.floor(energyOutput)) .. "            ", 0x00FF00);
  484.     end
  485.     if energyBuffer > 4000000 then
  486.         colorText(serverName, 17, 9, tostring(math.floor(energyBuffer)) .. "            ", 0x00FF00);
  487.     else
  488.         colorText(serverName, 17, 9, tostring(math.floor(energyBuffer)) .. "            ", 0xFF0000);
  489.     end
  490.     if fuelLoaded < 400000 then
  491.         colorText(serverName, 15, 10, tostring(math.floor(fuelLoaded)) .. "            ", 0xFF0000);
  492.     else
  493.         colorText(serverName, 15, 10, tostring(math.floor(fuelLoaded)) .. "             ", 0x00FF00);
  494.     end
  495.     if breakerStatus == "Connected" then
  496.         colorText(serverName, 18, 11, breakerStatus .. "     ", 0xFF0000);
  497.     else
  498.         colorText(serverName, 18, 11, breakerStatus .. "     ", 0x00FF00);
  499.     end
  500.     if checkCloche == false then
  501.         colorText(serverName, 19, 12, tostring(checkCloche) .. "     ", 0xFFFF00);
  502.     else
  503.         colorText(serverName, 19, 12, tostring(checkCloche) .. "     ", 0x00FF00);
  504.     end
  505. end
  506. --displayStats(client, tempServerData["connectionStatus"], tempServerData["turbineStatus"], tempServerData["energyOutput"], tempServerData["energyBuffer"], tempServerData["mainPowerConnection"], tempServerData["fuelLoaded"], tempServerData["breakerStatus"], tempServerData["checkCloche"])
  507.  
  508. function printClientData(client)
  509.     gpus[client] = component.proxy(servers[client]["gpuAddress"]);
  510.     gpus[client].bind(servers[client]["screenAddress"])
  511.     gpus[client].setResolution(34, 17);
  512.     gpus[client].fill(0, 0, 34, 17, " ");
  513.     while stayConnected[servers[client]["address"]] do
  514.         os.sleep(1);
  515.         tempServerData = serverData[servers[client]["address"]];
  516.         displayStats(client, tempServerData["connectionStatus"], tempServerData["turbineStatus"], tempServerData["energyOutput"], tempServerData["energyBuffer"], tempServerData["mainPowerConnection"], tempServerData["fuelLoaded"], tempServerData["breakerStatus"], tempServerData["checkCloche"])
  517.     end
  518. end
  519.  
  520.  
  521. function killHandler(handler);
  522.     local handlerServer = servers[handler]["address"]
  523.     local handlerThread = handlerThreads[handler];
  524.     if handlerThread == nil then
  525.         print("Error: Handler Address Not Found for " .. handler);
  526.     else
  527.         stayConnected[handlerServer] = false;
  528.         while handlerThreads[handler]:status() ~= "dead" do
  529.             os.sleep(1);
  530.         end
  531.         gpus[handler].fill(0, 0, 34, 17, " ");
  532.         handlerThreads[handler] = nil;
  533.         serverData[handlerServer] = nil;
  534.         return true;
  535.     end
  536. end
  537.  
  538. function shutdown(target, verify)
  539.     if verify == nil then verify = true; end
  540.     local targetAddress = servers[target]["address"];
  541.     local tempServerData = serverData[targetAddress];
  542.     local verifyOutput = true;
  543.     if verify then
  544.         print("Are you sure you would like to fully shut down " .. target .. "? [y/n]");
  545.         local verifyOutput = io.read();
  546.         if verifyOutput == "y" then verifyOutput = true;
  547.         else verifyOutput = false;
  548.         end
  549.     end
  550.     if verifyOutput then
  551.         print("Beginning Shutdown on " .. target);
  552.         if tempServerData["turbineStatus"] == "Online" then
  553.             print("Power Status of this Section Appears to be online...attempting to stop operations.");
  554.             stopSystem(target);
  555.             os.sleep(1);
  556.             while tempServerData["connectionStatus"] == "Stopping" do
  557.                 os.sleep(1);
  558.             end
  559.             print("System Successfully Stopped!")
  560.         end
  561.         print("Disconnecting from Client");
  562.         killHandler(target);
  563.         print("Successfully Disconnected");
  564.         print("Attempting To Shutdown Client");
  565.         modem.send(targetAddress, port, "shutdown");
  566.         local _, _, _, _, _, msg = event.pull(10, "modem_message", nil, targetAddress, nil, nil, "shutting down");
  567.         if msg == nil then print("Shutdown Attempt Failed...");
  568.             else print("Successfully Shutdown System!");
  569.         end
  570.     else
  571.        print("Aborting Shutdown...")
  572.     end
  573. end
  574.  
  575. print("Server Starting Up...Loading Files");
  576. if not filesystem.exists("screen.dat") then
  577.     print("No Primary Graphics card found on file.");
  578.     print("Please print the GPU you want your console to write on.");
  579.     local gpuTEMP, screenTEMP;
  580.     while true do
  581.         gpuTEMP = io.read();
  582.         if component.get(gpuTEMP, "gpu") == nil then
  583.             print("GPU Not Found");
  584.         else
  585.             break;
  586.         end
  587.     end
  588.  
  589.     print("Please Set the Screen you would like your console to be on");
  590.     while true do
  591.         screenTEMP = io.read();
  592.         if component.get(screenTEMP, "screen") == nil then
  593.             print("Screen Not Found");
  594.         else
  595.             break;
  596.         end
  597.     end
  598.     local screenFile = io.open("screen.dat", "w");
  599.     screenFile:write(component.get(screenTEMP, "screen"));
  600.     screenFile:close();
  601.     local gpuFile = io.open("gpu.dat", "w");
  602.     gpuFile:write(component.get(gpuTEMP, "gpu"));
  603.     gpuFile:close();
  604.  
  605.     consoleGpu = component.proxy(component.get(gpuTEMP, "gpu"));
  606.     consoleScreen = component.proxy(component.get(screenTEMP, "screen"));
  607. else
  608.     local screenFile = io.open("screen.dat", "r");
  609.     consoleScreen = component.proxy(screenFile:read());
  610.     screenFile:close();
  611.     local gpuFile = io.open("gpu.dat", "r");
  612.     consoleGpu = component.proxy(gpuFile:read());
  613.     gpuFile:close();
  614. end
  615. term.bind(consoleGpu);
  616. consoleGpu.bind(consoleScreen.address);
  617. if filesystem.exists("servers.dat") then
  618.     local serversFile = io.open("servers.dat", "r");
  619.     local serversSer = serversFile:read();
  620.     servers = serialization.unserialize(serversSer);
  621.  
  622.     for i in pairs(servers) do
  623.         wakeUpClient(i);
  624.     end
  625.     print("================================");
  626. end
  627.  
  628. print("Server Started Up");
  629. local apiThread = thread.create(API);
  630. while run do
  631.     io.write("> ");
  632.     local input = io.read();
  633.     executeCmd(input);
  634. end
  635.  
  636. apiThread:kill();
  637. print("Shutting Down Client");
  638. for i in pairs(servers) do
  639.     if serverData[servers[i]["address"]] ~= nil then
  640.         print("Killing Connection to " .. i);
  641.         killHandler(i);
  642.         print("Telling " .. i .. " to handle itself in my absense");
  643.         modem.send(servers[i]["address"], port, "startClocheCheck");
  644.     end
  645. end
  646. print("Goodbye :(");
  647. os.sleep(5);
  648. computer.shutdown()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement