MainClient.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using ErgometerLibrary;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace ErgometerDoctorApplication
  10. {
  11. class MainClient
  12. {
  13. public static TcpClient Server { get; set; }
  14. public static bool loggedin;
  15. private static Thread t;
  16. private static bool running;
  17. public static int Session;
  18. public static string HOST = "127.0.0.1";
  19. public static int PORT = 8888;
  20. public static List<NetCommand> backlog;
  21. //Server information
  22. public static List<ClientThread> clients;
  23. public static Dictionary<string, string> users;
  24. public static List<Tuple<string, double, int>> oldSessionsData;
  25. public static void RemoveActiveClient(ClientThread clientThread)
  26. {
  27. clients.Remove(clientThread);
  28. }
  29. public static Dictionary<int, string> activesessions;
  30. static MainClient()
  31. {
  32. Server = new TcpClient();
  33. loggedin = false;
  34. clients = new List<ClientThread>();
  35. users = new Dictionary<string, string>();
  36. activesessions = new Dictionary<int, string>();
  37. oldSessionsData = new List<Tuple<string, double, int>>();
  38. backlog = new List<NetCommand>();
  39. }
  40. public static bool Connect(string password, out string error)
  41. {
  42. error = "Succes";
  43. if (Server == null || !Server.Connected)
  44. {
  45. if (Server == null)
  46. Server = new TcpClient();
  47. try
  48. {
  49. Server.Connect(HOST, PORT);
  50. }
  51. catch (Exception e)
  52. {
  53. error = "Server is niet online.";
  54. return false;
  55. }
  56. NetCommand net = NetHelper.ReadNetCommand(Server);
  57. if (net.Type == NetCommand.CommandType.SESSION)
  58. Session = net.Session;
  59. else
  60. throw new Exception("Session not assigned");
  61. running = true;
  62. t = new Thread(run);
  63. t.IsBackground = true;
  64. t.Start();
  65. }
  66. if (!loggedin)
  67. {
  68. NetCommand command = new NetCommand("Doctor0tVfW", true, password, Session);
  69. NetHelper.SendNetCommand(Server, command);
  70. NetCommand response = NetHelper.ReadNetCommand(Server);
  71. if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG)
  72. {
  73. loggedin = false;
  74. error = "Het wachtwoord is onjuist.";
  75. return false;
  76. }
  77. loggedin = true;
  78. }
  79. SendNetCommand(new NetCommand(NetCommand.RequestType.SESSIONDATA, Session));
  80. Thread.Sleep(15);
  81. SendNetCommand(new NetCommand(NetCommand.RequestType.USERS, Session));
  82. Thread.Sleep(200);
  83. return true;
  84. }
  85. public static void Disconnect()
  86. {
  87. if (Server != null && Server.Connected)
  88. {
  89. NetHelper.SendNetCommand(Server, new NetCommand(NetCommand.CommandType.LOGOUT, Session));
  90. loggedin = false;
  91. running = false;
  92. Server.Close();
  93. Server = null;
  94. }
  95. }
  96. public static void run()
  97. {
  98. while (running)
  99. {
  100. if (loggedin && Server.Connected)
  101. {
  102. NetCommand command = NetHelper.ReadNetCommand(Server);
  103. HandleNetCommand(command);
  104. }
  105. }
  106. if(Server != null)
  107. Server.Close();
  108. }
  109. private static bool UsersBeingSent = false;
  110. private static int UsersSent = 0;
  111. private static int UsersLength = 0;
  112. private static bool SessionsBeingSent = false;
  113. private static int SessionsSent = 0;
  114. private static int SessionsLength = 0;
  115. private static bool ActiveSessionsBeingSent = false;
  116. private static int ActiveSessionsSent = 0;
  117. private static int ActiveSessionsLength = 0;
  118. private static void HandleNetCommand(NetCommand command)
  119. {
  120. switch (command.Type)
  121. {
  122. case NetCommand.CommandType.LENGTH:
  123. switch (command.Length)
  124. {
  125. case NetCommand.LengthType.USERS:
  126. users.Clear();
  127. UsersBeingSent = true;
  128. UsersSent = 0;
  129. UsersLength = command.LengthValue;
  130. break;
  131. case NetCommand.LengthType.SESSIONS:
  132. oldSessionsData.Clear();
  133. SessionsBeingSent = true;
  134. SessionsSent = 0;
  135. SessionsLength = command.LengthValue;
  136. break;
  137. case NetCommand.LengthType.SESSIONDATA:
  138. activesessions.Clear();
  139. ActiveSessionsBeingSent = true;
  140. ActiveSessionsSent = 0;
  141. ActiveSessionsLength = command.LengthValue;
  142. break;
  143. default:
  144. throw new FormatException("Error in NetCommand: Length type is not recognised");
  145. }
  146. break;
  147. case NetCommand.CommandType.ERROR:
  148. Console.WriteLine("An error occured, ignoring");
  149. break;
  150. case NetCommand.CommandType.USER:
  151. if(UsersBeingSent)
  152. {
  153. users.Add(command.DisplayName, command.Password);
  154. UsersSent++;
  155. if (UsersSent >= UsersLength)
  156. UsersBeingSent = false;
  157. }
  158. break;
  159. case NetCommand.CommandType.SESSIONDATA:
  160. if (ActiveSessionsBeingSent)
  161. {
  162. activesessions.Add(command.Session, command.DisplayName);
  163. ActiveSessionsSent++;
  164. if (ActiveSessionsSent >= ActiveSessionsLength)
  165. ActiveSessionsBeingSent = false;
  166. }
  167. if (SessionsBeingSent)
  168. {
  169. oldSessionsData.Add(new Tuple<string, double, int>(command.DisplayName, command.Timestamp, command.Session));
  170. SessionsSent++;
  171. if (SessionsSent >= SessionsLength)
  172. SessionsBeingSent = false;
  173. }
  174. break;
  175. default:
  176. HandToClient(command);
  177. break;
  178. }
  179. }
  180. private static void HandToClient(NetCommand command)
  181. {
  182. bool handled = false;
  183. foreach (ClientThread cl in clients)
  184. {
  185. if (cl.Session == command.Session)
  186. {
  187. cl.HandleCommand(command);
  188. handled = true;
  189. }
  190. }
  191. if(!handled)
  192. {
  193. backlog.Add(command);
  194. }
  195. }
  196. public static void SendNetCommand(NetCommand command)
  197. {
  198. if(! NetHelper.SendNetCommand(Server, command))
  199. {
  200. Disconnect();
  201. }
  202. }
  203. private static bool IsSessionRunning(int session)
  204. {
  205. foreach (ClientThread cl in clients)
  206. {
  207. if (cl.Session == session)
  208. return true;
  209. }
  210. return false;
  211. }
  212. private static List<NetCommand> GetBacklogForSession(int session)
  213. {
  214. List<NetCommand> temp = new List<NetCommand>();
  215. for(int i=backlog.Count-1; i>= 0; i--)
  216. {
  217. if (backlog[i].Session == session)
  218. {
  219. temp.Add(backlog[i]);
  220. backlog.RemoveAt(i);
  221. }
  222. }
  223. return temp;
  224. }
  225. public static void StartNewClient(string name, int session)
  226. {
  227. if (IsSessionRunning(session))
  228. return;
  229. //Start new client
  230. ClientThread cl = new ClientThread(name, session, false);
  231. clients.Add(cl);
  232. //Run client on new thread
  233. Thread thread = new Thread(new ThreadStart(cl.run));
  234. thread.IsBackground = true;
  235. thread.Start();
  236. Thread.Sleep(5);
  237. List<NetCommand> sessionbacklog = GetBacklogForSession(session);
  238. for (int i = sessionbacklog.Count - 1; i >= 0; i--)
  239. {
  240. cl.HandleCommand(sessionbacklog[i]);
  241. }
  242. Thread.Sleep(5);
  243. SendNetCommand(new NetCommand(NetCommand.RequestType.PERSONALDATA, session));
  244. }
  245. public static void StartOldClient(string name, int session)
  246. {
  247. if (IsSessionRunning(session))
  248. return;
  249. //Start new client
  250. ClientThread cl = new ClientThread(name, session, true);
  251. clients.Add(cl);
  252. //Run client on new thread
  253. Thread thread = new Thread(new ThreadStart(cl.run));
  254. thread.IsBackground = true;
  255. thread.Start();
  256. SendNetCommand(new NetCommand(NetCommand.RequestType.OLDDATA, session));
  257. Thread.Sleep(5);
  258. SendNetCommand(new NetCommand(NetCommand.RequestType.CHAT, session));
  259. Thread.Sleep(5);
  260. SendNetCommand(new NetCommand(NetCommand.RequestType.PERSONALDATA, session));
  261. Thread.Sleep(5);
  262. SendNetCommand(new NetCommand(NetCommand.RequestType.TESTRESULT, session));
  263. Thread.Sleep(5);
  264. }
  265. }
  266. }