Advertisement
kryptanyte

Untitled

Apr 14th, 2019
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. local modem = peripheral.wrap("top");
  2. local network = peripheral.wrap("back");
  3.  
  4. --[[
  5.  
  6.   Networking Variables
  7.  
  8. ]]--
  9.  
  10. local s_netChannel = 1;
  11. local s_computers = {};
  12.  
  13. local responseChannels = {};
  14.  
  15. --[[
  16.  
  17.   Generic Functions
  18.  
  19. ]]--
  20.  
  21. function in_table(tbl, val)
  22.   for i,v in pairs(tbl) do
  23.     if(v == val) then
  24.       return true;
  25.     end --if
  26.   end --for
  27.   return false;
  28. end --in_table
  29.  
  30. --[[
  31.  
  32.   Network Functions
  33.  
  34. ]]--
  35.  
  36. function generateResponseChannel()
  37.   while true do
  38.     local channel = math.random(10000,12000);
  39.     if(in_table(reponseChannels, channel) == false) then
  40.       table.insert(responseChannels, channel);
  41.       return channel;
  42.     end --if
  43.   end --while
  44. end --generateResponseChannel
  45.  
  46. function netTcpReceiveResponse(target)
  47.  
  48.   local timeout = os.startTimer(5);
  49.  
  50.   while true do
  51.  
  52.     local e, v1, sChannel, rChannel, resp = os.pullEvent();
  53.    
  54.     if( e == "modem_message" ) then
  55.    
  56.       if( resp == "tcpreceived:"..target ) then
  57.    
  58.         return true;
  59.    
  60.       end --if
  61.      
  62.     elseif( e == "timer" and v1 == timeout ) then
  63.    
  64.       return false;
  65.    
  66.     end --if
  67.  
  68.   end --while
  69.  
  70. end --netTcpReceiveResponse
  71.  
  72. function netTcpSendResponse(target)
  73.  
  74.   network.transmit(target, s_netChannel, "tcpreceived:"..os.getComputerID());
  75.  
  76. end --netTcpSendResponse
  77.  
  78. function netSend(target, message, tcp)
  79.  
  80.   network.transmit(target, s_netChannel, message);
  81.  
  82.   print("Request Sent");
  83.  
  84.   if( tcp ~= nil and tcp == true ) then
  85.    
  86.     for i = 1, 3 do
  87.      
  88.       if( netTcpReceiveResponse(target) ) then
  89.      
  90.         return true;
  91.      
  92.       end --if
  93.      
  94.     end --for
  95.    
  96.     return false;
  97.  
  98.   end --if
  99.  
  100.   return true;
  101.  
  102. end --netSend
  103.  
  104. function netPackage(act, dt)
  105.  
  106.   local pack = { action = act, data = dt }
  107.  
  108.   return textutils.serialise(pack);
  109.  
  110. end --netPackage
  111.  
  112. function netReceive(target, act)
  113.  
  114.   local timeout = os.startTimer(5);
  115.  
  116.   while true do
  117.  
  118.     local e, v1, sChannel, rChannel, resp = os.pullEvent();
  119.    
  120.     if( e == "modem_message" ) then
  121.      
  122.       local data = textutils.unserialise(resp);
  123.      
  124.       if( data.action == act ) then
  125.      
  126.         return data.data;
  127.      
  128.       end --if
  129.    
  130.     elseif( e == "timer" and v1 == timeout ) then
  131.      
  132.         break;
  133.    
  134.     end --if
  135.  
  136.   end --while
  137.  
  138. end --netReceive
  139.  
  140. function getComputers()
  141.  
  142.   local names = network.getNamesRemote();
  143.  
  144.   for i = 1, #names do
  145.  
  146.     local target = peripheral.wrap(names[i])
  147.  
  148.     if(target.isOn()) then
  149.    
  150.       local pack = netPackage("requestName")
  151.    
  152.       if( netSend(target.getID(), pack, true) ) then
  153.      
  154.         local data = netReceive(target.getID(), "requestName");
  155.        
  156.         s_computers[target.getID()] = { name = data.name, channel = target.getID() };
  157.    
  158.       end --if
  159.    
  160.     end --if
  161.  
  162.   end --for
  163.  
  164. end --getComputers
  165.  
  166.  
  167. function main()
  168.  
  169.   network.open(1);
  170.  
  171.   getComputers();
  172.  
  173.   term.clear();
  174.   term.setCursorPos(1,1);
  175.  
  176.   for k,v in pairs(s_computers) do
  177.  
  178.     print(v);
  179.  
  180.   end --if
  181.  
  182. end --main
  183.  
  184. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement