jimthenerd

PDATerm Server

Apr 19th, 2014
2,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.88 KB | None | 0 0
  1. rednet.open("top");
  2. local services = fs.open("/services", "r");
  3. local userFile = fs.open("/users", "r");
  4. local mailFile = fs.open("/mails", "r");
  5. local userID = {};
  6. local mailDatabase = {};
  7. local userCount = tonumber(userFile.readLine());
  8. local serviceCount = tonumber(services.readLine());
  9. local serviceName = {};
  10. local serviceAddress = {};
  11.  
  12. local serverName = "JimNet Central Server";
  13.  
  14. for i = 0, serviceCount - 1 do
  15.     serviceName[i] = services.readLine();
  16.     serviceAddress[i] = services.readLine();
  17.     print ("Loaded Service. NAME:" .. serviceName[i] .. ", ADDR:" .. serviceAddress[i]);
  18. end
  19.  
  20. for i = 0, userCount - 1 do
  21.     userID = userFile.readLine();
  22.     mailDatabase[i] = {};
  23.    
  24.     mailCount = tonumber(mailFile.readLine());
  25.     for j = 0, mailCount - 1 do
  26.         mailDatabase[i][j] = mailFile.readLine();
  27.     end
  28.    
  29.     print ("Loaded Mail. ID:" .. userID .. ", MAILCOUNT: " .. mailCount);
  30. end
  31.  
  32. sleep(1);
  33.  
  34. term.clear();
  35. term.setCursorPos(1, 1);
  36.  
  37. term.setTextColor(colors.red);
  38. print ("          Phone Carrier Server\n");
  39. term.setTextColor(colors.white);
  40.  
  41.  
  42. while true do
  43.    
  44.    
  45.     print ("\n\nListening for Requests...");
  46.  
  47.     id, msg = rednet.receive();
  48.     print ("Incoming Message. ID:" .. id .. ", MSG:" .. msg);
  49.    
  50.    
  51.     if (msg == "$REGISTER") then
  52.         for i = 0, userCount-1 do
  53.             if (userID[i] == id) then
  54.                 rednet.send(id, "$FAILED");
  55.                 print ("Terminal #" .. id .. " registration failed.");
  56.             else
  57.                 userID[userCount] = id;
  58.                 userCount = count + 1;
  59.                 print ("Terminal #" .. id .. " joined the network.");
  60.                 term.setTextColor(colors.red);
  61.                 print ("Updating Databases...");
  62.                
  63.             end
  64.         end
  65.     end
  66.    
  67.     if (msg == "$SERVICES") then
  68.         rednet.send(id, tostring(serviceCount));
  69.         for i = 0, serviceCount - 1 do
  70.             rednet.send(id, serviceName[i]);
  71.             rednet.send(id, serviceAddress[i]);
  72.         end
  73.     end
  74.    
  75.     if (msg == "$INFO") then
  76.         rednet.send(id, serverName);
  77.     end
  78.    
  79. end
Advertisement
Add Comment
Please, Sign In to add comment