Server.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using ErgometerLibrary;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading;
  10. namespace ErgometerServer
  11. {
  12. class Server
  13. {
  14. static void Main(string[] args)
  15. {
  16. new Server();
  17. }
  18. public static List<ClientThread> clients = new List<ClientThread>();
  19. private DoctorThread doctor;
  20. public Dictionary<string, string> users;
  21. public Server()
  22. {
  23. AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);
  24. FileHandler.CheckStorage();
  25. users = FileHandler.LoadUsers();
  26. TcpListener listener = new TcpListener(NetHelper.GetIP("127.0.0.1"), 8888);
  27. //TcpListener listener = new TcpListener(NetHelper.GetIP(GetIp()), 8888);
  28. listener.Start();
  29. Console.WriteLine("Server started successfully...");
  30. while (true)
  31. {
  32. Console.WriteLine("Waiting for connection with client...");
  33. //AcceptTcpClient waits for a connection from the client
  34. TcpClient client = listener.AcceptTcpClient();
  35. Console.WriteLine("Client connected");
  36. //Start new client
  37. ClientThread cl = new ClientThread(client, this);
  38. clients.Add(cl);
  39. //Run client on new thread
  40. Thread thread = new Thread(new ThreadStart(cl.run));
  41. thread.IsBackground = true;
  42. thread.Start();
  43. }
  44. }
  45. public void ChangeClientToDoctor(TcpClient client, ClientThread clth)
  46. {
  47. clients.Remove(clth);
  48. doctor = new DoctorThread(client, this);
  49. Thread thread = new Thread(new ThreadStart(doctor.run));
  50. thread.IsBackground = true;
  51. thread.Start();
  52. }
  53. public void SendToDoctor(NetCommand command)
  54. {
  55. if (doctor != null)
  56. {
  57. doctor.sendToDoctor(command);
  58. }
  59. }
  60. public void BroadcastToClients(string message)
  61. {
  62. foreach (ClientThread clientThread in clients)
  63. {
  64. clientThread.SendToClient(new NetCommand(message, true, clientThread.session));
  65. }
  66. }
  67. public void SendToClient(NetCommand command)
  68. {
  69. foreach (ClientThread clientThread in clients)
  70. {
  71. if (clientThread.session == command.Session)
  72. {
  73. clientThread.SendToClient(command);
  74. }
  75. }
  76. }
  77. public void AddUser(string name, string password)
  78. {
  79. users.Add(name, password);
  80. FileHandler.SaveUsers(users);
  81. }
  82. public void AddUser(Dictionary<string, string> users)
  83. {
  84. foreach(KeyValuePair<string, string> user in users)
  85. {
  86. users.Add(user.Key, user.Value);
  87. }
  88. FileHandler.SaveUsers(users);
  89. }
  90. public bool CheckPassword(string name, string password)
  91. {
  92. string pass;
  93. bool isOk = users.TryGetValue(name, out pass);
  94. if (!isOk) return false;
  95. return pass == password;
  96. }
  97. public List<int> GetRunningSessions()
  98. {
  99. List<int> sessions = new List<int>();
  100. foreach (ClientThread thread in clients)
  101. {
  102. sessions.Add(thread.session);
  103. }
  104. return sessions;
  105. }
  106. public List<Tuple<int, string>> GetRunningSessionsData()
  107. {
  108. List<Tuple<int, string>> sessions = new List<Tuple<int, string>>();
  109. foreach (ClientThread thread in clients)
  110. {
  111. sessions.Add(new Tuple<int, string>(thread.session, thread.name));
  112. }
  113. return sessions;
  114. }
  115. internal void RemoveActiveSession(ClientThread clientThread)
  116. {
  117. clients.Remove(clientThread);
  118. }
  119. private string GeneratePassword(int len = 8)
  120. {
  121. string pass = "";
  122. char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
  123. Random rand = new Random();
  124. for (int i=0; i<=len; i++)
  125. {
  126. pass += chars[rand.Next(0, chars.Length - 1)];
  127. }
  128. return pass;
  129. }
  130. private static void OnProcessExit(object sender, EventArgs e)
  131. {
  132. Console.WriteLine("Closing server");
  133. }
  134. }
  135. }