Corbinhol

Network Library

Jul 30th, 2024 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | None | 0 0
  1. local component = require("component");
  2. local modem = component.modem;
  3. local event = require("event");
  4. local serialization = require("serialization");
  5. local thread = require("thread");
  6.  
  7. local network = {};
  8. network.failedAttempts = 0;
  9. network.maxFailedAttempts = 3;
  10.  
  11. network.STATUS_FLAG_DISCONNECTED = -1;
  12. network.STATUS_FLAG_IDLE = 1;
  13. network.STATUS_FLAG_BUILDING = 2;
  14.  
  15. network.robotData = {};
  16. local networkThread;
  17.  
  18. function network.init(robotAddress, tryAttempts, waitTimeout)
  19.     print("Initiating Network Handler");
  20.     if tryAttempts == nil then
  21.         tryAttempts =5;
  22.     end
  23.    
  24.     if waitTimeout == nil then
  25.         waitTimeout = 1;
  26.     end
  27.  
  28.     modem.open(60);
  29.     network.robotAddress = robotAddress;
  30.     network.status = network.STATUS_FLAG_DISCONNECTED;
  31.     network.tryAttempts = tryAttempts;
  32.     network.waitTimeout = waitTimeout;
  33.  
  34.     networkThread = thread.create(network.handler);
  35.     return networkThread;
  36. end
  37.  
  38. function network.send(message1, message2, message3)
  39.     modem.send(network.robotAddress, 60, message1, message2, message3);
  40. end
  41.  
  42. function network.recieve()
  43.     local _, _, _, _, _, message1, message2, message3 = event.pull(network.waitTimeout, "modem_message", nil, network.robotAddress);
  44.     return message1, message2, message3;
  45. end
  46.  
  47. function network.requestRobotData()
  48.     local robotData;
  49.     local gotData = false;
  50.     for i = 1, network.tryAttempts do
  51.         network.send("getData");
  52.         local response, data = network.recieve();
  53.         if response == "heres data" then
  54.             robotData = serialization.unserialize(data);
  55.             gotData = true;
  56.             break;
  57.         end
  58.     end
  59.     return gotData, robotData;
  60. end
  61.  
  62. function network.handler()
  63.     print("Network handler started");
  64.     local shouldLoop = true;
  65.     local count = 0;
  66.  
  67.     --Begin by attempting to connect to the robot
  68.     print("&1Attempting to connect to robot...")
  69.     local gotData, data = network.requestRobotData();
  70.     if gotData == true then
  71.         network.robotData = data;
  72.         network.status = network.STATUS_FLAG_IDLE;
  73.         print("&5Connected to robot!");
  74.     else
  75.         print("&eRobot seems to be down. Will still try to connect in the background.");
  76.         while true do
  77.             local gotData, data = network.requestRobotData();
  78.             if gotData == true then
  79.                 network.robotData = data;
  80.                 network.status = network.STATUS_FLAG_IDLE;
  81.                 print("&5Connected to robot!");
  82.                 break;
  83.             end
  84.         end
  85.     end
  86.     shouldLoop = true;
  87.     while shouldLoop do
  88.         if network.status == network.STATUS_FLAG_IDLE then
  89.             local gotData, data = network.requestRobotData();
  90.             if gotData then
  91.                 network.robotData = data;
  92.                 network.failedAttempts = 0;
  93.             else
  94.                 if network.failedAttempts == network.maxFailedAttempts then
  95.                     print("&eIt appears that the robot is offline.");
  96.                     network.status = network.STATUS_FLAG_DISCONNECTED;
  97.                 end
  98.                 network.failedAttempts = network.failedAttempts + 1;
  99.             end
  100.             os.sleep(5);
  101.         elseif network.status == network.STATUS_FLAG_DISCONNECTED then
  102.             local gotData, data = network.requestRobotData();
  103.             if gotData then
  104.                 print("&5Robot is connected!")
  105.                 network.robotData = data;
  106.                 network.failedAttempts = 0;
  107.                 network.status = network.STATUS_FLAG_IDLE;
  108.             end
  109.             os.sleep(1);
  110.         end
  111.     end
  112. end
  113.  
  114. function network.findRobot()
  115.     local shouldLoop = true;
  116.     modem.open(60);
  117.     while shouldLoop do
  118.         modem.broadcast(60, "WhoIs", "CM_Auto_Robot");
  119.         local _, _, from, _, _, message = event.pull(1, "modem_message", nil, nil, nil, nil, "I am!");
  120.         if message == "I am!" then
  121.             print("Located potential robot. Attempting to pair.")
  122.             for i=1, 5 do
  123.                 modem.send(from, 60, "Confirmed. I am server.");
  124.                 _, _, _, _, _, message = event.pull(1, "modem_message", nil, from, nil, nil, "Confirmed. Communications locked.");
  125.                 if message == "Confirmed. Communications locked." then
  126.                     network.robotAddress = from;
  127.                     shouldLoop = false;
  128.                     break;
  129.                 end
  130.             end
  131.         end
  132.     end
  133.     return network.robotAddress;
  134. end
  135.  
  136. return network;
Advertisement
Add Comment
Please, Sign In to add comment