BendymelonCoding

PDATerm Server Bugfix

May 19th, 2015
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. local function openRednet()
  2. local listOfSides = rs.getSides()
  3. local listofPossibles = {}
  4. c1 = 0
  5. for c1 = 1,6 do
  6. if peripheral.isPresent(tostring(listOfSides[c1])) and peripheral.getType(listOfSides[c1]) == "modem" then
  7. table.insert(listofPossibles,tostring(listOfSides[c1]))
  8. end
  9.  
  10. if c1 == 6 and table.maxn(listofPossibles) == 0 then
  11. print("No Modem Detected!")
  12. return nil
  13. end
  14.  
  15. if c1 == 6 and table.maxn(listofPossibles) ~= 0 then
  16. rednet.open(listofPossibles[1])
  17. return listofPossibles[1]
  18. end
  19. end
  20. end
  21. local function chkfiles()
  22. if fs.exists("/services") then
  23. if fs.exists("/users") then
  24. if fs.exists("/mail") then
  25. return true
  26. else
  27. mail = fs.open("/mail", "w")
  28. end
  29. else
  30. shell.run("pastebin get SQvMcfK0 users")
  31. end
  32. else
  33. shell.run("pastebin get SQvMcfK0 services")
  34. end
  35. end
  36. local function readLines(filename)
  37. local ctr = 0
  38. for a in io.lines(filename) do
  39. ctr = ctr + 1
  40. end
  41. return ctr
  42. end
  43. function main()
  44. openRednet()
  45. local services = fs.open("/services", "r");
  46. local userFile = fs.open("/users", "r");
  47. local mailFile = fs.open("/mail", "r");
  48. local userID = {};
  49. local mailDatabase = {};
  50. local userCount = readLines("users")
  51. local serviceCount = readLines("services")
  52. local serviceName = {};
  53. local serviceAddress = {};
  54. local serverName = "JimNet Central Server";
  55.  
  56. --for i = 0, serviceCount - 1 do
  57. --serviceName[i] = services.readLine();
  58. --serviceAddress[i] = services.readLine();
  59. --print ("Loaded Service. NAME:" .. serviceName[i] .. ", ADDR:" .. serviceAddress[i]);
  60. --end
  61. -- Disabled Services, I see no use for it at the moment.
  62.  
  63. for i = 0, userCount - 1 do
  64. userID = userFile.readLine();
  65. mailDatabase[i] = {};
  66.  
  67. mailCount = readLines("mail")
  68. for j = 0, mailCount - 1 do
  69. mailDatabase[i][j] = mailFile.readLine();
  70. end
  71.  
  72. print ("Loaded Mail. ID:" .. userID .. ", MAILCOUNT: " .. mailCount);
  73. end
  74.  
  75. sleep(1);
  76.  
  77. term.clear();
  78. term.setCursorPos(1, 1);
  79.  
  80. term.setTextColor(colors.red);
  81. print (" Phone Carrier Server\n");
  82. term.setTextColor(colors.white);
  83.  
  84.  
  85. while true do
  86.  
  87.  
  88. print ("\n\nListening for Requests...");
  89.  
  90. id, msg = rednet.receive();
  91. print ("Incoming Message. ID:" .. id .. ", MSG:" .. msg);
  92.  
  93.  
  94. if (msg == "$REGISTER") then
  95. for i = 0, userCount-1 do
  96. if (userID[i] == id) then
  97. rednet.send(id, "$FAILED");
  98. print ("Terminal #" .. id .. " registration failed.");
  99. else
  100. userID[userCount] = id;
  101. userCount = count + 1;
  102. print ("Terminal #" .. id .. " joined the network.");
  103. term.setTextColor(colors.red);
  104. print ("Updating Databases...");
  105.  
  106. end
  107. end
  108. end
  109.  
  110. if (msg == "$SERVICES") then
  111. rednet.send(id, tostring(serviceCount));
  112. for i = 0, serviceCount - 1 do
  113. rednet.send(id, serviceName[i]);
  114. rednet.send(id, serviceAddress[i]);
  115. end
  116. end
  117.  
  118. if (msg == "$INFO") then
  119. rednet.send(id, serverName);
  120. end
  121.  
  122. end
  123. end
  124. chkfiles()
  125. main()
Advertisement
Add Comment
Please, Sign In to add comment