diff --git a/ClientApp/Utils/Client.cs b/ClientApp/Utils/Client.cs index 2ce0036..69f9f1b 100644 --- a/ClientApp/Utils/Client.cs +++ b/ClientApp/Utils/Client.cs @@ -301,6 +301,7 @@ namespace ClientApp.Utils this.stream.Dispose(); this.client.Dispose(); this.handler.stop(); + this.engineConnection.Stop(); } } } diff --git a/ClientApp/Utils/EngineConnection.cs b/ClientApp/Utils/EngineConnection.cs index 3706e87..c852058 100644 --- a/ClientApp/Utils/EngineConnection.cs +++ b/ClientApp/Utils/EngineConnection.cs @@ -178,7 +178,7 @@ namespace ClientApp.Utils Console.WriteLine("set tunnel id to " + tunnelId); if (tunnelId == null) { - Write("could not find a valid tunnel id!"); + //Write("could not find a valid tunnel id!"); OnNoTunnelId?.Invoke(); Connected = false; FollowingRoute = false; @@ -256,9 +256,9 @@ namespace ClientApp.Utils WriteTextMessage(mainCommand.ShowRoute("showRouteFalse", false)); }); }); - setEnvironment(); + setEnvironment(); + - } private void setEnvironment() @@ -348,7 +348,7 @@ namespace ClientApp.Utils ImprovedPerlin improvedPerlin = new ImprovedPerlin(0, LibNoise.NoiseQuality.Best); for (int i = 0; i < 256 * 256; i++) { - height[i] = improvedPerlin.GetValue(x /10, x / 10, x * 100) / 3.5f + 1; + height[i] = improvedPerlin.GetValue(x / 10, x / 10, x * 100) / 3.5f + 1; //if (height[i] > 1.1f) //{ diff --git a/DoctorApp/Utils/Client.cs b/DoctorApp/Utils/Client.cs index 5ddc8c3..4477b82 100644 --- a/DoctorApp/Utils/Client.cs +++ b/DoctorApp/Utils/Client.cs @@ -10,7 +10,7 @@ using Util; namespace DoctorApp.Utils { public delegate void EngineCallback(); - public class Client : IDataReceiver + public class Client { private TcpClient client; private NetworkStream stream; @@ -18,8 +18,6 @@ namespace DoctorApp.Utils private bool connected; private byte[] totalBuffer = new byte[1024]; private int totalBufferReceived = 0; - private bool sessionRunning = false; - private IHandler handler = null; private LoginViewModel LoginViewModel; private MainViewModel MainViewModel; private ClientInfoViewModel ClientInfoViewModel; @@ -83,6 +81,8 @@ namespace DoctorApp.Utils string identifier; bool isJson = DataParser.getJsonIdentifier(messageBytes, out identifier); + + Debug.WriteLine("doctor " + Encoding.ASCII.GetString(payloadbytes)); if (isJson) { switch (identifier) @@ -104,28 +104,15 @@ namespace DoctorApp.Utils break; /*case DataParser.START_SESSION: Console.WriteLine("Session started!"); - this.sessionRunning = true; - sendMessage(DataParser.getStartSessionJson()); break; case DataParser.STOP_SESSION: Console.WriteLine("Stop session identifier"); - this.sessionRunning = false; - sendMessage(DataParser.getStopSessionJson()); - break;*/ + break; case DataParser.SET_RESISTANCE: Console.WriteLine("Set resistance identifier"); - if (this.handler == null) - { - Console.WriteLine("handler is null"); - sendMessage(DataParser.getSetResistanceResponseJson(false)); - } - else - { - this.handler.setResistance(DataParser.getResistanceFromJson(payloadbytes)); - sendMessage(DataParser.getSetResistanceResponseJson(true)); - } break; case DataParser.NEW_CONNECTION: + Debug.WriteLine("doctor client new connection"); this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes)); break; case DataParser.DISCONNECT: @@ -167,63 +154,6 @@ namespace DoctorApp.Utils this.stream.EndWrite(ar); } - #region interface - //maybe move this to other place - /// - /// bpm method for receiving the BPM value from the bluetooth bike or the simulation - /// - /// the message - public void BPM(byte[] bytes) - { - if (!sessionRunning) - { - return; - } - if (bytes == null) - { - throw new ArgumentNullException("no bytes"); - } - byte[] message = DataParser.GetRawDataMessage(bytes); - - - this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); - } - - /// - /// method for receiving the bike message from the bluetooth bike or the simulation - /// - /// the message - public void Bike(byte[] bytes) - { - - if (!sessionRunning) - { - return; - } - if (bytes == null) - { - throw new ArgumentNullException("no bytes"); - } - byte[] message = DataParser.GetRawDataMessage(bytes); - - /* switch (bytes[0]) - { - - case 0x10: - - if (canSendToEngine) engineConnection.BikeSpeed = (bytes[4] | (bytes[5] << 8)) * 0.01f; - break; - case 0x19: - if (canSendToEngine) engineConnection.BikePower = (bytes[5]) | (bytes[6] & 0b00001111) << 8; - break; - }*/ - - - this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); - } - - #endregion - /// /// wether or not the client stream is connected /// @@ -246,15 +176,6 @@ namespace DoctorApp.Utils this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } - /// - /// sets the handler for the client, so either the bike simulator or the bluetooth bike handler - /// - /// - public void SetHandler(IHandler handler) - { - this.handler = handler; - } - internal void SetLoginViewModel(LoginViewModel loginViewModel) { this.LoginViewModel = loginViewModel; @@ -275,7 +196,6 @@ namespace DoctorApp.Utils Debug.WriteLine("client dispose called"); this.stream.Dispose(); this.client.Dispose(); - this.handler?.stop(); } } } diff --git a/DoctorApp/ViewModels/MainViewModel.cs b/DoctorApp/ViewModels/MainViewModel.cs index 7888a1d..90d4509 100644 --- a/DoctorApp/ViewModels/MainViewModel.cs +++ b/DoctorApp/ViewModels/MainViewModel.cs @@ -26,6 +26,7 @@ namespace DoctorApp.ViewModels public void NewConnectedUser(string username) { + System.Diagnostics.Debug.WriteLine("CREATING TAB FOR " + username); App.Current.Dispatcher.Invoke((Action)delegate { Tabs.Add(new ClientInfoViewModel(MainWindowViewModel) @@ -52,5 +53,5 @@ namespace DoctorApp.ViewModels } } - + } diff --git a/Hashing/DataParser.cs b/Hashing/DataParser.cs index 35d4b0a..20851f9 100644 --- a/Hashing/DataParser.cs +++ b/Hashing/DataParser.cs @@ -234,6 +234,8 @@ namespace Util public static byte[] getNewConnectionJson(string user) { + if (user == null) + throw new ArgumentNullException("user null"); dynamic data = new { username = user diff --git a/Server/Client.cs b/Server/Client.cs index fafa1d2..4d67c2b 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -5,6 +5,7 @@ using System.Text; using Newtonsoft.Json; using System.Diagnostics; using Util; +using System.Linq; namespace Server { @@ -96,16 +97,17 @@ namespace Server switch (identifier) { case DataParser.LOGIN: - handleLogin(payloadbytes); + if (handleLogin(payloadbytes)) + communication.NewLogin(this); break; case DataParser.LOGIN_DOCTOR: - if (communication.doctor != null) + if (communication.Doctor != null) return; if (handleLogin(payloadbytes)) { - communication.doctor = this; - Console.WriteLine("Set doctor to " + communication.doctor + " , this is " + this); + communication.Doctor = this; + Console.WriteLine("Set doctor to " + communication.Doctor + " , this is " + this); } break; case DataParser.START_SESSION: @@ -135,7 +137,7 @@ namespace Server else if (DataParser.isRawData(message)) { // print the raw data - Console.WriteLine(BitConverter.ToString(payloadbytes)); + //Console.WriteLine(BitConverter.ToString(payloadbytes)); // TODO change, checking for length is not that safe if (payloadbytes.Length == 8) { @@ -167,7 +169,6 @@ namespace Server this.username = username; sendMessage(DataParser.getLoginResponse("OK")); //sendMessage(DataParser.getStartSessionJson()); - communication.NewLogin(this); return true; } else @@ -184,6 +185,7 @@ namespace Server public void sendMessage(byte[] message) { + Debug.WriteLine("serverclient " + Encoding.ASCII.GetString(message.Skip(5).ToArray())); stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } diff --git a/Server/Communication.cs b/Server/Communication.cs index eea8392..1641174 100644 --- a/Server/Communication.cs +++ b/Server/Communication.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO.Pipes; using System.Linq; using System.Net.Sockets; @@ -12,7 +13,25 @@ namespace Server { private TcpListener listener; private List clients; - public Client doctor; + private Client mDoctor; + public Client Doctor + { + get + { + return this.mDoctor; + } + set + { + this.mDoctor = value; + this.clients.ForEach((client) => + { + Debug.WriteLine("foreach called for " + client.username); + byte[] dinges = DataParser.getNewConnectionJson(client.username); + Debug.WriteLine("foreach " + Encoding.ASCII.GetString(dinges.Skip(5).ToArray())); + this.mDoctor.sendMessage(dinges); + }); + } + } public Communication(TcpListener listener) { this.listener = listener; @@ -33,7 +52,7 @@ namespace Server var tcpClient = listener.EndAcceptTcpClient(ar); Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}"); - clients.Add(new Client(this, tcpClient)); + new Client(this, tcpClient); listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null); } @@ -45,15 +64,20 @@ namespace Server public void NewLogin(Client client) { - if (doctor == null) + this.clients.Add(client); + Debug.WriteLine("amount of clients is now " + this.clients.Count); + var dinges = DataParser.getNewConnectionJson(client.username); + Debug.WriteLine("new login" + Encoding.ASCII.GetString(dinges)); + Doctor?.sendMessage(dinges); + } + + public void LogOff(Client client) + { + if (this.Doctor == client) { - doctor = client; + this.Doctor = null; } - else - { - doctor.sendMessage(DataParser.getNewConnectionJson(client.username)); - } - + this.clients.Remove(client); } public void StartSessionUser(string user)