DoctorThread.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections;
  3. using System.Net.Sockets;
  4. using System.Runtime.Remoting.Lifetime;
  5. using System.Threading;
  6. using ErgometerLibrary;
  7. using System.Collections.Generic;
  8. namespace ErgometerServer
  9. {
  10. class DoctorThread
  11. {
  12. TcpClient client;
  13. Server server;
  14. bool running;
  15. public DoctorThread(TcpClient client, Server server)
  16. {
  17. this.client = client;
  18. this.server = server;
  19. this.running = false;
  20. }
  21. public void run()
  22. {
  23. running = true;
  24. while (running)
  25. {
  26. NetCommand input = NetHelper.ReadNetCommand(client);
  27. switch (input.Type)
  28. {
  29. case NetCommand.CommandType.LOGOUT:
  30. running = false;
  31. client.Close();
  32. server.backlog.Clear();
  33. Console.WriteLine("Doctor logged out");
  34. break;
  35. case NetCommand.CommandType.CHAT:
  36. server.SendToClient(input);
  37. break;
  38. case NetCommand.CommandType.BROADCAST:
  39. server.BroadcastToClients(input.ChatMessage);
  40. break;
  41. case NetCommand.CommandType.VALUESET:
  42. server.SendToClient(input);
  43. break;
  44. case NetCommand.CommandType.USER:
  45. server.AddUser(input.DisplayName, input.Password);
  46. break;
  47. case NetCommand.CommandType.REQUEST:
  48. switch (input.Request)
  49. {
  50. case NetCommand.RequestType.USERS:
  51. sendToDoctor(new NetCommand(NetCommand.LengthType.USERS, server.users.Count-1, input.Session));
  52. foreach (KeyValuePair<string, string> user in server.users)
  53. {
  54. Thread.Sleep(10);
  55. if(user.Key != "Doctor0tVfW")
  56. sendToDoctor(new NetCommand(user.Key, user.Value, input.Session));
  57. }
  58. break;
  59. case NetCommand.RequestType.CHAT:
  60. List<ChatMessage> chat = FileHandler.ReadChat(input.Session);
  61. foreach (ChatMessage msg in chat)
  62. {
  63. Thread.Sleep(10);
  64. sendToDoctor(new NetCommand(msg.Message, msg.IsDoctor, input.Session));
  65. }
  66. break;
  67. case NetCommand.RequestType.OLDDATA:
  68. List<Meting> metingen = FileHandler.ReadMetingen(input.Session);
  69. foreach (Meting meting in metingen)
  70. {
  71. Thread.Sleep(10);
  72. sendToDoctor(new NetCommand(meting, input.Session));
  73. }
  74. break;
  75. case NetCommand.RequestType.ALLSESSIONS:
  76. List<Tuple<int, string, double>> sessions = FileHandler.GetAllSessions();
  77. sendToDoctor(new NetCommand(NetCommand.LengthType.SESSIONS, sessions.Count, input.Session));
  78. foreach(Tuple<int,string,double> session in sessions)
  79. {
  80. sendToDoctor(new NetCommand(session.Item2, session.Item3, session.Item1, true));
  81. Thread.Sleep(10);
  82. }
  83. break;
  84. case NetCommand.RequestType.SESSIONDATA:
  85. List<Tuple<int, string>> currentsessionsdata = server.GetRunningSessionsData();
  86. sendToDoctor(new NetCommand(NetCommand.LengthType.SESSIONDATA, currentsessionsdata.Count, input.Session));
  87. foreach (Tuple<int, string> ses in currentsessionsdata)
  88. {
  89. sendToDoctor(new NetCommand(ses.Item2, Helper.Now, ses.Item1));
  90. Thread.Sleep(10);
  91. }
  92. break;
  93. case NetCommand.RequestType.PERSONALDATA:
  94. NetCommand response = FileHandler.ReadPersonalData(input.Session);
  95. sendToDoctor(response);
  96. break;
  97. case NetCommand.RequestType.TESTRESULT:
  98. NetCommand resp = FileHandler.ReadTestResult(input.Session);
  99. sendToDoctor(resp);
  100. break;
  101. default:
  102. throw new FormatException("Unknown Command");
  103. }
  104. break;
  105. case NetCommand.CommandType.ERROR:
  106. Console.WriteLine("An error occured, assuming docter disconnected");
  107. running = false;
  108. Console.WriteLine("Doctor logged out due to an error");
  109. client.Close();
  110. break;
  111. default:
  112. throw new FormatException("Unknown Command");
  113. }
  114. }
  115. }
  116. public void sendToDoctor(NetCommand command)
  117. {
  118. NetHelper.SendNetCommand(client, command);
  119. }
  120. }
  121. }