Advertisement
Corbinhol

Controller Boilerplate

Feb 3rd, 2023 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. --Controller Boiler Plate
  2. local protocol = "TEST"; --Do All Caps
  3. local hostname = "TestClient";
  4. local modemSide = "back";
  5.  
  6. local outputData = {};
  7. rednet.open(modemSide);
  8.  
  9. print("Starting Up Local Monitor Service");
  10.  
  11. if os.getComputerLabel() == nil then
  12. print("System Does Not Have ID...Attempting to obtain one")
  13. print("Protocol: " .. protocol);
  14. print("Hostname: " .. hostname);
  15. local i = 1;
  16. while true do
  17. local tempHostname = hostname .. "-" .. i;
  18. if rednet.lookup(protocol, tempHostname) == nil then
  19. os.setComputerLabel(tempHostname);
  20. print("Found Free Name. Set it to " .. tempHostname);
  21. break;
  22. end
  23. i = i + 1;
  24. end
  25. end
  26.  
  27. function log(value)
  28. table.insert(outputData, value);
  29. if table.getn(outputData) >= 15 then
  30. table.remove(outputData, 1);
  31. end
  32. end
  33.  
  34.  
  35. function mainLoop()
  36. log("1");
  37. log("2");
  38. end
  39.  
  40. function display()
  41. shell.run("clear");
  42. term.setCursorPos(1, 19);
  43. term.write(string.rep("=", 51));
  44. while true do
  45. term.setCursorPos(1,1);
  46. print(string.rep("=", 51));
  47. print("Protocol: " .. protocol);
  48. print("Hostname: " .. os.getComputerLabel());
  49. print(string.rep("=", 51));
  50. sleep(.5);
  51. for t=1, 14, 1 do
  52. term.clearLine()
  53. if outputData[t] ~= nil then
  54. print(outputData[t]);
  55. end
  56. end
  57. sleep(0);
  58. end
  59. end
  60.  
  61. function modemActivity()
  62. while true do
  63. local from, message = rednet.receive(protocol);
  64. if message == "getdata" then
  65. local outputData = textutils.serialize(outputData);
  66. rednet.send(from, outputData);
  67. log("Recieved Message From " .. from .. " Responding...");
  68. end
  69. end
  70. end
  71.  
  72. if os.getComputerLabel() ~= nil then
  73. rednet.host(protocol, os.getComputerLabel())
  74. print("Set Hostname");
  75. end
  76.  
  77. parallel.waitForAll(display, modemActivity, mainLoop);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement