diff --git a/RH-Engine/JSONParser.cs b/RH-Engine/JSONParser.cs index d42adb6..248ba73 100644 --- a/RH-Engine/JSONParser.cs +++ b/RH-Engine/JSONParser.cs @@ -10,20 +10,23 @@ namespace RH_Engine class JSONParser { /// - /// parses the given response from the server into strings + /// returns all the users from the given response /// /// the message gotten from the server, without the length prefix /// - public static string[] Parse(string msg) + public static PC[] GetUsers(string msg) { dynamic jsonData = JsonConvert.DeserializeObject(msg); Newtonsoft.Json.Linq.JArray data = jsonData.data; + PC[] res = new PC[data.Count]; + int counter = 0; 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; 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) { + Console.WriteLine("connecting to {0}, on {1} with id {2}", pc.user, pc.host, d.id); return d.id; } } @@ -45,6 +49,16 @@ namespace RH_Engine return null; } + public static string GetTunnelID(string json) + { + dynamic jsonData = JsonConvert.DeserializeObject(json); + if (jsonData.data.status == "ok") + { + return jsonData.data.id; + } + return null; + } + } } diff --git a/RH-Engine/Program.cs b/RH-Engine/Program.cs index a890844..5325fad 100644 --- a/RH-Engine/Program.cs +++ b/RH-Engine/Program.cs @@ -37,7 +37,7 @@ namespace RH_Engine stream.Write(res); - Console.WriteLine("sent message " + message); + //Console.WriteLine("sent message " + message); } public static string ReadPrefMessage(NetworkStream stream) @@ -48,7 +48,7 @@ namespace RH_Engine int length = BitConverter.ToInt32(lengthBytes); - Console.WriteLine("length is: " + length); + //Console.WriteLine("length is: " + length); byte[] buffer = new byte[length]; int totalRead = 0; @@ -58,7 +58,7 @@ namespace RH_Engine { int read = stream.Read(buffer, totalRead, buffer.Length - totalRead); totalRead += read; - Console.WriteLine("ReadMessage: " + read); + //Console.WriteLine("ReadMessage: " + read); } while (totalRead < length); return Encoding.UTF8.GetString(buffer, 0, totalRead); @@ -66,17 +66,28 @@ namespace RH_Engine 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}"); - string result = ReadPrefMessage(stream); - Console.WriteLine(result); - //JSONParser.Parse(result); + string id = JSONParser.GetSessionID(ReadPrefMessage(stream), PCs); + + 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 user { get; } + + public override string ToString() + { + return "PC - host:" + host + " - user:" + user; + } } } \ No newline at end of file