MainClient.cs 11 KB

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