diff --git a/ErgometerServer/ClientThread.cs b/ErgometerServer/ClientThread.cs index ca000e8..c5bb8dc 100644 --- a/ErgometerServer/ClientThread.cs +++ b/ErgometerServer/ClientThread.cs @@ -16,7 +16,7 @@ namespace ErgometerServer Server server; string name; - int session; + public int session { get; } bool running; bool loggedin; @@ -36,14 +36,14 @@ namespace ErgometerServer metingen = new List(); chat = new List(); + session = FileHandler.GenerateSession(); + Console.WriteLine("Generated new session: " + session); } public void run() { running = true; - session = FileHandler.GenerateSession(); - Console.WriteLine("Generated new session: " + session); NetHelper.SendNetCommand(client, new NetCommand(session)); while (running) @@ -72,10 +72,12 @@ namespace ErgometerServer break; case NetCommand.CommandType.DATA: metingen.Add(input.Meting); + server.sendToDoctor(input); FileHandler.WriteMetingen(session, metingen); break; case NetCommand.CommandType.CHAT: chat.Add(new ChatMessage(name, input.ChatMessage)); + server.sendToDoctor(input); Console.WriteLine(name + ": " + input.ChatMessage); break; case NetCommand.CommandType.LOGOUT: @@ -92,5 +94,13 @@ namespace ErgometerServer } } + public void writeToClient(NetCommand command) + { + NetHelper.SendNetCommand(client, command); + if (command.Type == NetCommand.CommandType.CHAT) + { + chat.Add(new ChatMessage("Doctor", command.ChatMessage)); + } + } } } diff --git a/ErgometerServer/DoctorSessionThread.cs b/ErgometerServer/DoctorSessionThread.cs index 523af96..15d4d5d 100644 --- a/ErgometerServer/DoctorSessionThread.cs +++ b/ErgometerServer/DoctorSessionThread.cs @@ -9,9 +9,10 @@ namespace ErgometerServer { class DoctorSessionThread { - private int session; + public int session { get; } private TcpClient client; private DoctorThread doctor; + private Boolean running; public DoctorSessionThread(int session, TcpClient client, DoctorThread doctor) { @@ -22,7 +23,11 @@ namespace ErgometerServer public void run() { - + running = true; + while (running) + { + + } } } } diff --git a/ErgometerServer/DoctorThread.cs b/ErgometerServer/DoctorThread.cs index 2ecd930..072bd4e 100644 --- a/ErgometerServer/DoctorThread.cs +++ b/ErgometerServer/DoctorThread.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Net.Sockets; using System.Runtime.Remoting.Lifetime; using System.Threading; @@ -12,7 +13,6 @@ namespace ErgometerServer Server server; string name; - ArrayList threads; bool running; bool loggedin; @@ -23,7 +23,6 @@ namespace ErgometerServer this.client = client; this.server = server; this.name = "Unknown"; - this.threads = new ArrayList(); this.running = false; this.loggedin = false; } @@ -34,19 +33,28 @@ namespace ErgometerServer loggedin = true; while (loggedin && running) { - if (threads.Count < server.clients.Count) + NetCommand input = NetHelper.ReadNetCommand(client); + + switch (input.Type) { - DoctorSessionThread dt = new DoctorSessionThread(1, client, this); - threads.Add(dt); - Thread thread = new Thread(new ThreadStart(dt.run)); - thread.Start(); + case NetCommand.CommandType.LOGOUT: + loggedin = false; + Console.WriteLine("Doctor logged out"); + break; + case NetCommand.CommandType.CHAT: + server.sendToClient(input); + Console.WriteLine(input); + break; + default: + throw new FormatException("Unknown Command"); } } + client.Close(); } - public void logout() + public void sendToDoctor(NetCommand command) { - loggedin = false; + NetHelper.SendNetCommand(client, command); } } } \ No newline at end of file diff --git a/ErgometerServer/Server.cs b/ErgometerServer/Server.cs index a584943..0975a4d 100644 --- a/ErgometerServer/Server.cs +++ b/ErgometerServer/Server.cs @@ -73,14 +73,27 @@ namespace ErgometerServer thread.Start(); } - public void sendToDoctor() + public void sendToDoctor(NetCommand command) { - + if (doctor != null) + { + doctor.sendToDoctor(command); + } + else + { + Console.WriteLine("No doctor connected to the server yet"); + } } - public void sendToClient(int session) + public void sendToClient(NetCommand command) { - + foreach (ClientThread clientThread in clients) + { + if (clientThread.session == command.Session) + { + clientThread.writeToClient(command); + } + } } } }