DoctorThread.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. Console.WriteLine("Doctor logged out");
  33. break;
  34. case NetCommand.CommandType.CHAT:
  35. server.SendToClient(input);
  36. break;
  37. case NetCommand.CommandType.BROADCAST:
  38. server.BroadcastToClients(input.ChatMessage);
  39. break;
  40. case NetCommand.CommandType.VALUESET:
  41. server.SendToClient(input);
  42. break;
  43. case NetCommand.CommandType.USER:
  44. server.AddUser(input.DisplayName, input.Password);
  45. break;
  46. case NetCommand.CommandType.REQUEST:
  47. switch (input.Request)
  48. {
  49. case NetCommand.RequestType.USERS:
  50. sendToDoctor(new NetCommand(NetCommand.LengthType.USERS, server.users.Count-1, input.Session));
  51. foreach (KeyValuePair<string, string> user in server.users)
  52. {
  53. Thread.Sleep(10);
  54. if(user.Key != "Doctor0tVfW")
  55. sendToDoctor(new NetCommand(user.Key, user.Value, input.Session));
  56. }
  57. break;
  58. case NetCommand.RequestType.CHAT:
  59. List<ChatMessage> chat = FileHandler.ReadChat(input.Session);
  60. foreach (ChatMessage msg in chat)
  61. {
  62. Thread.Sleep(10);
  63. sendToDoctor(new NetCommand(msg.Message, msg.IsDoctor, input.Session));
  64. }
  65. break;
  66. case NetCommand.RequestType.OLDDATA:
  67. List<Meting> metingen = FileHandler.ReadMetingen(input.Session);
  68. foreach (Meting meting in metingen)
  69. {
  70. Thread.Sleep(10);
  71. sendToDoctor(new NetCommand(meting, input.Session));
  72. }
  73. break;
  74. case NetCommand.RequestType.ALLSESSIONS:
  75. List<Tuple<int, string, double>> sessions = FileHandler.GetAllSessions();
  76. sendToDoctor(new NetCommand(NetCommand.LengthType.SESSIONS, sessions.Count, input.Session));
  77. foreach(Tuple<int,string,double> session in sessions)
  78. {
  79. sendToDoctor(new NetCommand(session.Item2, session.Item3, session.Item1));
  80. Thread.Sleep(10);
  81. }
  82. break;
  83. case NetCommand.RequestType.SESSIONDATA:
  84. List<Tuple<int, string>> currentsessionsdata = server.GetRunningSessionsData();
  85. sendToDoctor(new NetCommand(NetCommand.LengthType.SESSIONDATA, currentsessionsdata.Count, input.Session));
  86. foreach (Tuple<int, string> ses in currentsessionsdata)
  87. {
  88. sendToDoctor(new NetCommand(ses.Item2, Helper.Now, ses.Item1));
  89. Thread.Sleep(10);
  90. }
  91. break;
  92. default:
  93. throw new FormatException("Unknown Command");
  94. }
  95. break;
  96. case NetCommand.CommandType.ERROR:
  97. Console.WriteLine("An error occured, assuming docter disconnected");
  98. running = false;
  99. Console.WriteLine("Doctor logged out due to an error");
  100. client.Close();
  101. break;
  102. default:
  103. throw new FormatException("Unknown Command");
  104. }
  105. }
  106. }
  107. public void sendToDoctor(NetCommand command)
  108. {
  109. NetHelper.SendNetCommand(client, command);
  110. }
  111. }
  112. }