diff --git a/ClientApp/Utils/DataParser.cs b/ClientApp/Utils/DataParser.cs index 6c1e4b7..44208b6 100644 --- a/ClientApp/Utils/DataParser.cs +++ b/ClientApp/Utils/DataParser.cs @@ -15,6 +15,7 @@ namespace ClientApp.Utils public const string START_SESSION = "START SESSION"; public const string STOP_SESSION = "STOP SESSION"; public const string SET_RESISTANCE = "SET RESISTANCE"; + public const string LOGIN_DOCTOR = "LOGIN DOCTOR"; /// /// makes the json object with LOGIN identifier and username and password /// diff --git a/DoctorApp/Utils/Client.cs b/DoctorApp/Utils/Client.cs index 9ce05b7..5ab73e3 100644 --- a/DoctorApp/Utils/Client.cs +++ b/DoctorApp/Utils/Client.cs @@ -236,7 +236,7 @@ namespace DoctorApp.Utils string hashUser = Hashing.Hasher.HashString(username); string hashPassword = Hashing.Hasher.HashString(password); - byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(hashUser, hashPassword)); + byte[] message = DataParser.getJsonMessage(DataParser.LoginAsDoctor(hashUser, hashPassword)); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); diff --git a/DoctorApp/Utils/DataParser.cs b/DoctorApp/Utils/DataParser.cs index ca7ca90..b46b993 100644 --- a/DoctorApp/Utils/DataParser.cs +++ b/DoctorApp/Utils/DataParser.cs @@ -17,6 +17,7 @@ namespace DoctorApp.Utils public const string SET_RESISTANCE = "SET RESISTANCE"; public const string NEW_CONNECTION = "NEW CONNECTION"; public const string DISCONNECT = "DISCONNECT"; + public const string LOGIN_DOCTOR = "LOGIN DOCTOR"; /// /// makes the json object with LOGIN identifier and username and password /// @@ -38,6 +39,21 @@ namespace DoctorApp.Utils return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } + public static byte[] LoginAsDoctor(string mUsername, string mPassword) + { + dynamic json = new + { + identifier = LOGIN_DOCTOR, + data = new + { + username = mUsername, + password = mPassword, + } + }; + + return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); + } + public static bool GetUsernamePassword(byte[] jsonbytes, out string username, out string password) { dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonbytes)); diff --git a/Server/Client.cs b/Server/Client.cs index ee32ef8..0da1408 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -4,6 +4,7 @@ using System.Net.Sockets; using System.Text; using Newtonsoft.Json; using ClientApp.Utils; +using System.Diagnostics; namespace Server { @@ -93,28 +94,12 @@ namespace Server switch (identifier) { case DataParser.LOGIN: - string username; - string password; - bool worked = DataParser.GetUsernamePassword(payloadbytes, out username, out password); - if (worked) - { - if (verifyLogin(username, password)) - { - Console.WriteLine("Log in"); - this.username = username; - sendMessage(DataParser.getLoginResponse("OK")); - sendMessage(DataParser.getStartSessionJson()); - communication.NewLogin(this); - } - else - { - sendMessage(DataParser.getLoginResponse("wrong username or password")); - } - } - else - { - sendMessage(DataParser.getLoginResponse("invalid json")); - } + handleLogin(payloadbytes); + break; + case DataParser.LOGIN_DOCTOR: + handleLogin(payloadbytes); + communication.doctor = this; + Console.WriteLine("Set doctor to " + communication.doctor + " , this is " + this); break; case DataParser.START_SESSION: this.saveData = new SaveData(Directory.GetCurrentDirectory() + "/" + this.username + "/" + sessionStart.ToString("yyyy-MM-dd HH-mm-ss")); @@ -123,7 +108,7 @@ namespace Server this.saveData = null; break; case DataParser.SET_RESISTANCE: - worked = DataParser.getResistanceFromResponseJson(payloadbytes); + bool worked = DataParser.getResistanceFromResponseJson(payloadbytes); Console.WriteLine($"set resistance worked is " + worked); //set resistance on doctor GUI break; @@ -159,6 +144,32 @@ namespace Server } + private void handleLogin(byte[] payloadbytes) + { + string username; + string password; + bool worked = DataParser.GetUsernamePassword(payloadbytes, out username, out password); + if (worked) + { + if (verifyLogin(username, password)) + { + Console.WriteLine("Log in"); + this.username = username; + sendMessage(DataParser.getLoginResponse("OK")); + sendMessage(DataParser.getStartSessionJson()); + communication.NewLogin(this); + } + else + { + sendMessage(DataParser.getLoginResponse("wrong username or password")); + } + } + else + { + sendMessage(DataParser.getLoginResponse("invalid json")); + } + } + public void sendMessage(byte[] message) { stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); diff --git a/Server/Communication.cs b/Server/Communication.cs index 31350ce..a8ff762 100644 --- a/Server/Communication.cs +++ b/Server/Communication.cs @@ -12,7 +12,7 @@ namespace Server { private TcpListener listener; private List clients; - private Client doctor; + public Client doctor; public Communication(TcpListener listener) { this.listener = listener;