Server.cs 5.2 KB

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