diff --git a/Client/Client.cs b/Client/Client.cs index 9115f01..e4d1d77 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -38,6 +38,7 @@ namespace Client { engineConnection = EngineConnection.INSTANCE; engineConnection.OnNoTunnelId = retryEngineConnection; + engineConnection.OnSuccessFullConnection = engineConnected; if (!engineConnection.Connected) engineConnection.Connect(); } @@ -53,6 +54,12 @@ namespace Client engineConnection.CreateConnection(); } + private void engineConnected() + { + engineConnection.initScene(); + if (this.sessionRunning) engineConnection.StartRouteFollow(); + } + /// /// callback method for when the TCP client is connected /// @@ -193,7 +200,6 @@ namespace Client throw new ArgumentNullException("no bytes"); } byte[] message = DataParser.GetRawDataMessage(bytes); - Console.WriteLine("got bpm message: " + message); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } @@ -212,7 +218,6 @@ namespace Client throw new ArgumentNullException("no bytes"); } byte[] message = DataParser.GetRawDataMessage(bytes); - Console.WriteLine("got bike message: " + message); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } diff --git a/Client/EngineConnection.cs b/Client/EngineConnection.cs index 4905911..a0222a1 100644 --- a/Client/EngineConnection.cs +++ b/Client/EngineConnection.cs @@ -8,12 +8,14 @@ namespace Client { public delegate void HandleSerial(string message); public delegate void HandleNoTunnelId(); + public delegate void OnSuccessfullConnection(); public sealed class EngineConnection { private static EngineConnection instance = null; private static readonly object padlock = new object(); public HandleNoTunnelId OnNoTunnelId; + public OnSuccessfullConnection OnSuccessFullConnection; private static PC[] PCs = { @@ -29,9 +31,11 @@ namespace Client private static ServerResponseReader serverResponseReader; private static string sessionId = string.Empty; private static string tunnelId = string.Empty; + private static string cameraId = string.Empty; private static string routeId = string.Empty; private static string panelId = string.Empty; private static string bikeId = string.Empty; + private static string headId = string.Empty; private static NetworkStream stream; @@ -45,6 +49,9 @@ namespace Client } + /// + /// Singleton constructor + /// public static EngineConnection INSTANCE { get @@ -60,6 +67,11 @@ namespace Client } } + + + /// + /// connects to the vr engine and initalizes the serverResponseReader + /// public void Connect() { TcpClient client = new TcpClient("145.48.6.10", 6666); @@ -68,6 +80,19 @@ namespace Client CreateConnection(); } + /// + /// initializes and starts the reading of the responses from the vr server + /// + /// the networkstream + private void initReader() + { + serverResponseReader = new ServerResponseReader(stream); + serverResponseReader.callback = HandleResponse; + serverResponseReader.StartRead(); + Connected = true; + } + + #region VR Message traffic /// /// connects to the server and creates the tunnel /// @@ -89,20 +114,10 @@ namespace Client if (tunnelId != null) { Write("got tunnel id! " + tunnelId); + OnSuccessFullConnection?.Invoke(); } - mainCommand = new Command(tunnelId); - } - /// - /// initializes and starts the reading of the responses from the vr server - /// - /// the networkstream - private void initReader() - { - serverResponseReader = new ServerResponseReader(stream); - serverResponseReader.callback = HandleResponse; - serverResponseReader.StartRead(); - Connected = true; + } /// @@ -117,6 +132,7 @@ namespace Client if (id == "session/list") { sessionId = JSONParser.GetSessionID(message, PCs); + Write("got session id"); } else if (id == "tunnel/create") { @@ -139,6 +155,39 @@ namespace Client } } + public void initScene() + { + mainCommand = new Command(tunnelId); + + // reset the scene + WriteTextMessage(mainCommand.ResetScene()); + + //Get sceneinfo and set the id's + SendMessageAndOnResponse(mainCommand.GetSceneInfoCommand("sceneinfo"), "sceneinfo", + (message) => + { + //Console.WriteLine("\r\n\r\n\r\nscene info" + message); + cameraId = JSONParser.GetIdSceneInfoChild(message, "Camera"); + string headId = JSONParser.GetIdSceneInfoChild(message, "Head"); + string handLeftId = JSONParser.GetIdSceneInfoChild(message, "LeftHand"); + string handRightId = JSONParser.GetIdSceneInfoChild(message, "RightHand"); + + //Force(stream, mainCommand.DeleteNode(handLeftId, "deleteHandL"), "deleteHandL", (message) => Console.WriteLine("Left hand deleted")); + //Force(stream, mainCommand.DeleteNode(handRightId, "deleteHandR"), "deleteHandR", (message) => Console.WriteLine("Right hand deleted")); + }); + // add the route and set the route id + SendMessageAndOnResponse(mainCommand.RouteCommand("routeID"), "routeID", (message) => routeId = JSONParser.GetResponseUuid(message)); + } + + internal void StartRouteFollow() + { + throw new NotImplementedException(); + } + + #endregion + + #region message send/receive + /// /// method that sends the speciefied message with the specified serial, and executes the given action upon receivind a reply from the server with this serial. /// @@ -169,6 +218,8 @@ namespace Client //Write("sent message " + message); } + + #endregion public void Write(string msg) { Console.WriteLine( "[ENGINECONNECT] " + msg); diff --git a/Client/Program.cs b/Client/Program.cs index 303b929..80b7fd8 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -11,7 +11,7 @@ namespace Client { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + Console.WriteLine("// Connecting... //"); //connect fiets? Client client = new Client(); diff --git a/Server/Client.cs b/Server/Client.cs index a38f747..3fa7ba0 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -142,7 +142,9 @@ namespace Server } else if (DataParser.isRawData(message)) { + // print the raw data Console.WriteLine(BitConverter.ToString(payloadbytes)); + // TODO change, checking for length is not that safe if (payloadbytes.Length == 8) { saveData?.WriteDataRAWBike(payloadbytes);