Advertisement
kryptanyte

serv

Apr 13th, 2019 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. os.loadAPI('user')
  2. os.loadAPI('comp')
  3.  
  4. local modem = peripheral.wrap("left");
  5.  
  6. local cmpsOnline = {};
  7.  
  8. function openModemChannels()
  9.  
  10.   modem.open(100); -- Computer Authentication
  11.   modem.open(101); -- User Authentication
  12.   modem.open(102); --
  13.   modem.open(103); --
  14.  
  15. end --openModemChannels()
  16.  
  17. function receivedComputer(rChannel, data)
  18.  
  19.   local msg = textutils.unserialize(data);
  20.  
  21.   if( msg.task == 'register') then
  22.  
  23.     comp.create( msg.cmp.id, msg.cmp.name );
  24.    
  25.     local cmp = comp.get( msg.cmp.id);
  26.    
  27.     cmp.respChannel = math.random(10500, 11000);
  28.    
  29.     cmpsOnline[#cmpsOnline+1] = cmp;
  30.    
  31.     print(rChannel);
  32.    
  33.     modem.transmit(rChannel, 100, textutils.serialize({task = "settings", channel = cmp.respChannel}));
  34.  
  35.   end --if
  36.  
  37. end --receivedComputer
  38.  
  39. function modemListen()
  40.  
  41.   while true do
  42.  
  43.     local e, s, sChannel, rChannel, data, dist = os.pullEvent();
  44.    
  45.     if( e == "modem_message" ) then
  46.    
  47.       modem.transmit(rChannel, 100, 'received');
  48.      
  49.       if( sChannel == 100 ) then
  50.      
  51.         receivedComputer(rChannel, data);
  52.      
  53.       end --if
  54.      
  55.     elseif( e == "key") then
  56.    
  57.       if( s == keys.q ) then
  58.      
  59.         term.clear();
  60.         term.setCursorPos(1,1);
  61.      
  62.         for i = 1, #cmpsOnline do
  63.        
  64.           print(cmpsOnline[i].id .. ': ' .. cmpsOnline[i].respChannel);
  65.        
  66.         end --if
  67.      
  68.       end --if
  69.      
  70.     end --if
  71.  
  72.   end
  73.  
  74. end --modemListen
  75.  
  76. function main()
  77.  
  78.   openModemChannels();
  79.   modemListen();
  80.  
  81. end --main
  82.  
  83. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement