set doctor of communication when doctor logs in
This commit is contained in:
@@ -15,6 +15,7 @@ namespace ClientApp.Utils
|
|||||||
public const string START_SESSION = "START SESSION";
|
public const string START_SESSION = "START SESSION";
|
||||||
public const string STOP_SESSION = "STOP SESSION";
|
public const string STOP_SESSION = "STOP SESSION";
|
||||||
public const string SET_RESISTANCE = "SET RESISTANCE";
|
public const string SET_RESISTANCE = "SET RESISTANCE";
|
||||||
|
public const string LOGIN_DOCTOR = "LOGIN DOCTOR";
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// makes the json object with LOGIN identifier and username and password
|
/// makes the json object with LOGIN identifier and username and password
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -236,7 +236,7 @@ namespace DoctorApp.Utils
|
|||||||
string hashUser = Hashing.Hasher.HashString(username);
|
string hashUser = Hashing.Hasher.HashString(username);
|
||||||
string hashPassword = Hashing.Hasher.HashString(password);
|
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);
|
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ namespace DoctorApp.Utils
|
|||||||
public const string SET_RESISTANCE = "SET RESISTANCE";
|
public const string SET_RESISTANCE = "SET RESISTANCE";
|
||||||
public const string NEW_CONNECTION = "NEW CONNECTION";
|
public const string NEW_CONNECTION = "NEW CONNECTION";
|
||||||
public const string DISCONNECT = "DISCONNECT";
|
public const string DISCONNECT = "DISCONNECT";
|
||||||
|
public const string LOGIN_DOCTOR = "LOGIN DOCTOR";
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// makes the json object with LOGIN identifier and username and password
|
/// makes the json object with LOGIN identifier and username and password
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -38,6 +39,21 @@ namespace DoctorApp.Utils
|
|||||||
return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json));
|
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)
|
public static bool GetUsernamePassword(byte[] jsonbytes, out string username, out string password)
|
||||||
{
|
{
|
||||||
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonbytes));
|
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonbytes));
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.Net.Sockets;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using ClientApp.Utils;
|
using ClientApp.Utils;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace Server
|
namespace Server
|
||||||
{
|
{
|
||||||
@@ -93,28 +94,12 @@ namespace Server
|
|||||||
switch (identifier)
|
switch (identifier)
|
||||||
{
|
{
|
||||||
case DataParser.LOGIN:
|
case DataParser.LOGIN:
|
||||||
string username;
|
handleLogin(payloadbytes);
|
||||||
string password;
|
break;
|
||||||
bool worked = DataParser.GetUsernamePassword(payloadbytes, out username, out password);
|
case DataParser.LOGIN_DOCTOR:
|
||||||
if (worked)
|
handleLogin(payloadbytes);
|
||||||
{
|
communication.doctor = this;
|
||||||
if (verifyLogin(username, password))
|
Console.WriteLine("Set doctor to " + communication.doctor + " , this is " + this);
|
||||||
{
|
|
||||||
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"));
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case DataParser.START_SESSION:
|
case DataParser.START_SESSION:
|
||||||
this.saveData = new SaveData(Directory.GetCurrentDirectory() + "/" + this.username + "/" + sessionStart.ToString("yyyy-MM-dd HH-mm-ss"));
|
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;
|
this.saveData = null;
|
||||||
break;
|
break;
|
||||||
case DataParser.SET_RESISTANCE:
|
case DataParser.SET_RESISTANCE:
|
||||||
worked = DataParser.getResistanceFromResponseJson(payloadbytes);
|
bool worked = DataParser.getResistanceFromResponseJson(payloadbytes);
|
||||||
Console.WriteLine($"set resistance worked is " + worked);
|
Console.WriteLine($"set resistance worked is " + worked);
|
||||||
//set resistance on doctor GUI
|
//set resistance on doctor GUI
|
||||||
break;
|
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)
|
public void sendMessage(byte[] message)
|
||||||
{
|
{
|
||||||
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Server
|
|||||||
{
|
{
|
||||||
private TcpListener listener;
|
private TcpListener listener;
|
||||||
private List<Client> clients;
|
private List<Client> clients;
|
||||||
private Client doctor;
|
public Client doctor;
|
||||||
public Communication(TcpListener listener)
|
public Communication(TcpListener listener)
|
||||||
{
|
{
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
|
|||||||
Reference in New Issue
Block a user