set doctor of communication when doctor logs in

This commit is contained in:
Sem van der Hoeven
2020-10-14 14:49:53 +02:00
parent b519f33cb5
commit 91a20e0af7
5 changed files with 53 additions and 25 deletions

View File

@@ -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";
/// <summary>
/// makes the json object with LOGIN identifier and username and password
/// </summary>

View File

@@ -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);

View File

@@ -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";
/// <summary>
/// makes the json object with LOGIN identifier and username and password
/// </summary>
@@ -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));

View File

@@ -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);

View File

@@ -12,7 +12,7 @@ namespace Server
{
private TcpListener listener;
private List<Client> clients;
private Client doctor;
public Client doctor;
public Communication(TcpListener listener)
{
this.listener = listener;