diff --git a/ErgometerDoctorApplication/ClientThread.cs b/ErgometerDoctorApplication/ClientThread.cs index 418043d..88ce734 100644 --- a/ErgometerDoctorApplication/ClientThread.cs +++ b/ErgometerDoctorApplication/ClientThread.cs @@ -1,8 +1,10 @@ -using System; +using ErgometerLibrary; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Forms; namespace ErgometerDoctorApplication { @@ -11,10 +13,44 @@ namespace ErgometerDoctorApplication public int Session { get; } public string Name { get; } + private SessionWindow window; + public ClientThread(string name, int session) { Name = name; Session = session; + + window = new SessionWindow(Name, true); + window.FormClosed += Window_FormClosed; + } + + private void Window_FormClosed(object sender, FormClosedEventArgs e) + { + MainClient.RemoveActiveClient(this); + } + + public void HandleCommand(NetCommand command) + { + switch(command.Type) + { + + } + } + + public void run() + { + Application.Run(window); + } + + public void stop() + { + window.Close(); + MainClient.RemoveActiveClient(this); + } + + private void sendCommand(NetCommand command) + { + MainClient.SendNetCommand(command); } } diff --git a/ErgometerDoctorApplication/ConActiveSessions.cs b/ErgometerDoctorApplication/ConActiveSessions.cs index 90315a8..d899ee4 100644 --- a/ErgometerDoctorApplication/ConActiveSessions.cs +++ b/ErgometerDoctorApplication/ConActiveSessions.cs @@ -22,7 +22,7 @@ namespace ErgometerDoctorApplication this.Size = new System.Drawing.Size(584, 459); this.TabIndex = 0; // - // labelPassword + // labelActiveSessions // this.labelActiveSessions.Anchor = System.Windows.Forms.AnchorStyles.None; this.labelActiveSessions.AutoSize = true; @@ -32,6 +32,8 @@ namespace ErgometerDoctorApplication this.labelActiveSessions.Size = new System.Drawing.Size(103, 21); this.labelActiveSessions.TabIndex = 3; this.labelActiveSessions.Text = "Geen actieve sessies."; + + this.Controls.Add(labelActiveSessions); } public System.Windows.Forms.Label labelActiveSessions; diff --git a/ErgometerDoctorApplication/MainClient.cs b/ErgometerDoctorApplication/MainClient.cs index 45d7e5e..c6b3da7 100644 --- a/ErgometerDoctorApplication/MainClient.cs +++ b/ErgometerDoctorApplication/MainClient.cs @@ -29,6 +29,12 @@ namespace ErgometerDoctorApplication public static List clients; public static Dictionary users; public static List oldsessions; + + public static void RemoveActiveClient(ClientThread clientThread) + { + clients.Remove(clientThread); + } + public static Dictionary activesessions; static MainClient() @@ -76,6 +82,8 @@ namespace ErgometerDoctorApplication loggedin = true; } + SendNetCommand(new NetCommand(NetCommand.RequestType.SESSIONDATA, Session)); + return true; } @@ -152,13 +160,43 @@ namespace ErgometerDoctorApplication private static void HandToClient(NetCommand command) { - //throw new NotImplementedException(); - Console.WriteLine(command); + foreach(ClientThread cl in clients) + { + if(cl.Session == command.Session) + { + cl.HandleCommand(command); + } + } } public static void SendNetCommand(NetCommand command) { NetHelper.SendNetCommand(Server, command); } + + private static bool IsSessionRunning(int session) + { + foreach(ClientThread cl in clients) + { + if (cl.Session == session) + return true; + } + + return false; + } + + public static void StartNewCLient(string name, int session) + { + if (IsSessionRunning(session)) + return; + + //Start new client + ClientThread cl = new ClientThread(name, session); + clients.Add(cl); + + //Run client on new thread + Thread thread = new Thread(new ThreadStart(cl.run)); + thread.Start(); + } } } diff --git a/ErgometerDoctorApplication/MainWindow.cs b/ErgometerDoctorApplication/MainWindow.cs index 4ed267d..5d02bcc 100644 --- a/ErgometerDoctorApplication/MainWindow.cs +++ b/ErgometerDoctorApplication/MainWindow.cs @@ -21,14 +21,14 @@ namespace ErgometerDoctorApplication private void BtnActiveSessions_Click(object sender, EventArgs e) { - MainClient.SendNetCommand(new NetCommand(NetCommand.RequestType.SESSIONDATA, MainClient.Session)); this.HeaderLabel.Text = "Actieve Sessies"; conActiveSessions.BringToFront(); string str = ""; - foreach(string name in MainClient.activesessions.Values) + foreach(KeyValuePair client in MainClient.activesessions) { - str += name + ", "; + str += client.Value + ", "; + MainClient.StartNewCLient(client.Value, client.Key); } conActiveSessions.labelActiveSessions.Text = str; Console.WriteLine(str);