diff --git a/ErgometerDoctorApplication/Client/ClientThread.cs b/ErgometerDoctorApplication/Client/ClientThread.cs index dbb6e4c..c47a5d2 100644 --- a/ErgometerDoctorApplication/Client/ClientThread.cs +++ b/ErgometerDoctorApplication/Client/ClientThread.cs @@ -13,17 +13,20 @@ namespace ErgometerDoctorApplication public int Session { get; } public string Name { get; } + public bool IsOldData { get; } + private SessionWindow window; - public List Metingen { get; } + public List Metingen { get; set; } public List Chat { get; } - public ClientThread(string name, int session) + public ClientThread(string name, int session, bool old) { Name = name; Session = session; + IsOldData = old; - window = new SessionWindow(Name, true, Session, this); + window = new SessionWindow(Name, old, Session, this); window.FormClosed += Window_FormClosed; Metingen = new List(); @@ -40,11 +43,14 @@ namespace ErgometerDoctorApplication switch (command.Type) { case NetCommand.CommandType.DATA: - lock(Metingen) + if (!IsOldData) { - Metingen.Add(command.Meting); + lock (Metingen) + { + Metingen.Add(command.Meting); + } + window.Invoke(window.updateMetingen, new Object[] { command.Meting }); } - window.Invoke(window.updateMetingen, new Object[] { command.Meting }); break; case NetCommand.CommandType.CHAT: ChatMessage chat = new ChatMessage(command.DisplayName, command.ChatMessage, false); @@ -67,7 +73,14 @@ namespace ErgometerDoctorApplication private void sendCommand(NetCommand command) { - MainClient.SendNetCommand(command); + 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 ed6b367..faded45 100644 --- a/ErgometerDoctorApplication/Client/SessionWindow.cs +++ b/ErgometerDoctorApplication/Client/SessionWindow.cs @@ -25,9 +25,9 @@ namespace ErgometerDoctorApplication public delegate void UpdateMetingen(Meting m); public UpdateMetingen updateMetingen; - public SessionWindow(string ClientName, bool ActiveSessionm, int session, ClientThread parentthread) + public SessionWindow(string ClientName, bool old, int session, ClientThread parentthread) { - this.ActiveSession = ActiveSession; + this.ActiveSession = !old; this.ClientName = ClientName; this.client = parentthread; Session = session; @@ -38,7 +38,8 @@ namespace ErgometerDoctorApplication InitializeComponent(); - Form.CheckForIllegalCrossThreadCalls = false; + if (!ActiveSession) + panelClientChat.Visible = false; } public void ClientApplicatie_Resize(object sender, EventArgs e) diff --git a/ErgometerDoctorApplication/ConActiveSessions.cs b/ErgometerDoctorApplication/ConActiveSessions.cs index 3dbb56e..a7a261c 100644 --- a/ErgometerDoctorApplication/ConActiveSessions.cs +++ b/ErgometerDoctorApplication/ConActiveSessions.cs @@ -83,6 +83,8 @@ namespace ErgometerDoctorApplication this.Controls.Add(flowlayout); //this.Controls.Add(data); + + updateActiveSessions(MainClient.activesessions); } diff --git a/ErgometerDoctorApplication/ConClientData.cs b/ErgometerDoctorApplication/ConClientData.cs index 35d5ed2..57293a9 100644 --- a/ErgometerDoctorApplication/ConClientData.cs +++ b/ErgometerDoctorApplication/ConClientData.cs @@ -1,4 +1,5 @@ -using System; +using ErgometerLibrary; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -9,8 +10,62 @@ namespace ErgometerDoctorApplication { public class ConClientData : Panel { + public System.Windows.Forms.TextBox textBoxPassword; + public System.Windows.Forms.TextBox textBoxUsername; + public System.Windows.Forms.Button buttonCreate; + public System.Windows.Forms.ListBox listUsers; + public ConClientData() : base() { + this.buttonCreate = new System.Windows.Forms.Button(); + this.textBoxPassword = new System.Windows.Forms.TextBox(); + this.textBoxUsername = new System.Windows.Forms.TextBox(); + this.listUsers = new System.Windows.Forms.ListBox(); + + // + // buttonLogin + // + this.buttonCreate.Anchor = System.Windows.Forms.AnchorStyles.None; + this.buttonCreate.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.buttonCreate.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.buttonCreate.Location = new System.Drawing.Point(0, -40); + this.buttonCreate.Name = "buttonCreate"; + this.buttonCreate.Size = new System.Drawing.Size(168, 31); + this.buttonCreate.TabIndex = 3; + this.buttonCreate.Text = "Aanmaken"; + this.buttonCreate.UseVisualStyleBackColor = true; + this.buttonCreate.Click += new System.EventHandler(this.buttonCreate_Click); + // + // textBoxPassword + // + this.textBoxPassword.Anchor = System.Windows.Forms.AnchorStyles.None; + this.textBoxPassword.Location = new System.Drawing.Point(0, 0); + this.textBoxPassword.MaxLength = 16; + this.textBoxPassword.Name = "textBoxPassword"; + this.textBoxPassword.Size = new System.Drawing.Size(167, 20); + this.textBoxPassword.TabIndex = 2; + // + // textBoxUsername + // + this.textBoxUsername.Anchor = System.Windows.Forms.AnchorStyles.None; + this.textBoxUsername.Location = new System.Drawing.Point(0, 40); + this.textBoxUsername.MaxLength = 16; + this.textBoxUsername.Name = "textBoxUsername"; + this.textBoxUsername.Size = new System.Drawing.Size(167, 20); + this.textBoxUsername.TabIndex = 2; + // + // listUsers + // + this.listUsers.Anchor = System.Windows.Forms.AnchorStyles.None; + this.listUsers.Location = new System.Drawing.Point(0, 80); + this.listUsers.Name = "listUsers"; + this.listUsers.Size = new System.Drawing.Size(200, 400); + this.listUsers.Items.Add("No Users"); + + this.Controls.Add(this.listUsers); + this.Controls.Add(this.buttonCreate); + this.Controls.Add(this.textBoxUsername); + this.Controls.Add(this.textBoxPassword); // // ConClientData // @@ -20,7 +75,25 @@ namespace ErgometerDoctorApplication this.Size = new System.Drawing.Size(584, 459); this.TabIndex = 0; + + this.BackColor = System.Drawing.Color.Red; } + + internal void updateUsers(Dictionary users) + { + this.listUsers.Items.Clear(); + + foreach(KeyValuePair user in users) + { + this.listUsers.Items.Add(user.Key + ": " + user.Value); + } + } + + private void buttonCreate_Click(object sender, EventArgs e) + { + MainClient.SendNetCommand(new NetCommand(textBoxUsername.Text, textBoxPassword.Text, MainClient.Session)); + MainClient.SendNetCommand(new NetCommand(NetCommand.RequestType.USERS, MainClient.Session)); + } } } diff --git a/ErgometerDoctorApplication/ConSessionLibrary.cs b/ErgometerDoctorApplication/ConSessionLibrary.cs deleted file mode 100644 index ca6e921..0000000 --- a/ErgometerDoctorApplication/ConSessionLibrary.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace ErgometerDoctorApplication -{ - public class ConSessionLibrary : Panel - { - public ConSessionLibrary() : base() - { - // - // ConSessionLibrary - // - this.Dock = System.Windows.Forms.DockStyle.Fill; - this.Location = new System.Drawing.Point(0, 0); - this.Name = "ConSessionLibrary"; - this.Size = new System.Drawing.Size(584, 459); - this.TabIndex = 0; - - this.BackColor = System.Drawing.Color.Blue; - } - } -} diff --git a/ErgometerDoctorApplication/ErgometerDoctorApplication.csproj b/ErgometerDoctorApplication/ErgometerDoctorApplication.csproj index 6f46a08..a4e925d 100644 --- a/ErgometerDoctorApplication/ErgometerDoctorApplication.csproj +++ b/ErgometerDoctorApplication/ErgometerDoctorApplication.csproj @@ -83,9 +83,6 @@ Component - - Component - Form diff --git a/ErgometerDoctorApplication/MainClient.cs b/ErgometerDoctorApplication/MainClient.cs index fec7f5c..1aa634c 100644 --- a/ErgometerDoctorApplication/MainClient.cs +++ b/ErgometerDoctorApplication/MainClient.cs @@ -62,7 +62,7 @@ namespace ErgometerDoctorApplication { Server.Connect(HOST, PORT); } - catch(Exception e) + catch (Exception e) { error = "Server is niet online."; return false; @@ -121,19 +121,31 @@ namespace ErgometerDoctorApplication if (Server.Connected && Server.Available > 0) { NetCommand command = NetHelper.ReadNetCommand(Server); - switch(command.Type) + Console.WriteLine(command.Type.ToString()); + switch (command.Type) { case NetCommand.CommandType.LENGTH: - switch(command.Length) + Console.WriteLine(command.Length.ToString() + " : " + command.LengthValue); + switch (command.Length) { case NetCommand.LengthType.USERS: - users.Clear(); - for(int i=0; i 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"); } @@ -170,7 +197,7 @@ namespace ErgometerDoctorApplication HandToClient(command); break; } - + } } @@ -179,15 +206,26 @@ namespace ErgometerDoctorApplication private static void HandToClient(NetCommand command) { - foreach(ClientThread cl in clients) + foreach (ClientThread cl in clients) { - if(cl.Session == command.Session) + if (cl.Session == command.Session) { cl.HandleCommand(command); } } } + 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); @@ -195,7 +233,7 @@ namespace ErgometerDoctorApplication private static bool IsSessionRunning(int session) { - foreach(ClientThread cl in clients) + foreach (ClientThread cl in clients) { if (cl.Session == session) return true; @@ -210,7 +248,24 @@ namespace ErgometerDoctorApplication return; //Start new client - ClientThread cl = new ClientThread(name, session); + ClientThread cl = new ClientThread(name, session, false); + clients.Add(cl); + + //Run client on new thread + Thread thread = new Thread(new ThreadStart(cl.run)); + thread.IsBackground = true; + thread.Start(); + } + + public static void StartOldCLient(string name, int session) + { + if (IsSessionRunning(session)) + return; + + SendNetCommand(new NetCommand(NetCommand.RequestType.OLDDATA, session)); + + //Start new client + ClientThread cl = new ClientThread(name, session, true); clients.Add(cl); //Run client on new thread diff --git a/ErgometerDoctorApplication/MainWindow.Designer.cs b/ErgometerDoctorApplication/MainWindow.Designer.cs index cbd8913..2d1cf0d 100644 --- a/ErgometerDoctorApplication/MainWindow.Designer.cs +++ b/ErgometerDoctorApplication/MainWindow.Designer.cs @@ -33,7 +33,6 @@ namespace ErgometerDoctorApplication { this.MenuPanel = new System.Windows.Forms.Panel(); this.BtnLogout = new System.Windows.Forms.Button(); - this.BtnSessionLibrary = new System.Windows.Forms.Button(); this.BtnSessionHistory = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); @@ -51,7 +50,6 @@ namespace ErgometerDoctorApplication this.conActiveSessions = new ErgometerDoctorApplication.ConActiveSessions(); this.conClientData = new ErgometerDoctorApplication.ConClientData(); this.conSessionHistory = new ErgometerDoctorApplication.ConSessionHistory(); - this.conSessionLibrary = new ErgometerDoctorApplication.ConSessionLibrary(); this.conPanelLogin = new ErgometerDoctorApplication.ConPanelLogin(this); this.updateTimer = new Timer(); MainContainer.Visible = false; @@ -63,7 +61,6 @@ namespace ErgometerDoctorApplication // this.MenuPanel.BackColor = System.Drawing.SystemColors.ControlLight; this.MenuPanel.Controls.Add(this.BtnLogout); - this.MenuPanel.Controls.Add(this.BtnSessionLibrary); this.MenuPanel.Controls.Add(this.BtnSessionHistory); this.MenuPanel.Controls.Add(this.label2); this.MenuPanel.Controls.Add(this.label1); @@ -92,21 +89,6 @@ namespace ErgometerDoctorApplication this.BtnLogout.UseVisualStyleBackColor = false; this.BtnLogout.Click += new System.EventHandler(this.buttonLogout_Click); // - // BtnSessionLibrary - // - this.BtnSessionLibrary.BackColor = System.Drawing.Color.DarkGray; - this.BtnSessionLibrary.Dock = System.Windows.Forms.DockStyle.Top; - this.BtnSessionLibrary.FlatAppearance.BorderSize = 0; - this.BtnSessionLibrary.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.BtnSessionLibrary.ForeColor = System.Drawing.Color.White; - this.BtnSessionLibrary.Location = new System.Drawing.Point(0, 205); - this.BtnSessionLibrary.Name = "BtnSessionLibrary"; - this.BtnSessionLibrary.Size = new System.Drawing.Size(200, 35); - this.BtnSessionLibrary.TabIndex = 8; - this.BtnSessionLibrary.Text = "Sessie Bibliotheek"; - this.BtnSessionLibrary.UseVisualStyleBackColor = false; - this.BtnSessionLibrary.Click += new System.EventHandler(this.BtnSessionLibrary_Click); - // // BtnSessionHistory // this.BtnSessionHistory.BackColor = System.Drawing.Color.DarkGray; @@ -262,7 +244,6 @@ namespace ErgometerDoctorApplication this.MainContainer.Controls.Add(this.conActiveSessions); this.MainContainer.Controls.Add(this.conClientData); this.MainContainer.Controls.Add(this.conSessionHistory); - this.MainContainer.Controls.Add(this.conSessionLibrary); this.MainContainer.Dock = System.Windows.Forms.DockStyle.Fill; this.MainContainer.Location = new System.Drawing.Point(200, 102); this.MainContainer.Name = "MainContainer"; @@ -297,7 +278,6 @@ namespace ErgometerDoctorApplication private ToolStripMenuItem editToolStripMenuItem; private ToolStripMenuItem viewToolStripMenuItem; private Button BtnActiveSessions; - private Button BtnSessionLibrary; private Button BtnLogout; private Button BtnSessionHistory; private Button BtnClientData; @@ -311,7 +291,6 @@ namespace ErgometerDoctorApplication public ConActiveSessions conActiveSessions; public ConSessionHistory conSessionHistory; public ConClientData conClientData; - public ConSessionLibrary conSessionLibrary; public ConPanelLogin conPanelLogin; } diff --git a/ErgometerDoctorApplication/MainWindow.cs b/ErgometerDoctorApplication/MainWindow.cs index 57b1f60..02a6e98 100644 --- a/ErgometerDoctorApplication/MainWindow.cs +++ b/ErgometerDoctorApplication/MainWindow.cs @@ -13,11 +13,15 @@ namespace ErgometerDoctorApplication { public partial class MainWindow : Form { + private bool request; + public MainWindow() { InitializeComponent(); conPanelLogin.BringToFront(); - updateTimer.Start(); + request = false; + //updateTimer.Start(); + } private void BtnActiveSessions_Click(object sender, EventArgs e) @@ -46,22 +50,23 @@ namespace ErgometerDoctorApplication */ } - private void BtnSessionLibrary_Click(object sender, EventArgs e) - { - this.HeaderLabel.Text = "Bibliotheek"; - conSessionLibrary.BringToFront(); - } - private void BtnClientData_Click(object sender, EventArgs e) { this.HeaderLabel.Text = "Clientenbestand"; conClientData.BringToFront(); + + if (MainClient.users.Count > 0) + { + conClientData.updateUsers(MainClient.users); + } } private void BtnSessionHistory_Click(object sender, EventArgs e) { this.HeaderLabel.Text = "Sessie geschiedenis"; conSessionHistory.BringToFront(); + + MainClient.StartOldCLient("Test", 2001739555); } public void validateLogin() { @@ -108,7 +113,12 @@ namespace ErgometerDoctorApplication private void updateTimer_tick(object sender, EventArgs e) { - MainClient.SendNetCommand(new NetCommand(NetCommand.RequestType.SESSIONDATA, MainClient.Session)); + if (request) + MainClient.SendNetCommand(new NetCommand(NetCommand.RequestType.USERS, MainClient.Session)); + else + MainClient.SendNetCommand(new NetCommand(NetCommand.RequestType.SESSIONDATA, MainClient.Session)); + + request = !request; } } }