From 17a17e3c6de4b73378cf62ef3b5bcf4669cad13b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Mon, 19 Oct 2020 11:55:20 +0200 Subject: [PATCH] added comments to dataparser --- Hashing/DataParser.cs | 104 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 2 deletions(-) diff --git a/Hashing/DataParser.cs b/Hashing/DataParser.cs index 20851f9..a3d7fc4 100644 --- a/Hashing/DataParser.cs +++ b/Hashing/DataParser.cs @@ -39,7 +39,11 @@ namespace Util return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } - + /// + /// converts the given string parameter into a message using our protocol. + /// + /// the message string to send + /// a byte array using our protocol to send the message public static byte[] GetMessageToSend(string messageToSend) { dynamic json = new @@ -53,6 +57,12 @@ namespace Util return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } + /// + /// creates a message for when the doctor wants to log in. + /// + /// the username of the doctor + /// the (hashed) password of the doctor + /// a byte array using our protocol that contains the username and password of the doctor public static byte[] LoginAsDoctor(string mUsername, string mPassword) { dynamic json = new @@ -68,6 +78,13 @@ namespace Util return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)); } + /// + /// gets the username and password from a given message array. + /// + /// the array of bytes containing the message + /// the username variable that the username will be put into + /// the password variable that the password will be put into + /// true if the username and password were received correctly, false otherwise public static bool GetUsernamePassword(byte[] jsonbytes, out string username, out string password) { dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(jsonbytes)); @@ -85,6 +102,12 @@ namespace Util } } + /// + /// gets message using our protocol of the given identifier and data. + /// + /// the identifier string of the message + /// the payload data of the message + /// a byte array containing the json message with the given parameters, using our protocol. private static byte[] getJsonMessage(string mIdentifier, dynamic data) { dynamic json = new @@ -95,6 +118,11 @@ namespace Util return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01); } + /// + /// gets a message using our protocol with only the given identifier string. + /// + /// the identifier to put into the message + /// a byte array containing the json with only the identifier, using our protocol. private static byte[] getJsonMessage(string mIdentifier) { dynamic json = new @@ -104,11 +132,21 @@ namespace Util return getMessage(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(json)), 0x01); } + /// + /// gets the login response of the given status + /// + /// the status of the response + /// a byte array containing the response for the given status, using our protocol. public static byte[] getLoginResponse(string mStatus) { return getJsonMessage(LOGIN_RESPONSE, new { status = mStatus }); } + /// + /// gets the status of the given json message + /// + /// the byte array containing a json message using our protocol + /// the response of the message public static string getResponseStatus(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.status; @@ -195,6 +233,11 @@ namespace Util return getMessage(payload, 0x01); } + /// + /// gets the message to start a session with the given user username + /// + /// the username of the user we want to start the session for + /// a byte array containing the message to start the session of the given user, using our protocol. public static byte[] getStartSessionJson(string user) { dynamic data = new @@ -204,6 +247,11 @@ namespace Util return getJsonMessage(START_SESSION, data); } + /// + /// gets the message to stop a session with the given user username + /// + /// the username of the user we want to stop the session for + /// a byte array containing the message to stop the session of the given user, using our protocol. public static byte[] getStopSessionJson(string user) { dynamic data = new @@ -212,7 +260,13 @@ namespace Util }; return getJsonMessage(STOP_SESSION, data); } - + + /// + /// gets the message to set the resistance of the given user with the given resistance. + /// + /// the username to set the resistance of. + /// the resistance value to set + /// a byte array containing a json messsage to set the user's resistance, using our protocol. public static byte[] getSetResistanceJson(string user,float mResistance) { dynamic data = new @@ -223,6 +277,11 @@ namespace Util return getJsonMessage(SET_RESISTANCE, data); } + /// + /// gets the response message with the given value. + /// + /// the boolean value to indicate if the operation we want to send a response for was successful or not. + /// a byte array containing a json message with the response and the given value. public static byte[] getSetResistanceResponseJson(bool mWorked) { dynamic data = new @@ -232,6 +291,11 @@ namespace Util return getJsonMessage(SET_RESISTANCE, data); } + /// + /// gets the message to indicate a new connection for the given user. + /// + /// the username of the user to start a connection for. + /// a byte array containing a json message to indicate a new connection for the given user, using our protocol. public static byte[] getNewConnectionJson(string user) { if (user == null) @@ -243,6 +307,11 @@ namespace Util return getJsonMessage(NEW_CONNECTION, data); } + /// + /// gets the message for when a user has been disconnected. + /// + /// the username of the user that has been disconnected + /// a byte array containing a json message to indicate that the given user has disconnected, using our protocol. public static byte[] getDisconnectJson(string user) { dynamic data = new @@ -252,31 +321,62 @@ namespace Util return getJsonMessage(DISCONNECT, data); } + /// + /// gets the resistance from the given json message + /// + /// the json messag + /// the resistance that was in the message public static float getResistanceFromJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.resistance; } + /// + /// gets the resistance response from the given json message + /// + /// the byte array containin the json message + /// the response of the message, so wether it was successful or not. public static bool getResistanceFromResponseJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.worked; } + /// + /// gets the username from the given response message. + /// + /// the byte array containin the json message + /// the username in the message. public static string getUsernameFromResponseJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username; } + /// + /// gets the chat message from the given json message. + /// + /// the byte array containin the json message + /// the chat message in the json message public static string getChatMessageFromJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.chat; } + /// + /// gets the username from the given json message. + /// + /// the byte array containin the json message + /// the username that is in the message public static string getUsernameFromJson(byte[] json) { return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username; } + /// + /// gets the byte array with the json message to send a message with the given parameters. + /// + /// the username of the user that wants to send the message + /// the message the user wants to send + /// a byte array containing a json message with the username and corresponding message, using our protocol. public static byte[] getChatJson(string user, string message) { dynamic data = new