Advertisement
Guest User

router

a guest
Apr 18th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.35 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 responseChannels = {};
  12.  
  13. --[[
  14.  
  15.   Generic Functions
  16.  
  17. ]]--
  18.  
  19. function in_table(tbl, val)
  20.   for i,v in pairs(tbl) do
  21.     if(v == val) then
  22.       return true;
  23.     end --if
  24.   end --for
  25.   return false;
  26. end --in_table
  27.  
  28. --[[
  29.  
  30.   Network Functions
  31.  
  32. ]]--
  33.  
  34. function generateResponseChannel()
  35.   while true do
  36.     local channel = math.random(10000,12000);
  37.     if(in_table(reponseChannels, channel) == false) then
  38.       table.insert(responseChannels, channel);
  39.       return channel;
  40.     end --if
  41.   end --while
  42. end --generateResponseChannel
  43.  
  44. function netTcpResponse(target)
  45.  
  46.   local timeout = os.startTimer(5);
  47.  
  48.   while true do
  49.  
  50.     local e, v1, sChannel, rChannel, resp = os.pullEvent();
  51.    
  52.     if( e == "modem_message" ) then
  53.    
  54.       if( resp == "tcpreceived:"..target ) then
  55.    
  56.         return true;
  57.    
  58.       end --if
  59.      
  60.     elseif( e == "timer" and v1 == timeout ) then
  61.    
  62.       return false;
  63.    
  64.     end --if
  65.  
  66.   end --while
  67.  
  68. end --netTcpResponse
  69.  
  70. function netSend(target, message, tcp = true)
  71.  
  72.   modem.transmit(target, s_netChannel, textutils.serialise(message));
  73.  
  74.   if(tcp) then
  75.  
  76.     netTcpResponse(target);
  77.  
  78.   end --if
  79.  
  80. end --netSend
  81.  
  82. function netPackage(act, dt = nil)
  83.  
  84.   local pack = { action = act, data = dt }
  85.  
  86.   return textutils.serialise(pack);
  87.  
  88. end --netPackage
  89.  
  90. function netReceive(target, act)
  91.  
  92.   local timer = os.startTimer(5);
  93.  
  94.   while true do
  95.  
  96.     local e, v1, sChannel, rChannel, resp = os.pullEvent();
  97.    
  98.     if( e == "modem_message" ) then
  99.    
  100.       local data = textutils.unserialise(resp);
  101.      
  102.       if( data.action = act ) then
  103.      
  104.         return data.data;
  105.      
  106.       end
  107.    
  108.     end --if
  109.  
  110.   end --while
  111.  
  112. end --netReceive
  113.  
  114. function getComputers()
  115.  
  116.   local names = network.getNamesRemote();
  117.  
  118.   for i = 1, #names do
  119.  
  120.     local target = peripheral.wrap(names[i])
  121.  
  122.     if(target.isOn()) then
  123.    
  124.       local pack = netPackage("requestName")
  125.    
  126.       netSend(target.getID(), pack);
  127.      
  128.       netReceive(target.getID(), "requestName")
  129.    
  130.     end --if
  131.  
  132.   end --for
  133.  
  134. end --getComputers
  135.  
  136.  
  137. function main()
  138.  
  139.   network.open(1);
  140.  
  141.   getComputers();
  142.  
  143. end --main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement