From 48b9e1365e2a00342ccc4052ec5792f28215557c Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Tue, 20 Oct 2015 11:19:08 +0200 Subject: [PATCH] Fixed old client data --- .../Client/ClientThread.cs | 15 +- .../Client/SessionWindow.cs | 9 + .../ConActiveSessions.cs | 2 +- ErgometerDoctorApplication/MainClient.cs | 169 +++++++++--------- ErgometerDoctorApplication/MainWindow.cs | 5 +- 5 files changed, 97 insertions(+), 103 deletions(-) diff --git a/ErgometerDoctorApplication/Client/ClientThread.cs b/ErgometerDoctorApplication/Client/ClientThread.cs index c47a5d2..19025af 100644 --- a/ErgometerDoctorApplication/Client/ClientThread.cs +++ b/ErgometerDoctorApplication/Client/ClientThread.cs @@ -43,14 +43,11 @@ namespace ErgometerDoctorApplication switch (command.Type) { case NetCommand.CommandType.DATA: - if (!IsOldData) + lock (Metingen) { - lock (Metingen) - { - Metingen.Add(command.Meting); - } - window.Invoke(window.updateMetingen, new Object[] { command.Meting }); + Metingen.Add(command.Meting); } + window.Invoke(window.updateMetingen, new Object[] { command.Meting }); break; case NetCommand.CommandType.CHAT: ChatMessage chat = new ChatMessage(command.DisplayName, command.ChatMessage, false); @@ -76,12 +73,6 @@ namespace ErgometerDoctorApplication if(! IsOldData) MainClient.SendNetCommand(command); } - - public void HandMetingen(List metingen) - { - Metingen = metingen; - window.Invoke(window.updateMetingen, new Object[] { new Meting(0,0,0,0,0,0,0,0,0) }); - } } } diff --git a/ErgometerDoctorApplication/Client/SessionWindow.cs b/ErgometerDoctorApplication/Client/SessionWindow.cs index faded45..1b9f811 100644 --- a/ErgometerDoctorApplication/Client/SessionWindow.cs +++ b/ErgometerDoctorApplication/Client/SessionWindow.cs @@ -25,6 +25,9 @@ namespace ErgometerDoctorApplication public delegate void UpdateMetingen(Meting m); public UpdateMetingen updateMetingen; + public delegate void UpdateGraph(); + public UpdateGraph updateGraph; + public SessionWindow(string ClientName, bool old, int session, ClientThread parentthread) { this.ActiveSession = !old; @@ -35,6 +38,7 @@ namespace ErgometerDoctorApplication count = 0; updateMetingen = new UpdateMetingen(this.SaveMeting); + updateGraph = new UpdateGraph(this.LoadGraph); InitializeComponent(); @@ -85,5 +89,10 @@ namespace ErgometerDoctorApplication count++; } + + public void LoadGraph() + { + panelGraphView.updateAllCharts(client.Metingen); + } } } diff --git a/ErgometerDoctorApplication/ConActiveSessions.cs b/ErgometerDoctorApplication/ConActiveSessions.cs index a7a261c..4fbb9bd 100644 --- a/ErgometerDoctorApplication/ConActiveSessions.cs +++ b/ErgometerDoctorApplication/ConActiveSessions.cs @@ -64,7 +64,7 @@ namespace ErgometerDoctorApplication this.labelActiveSessions.Anchor = System.Windows.Forms.AnchorStyles.None; this.labelActiveSessions.AutoSize = true; this.labelActiveSessions.Font = new System.Drawing.Font("Segoe UI Semibold", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.labelActiveSessions.Location = new System.Drawing.Point(0, 0); + this.labelActiveSessions.Location = new System.Drawing.Point(0, 50); this.labelActiveSessions.Name = "labelActiveSessions"; this.labelActiveSessions.Size = new System.Drawing.Size(103, 21); this.labelActiveSessions.TabIndex = 3; diff --git a/ErgometerDoctorApplication/MainClient.cs b/ErgometerDoctorApplication/MainClient.cs index 1aa634c..e164262 100644 --- a/ErgometerDoctorApplication/MainClient.cs +++ b/ErgometerDoctorApplication/MainClient.cs @@ -96,6 +96,7 @@ namespace ErgometerDoctorApplication loggedin = true; } + SendNetCommand(new NetCommand(NetCommand.RequestType.USERS, Session)); SendNetCommand(new NetCommand(NetCommand.RequestType.SESSIONDATA, Session)); return true; @@ -118,85 +119,11 @@ namespace ErgometerDoctorApplication { while (running) { - if (Server.Connected && Server.Available > 0) + if (loggedin && Server.Connected && Server.Available > 0) { NetCommand command = NetHelper.ReadNetCommand(Server); - Console.WriteLine(command.Type.ToString()); - switch (command.Type) - { - case NetCommand.CommandType.LENGTH: - Console.WriteLine(command.Length.ToString() + " : " + command.LengthValue); - switch (command.Length) - { - case NetCommand.LengthType.USERS: - lock (users) - { - users.Clear(); - for (int i = 0; i < command.LengthValue; i++) - { - NetCommand c = NetHelper.ReadNetCommand(Server); - if (c.Type == NetCommand.CommandType.USER) - { - try - { - users.Add(c.DisplayName, c.Password); - } - catch (ArgumentException e) - { - // ignore double username - } - } - } - } - break; - case NetCommand.LengthType.SESSIONS: - oldsessions.Clear(); - for (int i = 0; i < command.LengthValue; i++) - { - NetCommand c = NetHelper.ReadNetCommand(Server); - if (c.Type == NetCommand.CommandType.SESSION) - { - oldsessions.Add(c.Session); - } - } - break; - case NetCommand.LengthType.SESSIONDATA: - activesessions.Clear(); - for (int i = 0; i < command.LengthValue; i++) - { - NetCommand c = NetHelper.ReadNetCommand(Server); - if (c.Type == NetCommand.CommandType.SESSIONDATA) - { - activesessions.Add(c.Session, c.DisplayName); - } - } - break; - case NetCommand.LengthType.DATA: - Console.WriteLine(command); - List metingen = new List(); - for (int i = 0; i < command.LengthValue; i++) - { - NetCommand c = NetHelper.ReadNetCommand(Server); - Console.WriteLine(c); - if (c.Type == NetCommand.CommandType.DATA) - { - metingen.Add(c.Meting); - } - } - - HandMetingenToClient(metingen, command.Session); - break; - default: - throw new FormatException("Error in NetCommand: Length type is not recognised"); - } - break; - case NetCommand.CommandType.ERROR: - Console.WriteLine("An error occured, ignoring"); - break; - default: - HandToClient(command); - break; - } + Console.WriteLine(command.Type.ToString() + " # " + command); + HandleNetCommand(command); } } @@ -204,6 +131,83 @@ namespace ErgometerDoctorApplication Server.Close(); } + private static bool UsersBeingSent = false; + private static int UsersSent = 0; + private static int UsersLength = 0; + + private static bool SessionsBeingSent = false; + private static int SessionsSent = 0; + private static int SessionsLength = 0; + + private static bool ActiveSessionsBeingSent = false; + private static int ActiveSessionsSent = 0; + private static int ActiveSessionsLength = 0; + + private static void HandleNetCommand(NetCommand command) + { + switch (command.Type) + { + case NetCommand.CommandType.LENGTH: + switch (command.Length) + { + case NetCommand.LengthType.USERS: + users.Clear(); + UsersBeingSent = true; + UsersSent = 0; + UsersLength = command.LengthValue; + break; + case NetCommand.LengthType.SESSIONS: + oldsessions.Clear(); + SessionsBeingSent = true; + SessionsSent = 0; + SessionsLength = command.LengthValue; + break; + case NetCommand.LengthType.SESSIONDATA: + activesessions.Clear(); + ActiveSessionsBeingSent = true; + ActiveSessionsSent = 0; + ActiveSessionsLength = command.LengthValue; + break; + default: + throw new FormatException("Error in NetCommand: Length type is not recognised"); + } + break; + case NetCommand.CommandType.ERROR: + Console.WriteLine("An error occured, ignoring"); + break; + case NetCommand.CommandType.USER: + if(UsersBeingSent) + { + users.Add(command.DisplayName, command.Password); + UsersSent++; + if (UsersSent >= UsersLength) + UsersBeingSent = false; + } + break; + case NetCommand.CommandType.SESSION: + if (SessionsBeingSent) + { + oldsessions.Add(command.Session); + SessionsSent++; + if (SessionsSent >= SessionsLength) + SessionsBeingSent = false; + } + break; + case NetCommand.CommandType.SESSIONDATA: + if (ActiveSessionsBeingSent) + { + activesessions.Add(command.Session, command.DisplayName); + ActiveSessionsSent++; + if (ActiveSessionsSent >= ActiveSessionsLength) + ActiveSessionsBeingSent = false; + } + break; + default: + HandToClient(command); + break; + } + } + private static void HandToClient(NetCommand command) { foreach (ClientThread cl in clients) @@ -215,17 +219,6 @@ namespace ErgometerDoctorApplication } } - private static void HandMetingenToClient(List metingen, int session) - { - foreach (ClientThread cl in clients) - { - if (cl.Session == session) - { - cl.HandMetingen(metingen); - } - } - } - public static void SendNetCommand(NetCommand command) { NetHelper.SendNetCommand(Server, command); diff --git a/ErgometerDoctorApplication/MainWindow.cs b/ErgometerDoctorApplication/MainWindow.cs index 02a6e98..2892d86 100644 --- a/ErgometerDoctorApplication/MainWindow.cs +++ b/ErgometerDoctorApplication/MainWindow.cs @@ -20,7 +20,7 @@ namespace ErgometerDoctorApplication InitializeComponent(); conPanelLogin.BringToFront(); request = false; - //updateTimer.Start(); + updateTimer.Start(); } @@ -37,6 +37,7 @@ namespace ErgometerDoctorApplication } else { + conActiveSessions.updateActiveSessions(MainClient.activesessions); conActiveSessions.labelActiveSessions.Text = "Er zijn geen actieve sessies."; } @@ -66,7 +67,7 @@ namespace ErgometerDoctorApplication this.HeaderLabel.Text = "Sessie geschiedenis"; conSessionHistory.BringToFront(); - MainClient.StartOldCLient("Test", 2001739555); + MainClient.StartOldCLient("Test", 1148735907); } public void validateLogin() {