From c6735fadc3e4cbcd4d857c3e967db1528f6dde9f Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Thu, 8 Oct 2015 18:29:24 +0200 Subject: [PATCH] Added new functionality --- ErgometerServer/ClientThread.cs | 10 +++--- ErgometerServer/DoctorSessionThread.cs | 33 ------------------ ErgometerServer/DoctorThread.cs | 2 +- ErgometerServer/ErgometerServer.csproj | 6 ++-- ErgometerServer/Server.cs | 48 +++++++++++++++++++++++--- 5 files changed, 51 insertions(+), 48 deletions(-) delete mode 100644 ErgometerServer/DoctorSessionThread.cs diff --git a/ErgometerServer/ClientThread.cs b/ErgometerServer/ClientThread.cs index c5bb8dc..f1dbc42 100644 --- a/ErgometerServer/ClientThread.cs +++ b/ErgometerServer/ClientThread.cs @@ -24,7 +24,6 @@ namespace ErgometerServer List metingen; List chat; - public ClientThread(TcpClient client, Server server) { this.client = client; @@ -66,18 +65,17 @@ namespace ErgometerServer { name = input.DisplayName; loggedin = true; - Console.WriteLine(name + " (client) connected"); + Console.WriteLine(name + " (client) connected with password: " + input.Password); FileHandler.CreateSession(session, name); } break; case NetCommand.CommandType.DATA: metingen.Add(input.Meting); - server.sendToDoctor(input); - FileHandler.WriteMetingen(session, metingen); + server.SendToDoctor(input); break; case NetCommand.CommandType.CHAT: chat.Add(new ChatMessage(name, input.ChatMessage)); - server.sendToDoctor(input); + server.SendToDoctor(input); Console.WriteLine(name + ": " + input.ChatMessage); break; case NetCommand.CommandType.LOGOUT: @@ -94,7 +92,7 @@ namespace ErgometerServer } } - public void writeToClient(NetCommand command) + public void SendToClient(NetCommand command) { NetHelper.SendNetCommand(client, command); if (command.Type == NetCommand.CommandType.CHAT) diff --git a/ErgometerServer/DoctorSessionThread.cs b/ErgometerServer/DoctorSessionThread.cs deleted file mode 100644 index 15d4d5d..0000000 --- a/ErgometerServer/DoctorSessionThread.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; - -namespace ErgometerServer -{ - class DoctorSessionThread - { - public int session { get; } - private TcpClient client; - private DoctorThread doctor; - private Boolean running; - - public DoctorSessionThread(int session, TcpClient client, DoctorThread doctor) - { - this.session = session; - this.client = client; - this.doctor = doctor; - } - - public void run() - { - running = true; - while (running) - { - - } - } - } -} diff --git a/ErgometerServer/DoctorThread.cs b/ErgometerServer/DoctorThread.cs index 072bd4e..21477a2 100644 --- a/ErgometerServer/DoctorThread.cs +++ b/ErgometerServer/DoctorThread.cs @@ -42,7 +42,7 @@ namespace ErgometerServer Console.WriteLine("Doctor logged out"); break; case NetCommand.CommandType.CHAT: - server.sendToClient(input); + server.SendToClient(input); Console.WriteLine(input); break; default: diff --git a/ErgometerServer/ErgometerServer.csproj b/ErgometerServer/ErgometerServer.csproj index 93b739f..adce5c8 100644 --- a/ErgometerServer/ErgometerServer.csproj +++ b/ErgometerServer/ErgometerServer.csproj @@ -33,8 +33,9 @@ 4 - - ..\..\ErgometerLibrary\ErgometerLibrary\bin\Debug\ErgometerLibrary.dll + + False + ..\..\..\ErgometerLibrary\ErgometerLibrary\ErgometerLibrary\bin\Debug\ErgometerLibrary.dll @@ -47,7 +48,6 @@ - diff --git a/ErgometerServer/Server.cs b/ErgometerServer/Server.cs index 0975a4d..fabc081 100644 --- a/ErgometerServer/Server.cs +++ b/ErgometerServer/Server.cs @@ -19,18 +19,22 @@ namespace ErgometerServer public List clients { get; } private DoctorThread doctor; + private static Dictionary users; public Server() { - FileHandler.CheckDataFolder(); + AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit); - Console.WriteLine("Checking datafolder " + FileHandler.DataFolder); + FileHandler.CheckStorage(); + users = FileHandler.LoadUsers(); clients = new List(); TcpListener listener = new TcpListener(NetHelper.GetIP("127.0.0.1"), 8888); listener.Start(); + Console.WriteLine("Server started successfully..."); + while (true) { Console.WriteLine("Waiting for connection with client..."); @@ -73,7 +77,7 @@ namespace ErgometerServer thread.Start(); } - public void sendToDoctor(NetCommand command) + public void SendToDoctor(NetCommand command) { if (doctor != null) { @@ -85,15 +89,49 @@ namespace ErgometerServer } } - public void sendToClient(NetCommand command) + public void BroadcastToClients(NetCommand command) + { + foreach (ClientThread clientThread in clients) + { + clientThread.SendToClient(command); + } + } + + public void SendToClient(NetCommand command) { foreach (ClientThread clientThread in clients) { if (clientThread.session == command.Session) { - clientThread.writeToClient(command); + clientThread.SendToClient(command); } } } + + private void AddUser(string name, string password) + { + users.Add(name, password); + } + + private bool CheckPassword(string name, string password) + { + string pass; + bool isOk = users.TryGetValue(name, out pass); + if (!isOk) return false; + + return pass == password; + } + + private void ChangePassword(string name, string newpassword) + { + + } + + private static void OnProcessExit(object sender, EventArgs e) + { + FileHandler.SaveUsers(users); + Console.WriteLine("Closing server"); + } } + }