Merge remote-tracking branch 'origin/develop' into opdrachten

This commit is contained in:
fabjuuuh
2020-09-16 14:29:11 +02:00
2 changed files with 47 additions and 17 deletions

View File

@@ -10,20 +10,23 @@ namespace RH_Engine
class JSONParser class JSONParser
{ {
/// <summary> /// <summary>
/// parses the given response from the server into strings /// returns all the users from the given response
/// </summary> /// </summary>
/// <param name="msg">the message gotten from the server, without the length prefix</param> /// <param name="msg">the message gotten from the server, without the length prefix</param>
/// <returns></returns> /// <returns></returns>
public static string[] Parse(string msg) public static PC[] GetUsers(string msg)
{ {
dynamic jsonData = JsonConvert.DeserializeObject(msg); dynamic jsonData = JsonConvert.DeserializeObject(msg);
Newtonsoft.Json.Linq.JArray data = jsonData.data; Newtonsoft.Json.Linq.JArray data = jsonData.data;
PC[] res = new PC[data.Count];
int counter = 0;
foreach (dynamic d in data) foreach (dynamic d in data)
{ {
Console.WriteLine(d.clientinfo.host); res[counter] = new PC((string)d.clientinfo.host, (string)d.clientinfo.user);
counter++;
} }
return null; return res;
} }
@@ -33,10 +36,11 @@ namespace RH_Engine
Newtonsoft.Json.Linq.JArray data = jsonData.data; Newtonsoft.Json.Linq.JArray data = jsonData.data;
foreach (dynamic d in data) foreach (dynamic d in data)
{ {
foreach(PC pc in PCs) foreach (PC pc in PCs)
{ {
if (d.clientinfo.host == pc.host && d.clientinfo.user == pc.user) if (d.clientinfo.host == pc.host && d.clientinfo.user == pc.user)
{ {
Console.WriteLine("connecting to {0}, on {1} with id {2}", pc.user, pc.host, d.id);
return d.id; return d.id;
} }
} }
@@ -45,6 +49,16 @@ namespace RH_Engine
return null; return null;
} }
public static string GetTunnelID(string json)
{
dynamic jsonData = JsonConvert.DeserializeObject(json);
if (jsonData.data.status == "ok")
{
return jsonData.data.id;
}
return null;
}
} }
} }

View File

@@ -37,7 +37,7 @@ namespace RH_Engine
stream.Write(res); stream.Write(res);
Console.WriteLine("sent message " + message); //Console.WriteLine("sent message " + message);
} }
public static string ReadPrefMessage(NetworkStream stream) public static string ReadPrefMessage(NetworkStream stream)
@@ -48,7 +48,7 @@ namespace RH_Engine
int length = BitConverter.ToInt32(lengthBytes); int length = BitConverter.ToInt32(lengthBytes);
Console.WriteLine("length is: " + length); //Console.WriteLine("length is: " + length);
byte[] buffer = new byte[length]; byte[] buffer = new byte[length];
int totalRead = 0; int totalRead = 0;
@@ -58,7 +58,7 @@ namespace RH_Engine
{ {
int read = stream.Read(buffer, totalRead, buffer.Length - totalRead); int read = stream.Read(buffer, totalRead, buffer.Length - totalRead);
totalRead += read; totalRead += read;
Console.WriteLine("ReadMessage: " + read); //Console.WriteLine("ReadMessage: " + read);
} while (totalRead < length); } while (totalRead < length);
return Encoding.UTF8.GetString(buffer, 0, totalRead); return Encoding.UTF8.GetString(buffer, 0, totalRead);
@@ -66,17 +66,28 @@ namespace RH_Engine
private static void CreateConnection(NetworkStream stream) private static void CreateConnection(NetworkStream stream)
{ {
//WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\"\r\n}");
//string msg = ReadPrefMessage(stream);
//Console.WriteLine(msg);
//string id = JSONParser.GetSessionID(msg, PCs);
//Console.WriteLine(id);
WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\"\r\n}"); WriteTextMessage(stream, "{\r\n\"id\" : \"session/list\"\r\n}");
string result = ReadPrefMessage(stream); string id = JSONParser.GetSessionID(ReadPrefMessage(stream), PCs);
Console.WriteLine(result);
//JSONParser.Parse(result); string tunnelCreate = "{\"id\" : \"tunnel/create\", \"data\" : {\"session\" : \"" + id + "\"}}";
WriteTextMessage(stream, tunnelCreate);
string tunnelResponse = ReadPrefMessage(stream);
Console.WriteLine(tunnelResponse);
string tunnelID = JSONParser.GetTunnelID(tunnelResponse);
Console.WriteLine("tunnelID is: " + tunnelID);
string sceneReset = "{\"id\" : \"tunnel/send\", \"data\" : {\"dest\" : \"" + tunnelID + "\",\"data\" :{\"id\" : \"scene/reset\",\"data\" : { }}}}}";
//string sceneReset = "{\"id\" : \"scene/reset\"}";
WriteTextMessage(stream, sceneReset);
Console.WriteLine(ReadPrefMessage(stream));
} }
} }
@@ -89,5 +100,10 @@ namespace RH_Engine
} }
public string host { get; } public string host { get; }
public string user { get; } public string user { get; }
public override string ToString()
{
return "PC - host:" + host + " - user:" + user;
}
} }
} }