Browse Source

Added backlog handling

Kenneth van Ewijk 10 years ago
parent
commit
43dbc4ad13

+ 34 - 0
ErgometerIPR/ErgometerDoctorApplication/MainClient.cs

@@ -24,6 +24,8 @@ namespace ErgometerDoctorApplication
         public static string HOST = "127.0.0.1";
         public static int PORT = 8888;
 
+        public static List<NetCommand> backlog;
+
 
         //Server information
         public static List<ClientThread> clients;
@@ -47,6 +49,7 @@ namespace ErgometerDoctorApplication
             users = new Dictionary<string, string>();
             activesessions = new Dictionary<int, string>();
             oldSessionsData = new List<Tuple<string, double, int>>();
+            backlog = new List<NetCommand>();
         }
 
         public static bool Connect(string password, out string error)
@@ -212,13 +215,20 @@ namespace ErgometerDoctorApplication
 
         private static void HandToClient(NetCommand command)
         {
+            bool handled = false;
             foreach (ClientThread cl in clients)
             {
                 if (cl.Session == command.Session)
                 {
                     cl.HandleCommand(command);
+                    handled = true;
                 }
             }
+
+            if(!handled)
+            {
+                backlog.Add(command);
+            }
         }
 
         public static void SendNetCommand(NetCommand command)
@@ -240,6 +250,22 @@ namespace ErgometerDoctorApplication
             return false;
         }
 
+        private static List<NetCommand> GetBacklogForSession(int session)
+        {
+            List<NetCommand> temp = new List<NetCommand>();
+
+            for(int i=backlog.Count-1; i>= 0; i--)
+            {
+                if (backlog[i].Session == session)
+                {
+                    temp.Add(backlog[i]);
+                    backlog.RemoveAt(i);
+                }
+            }
+
+            return temp;
+        }
+
         public static void StartNewClient(string name, int session)
         {
             if (IsSessionRunning(session))
@@ -254,6 +280,14 @@ namespace ErgometerDoctorApplication
             thread.IsBackground = true;
             thread.Start();
 
+            Thread.Sleep(5);
+
+            List<NetCommand> sessionbacklog = GetBacklogForSession(session);
+            for (int i = sessionbacklog.Count - 1; i >= 0; i--)
+            {
+                cl.HandleCommand(sessionbacklog[i]);
+            }
+
             Thread.Sleep(5);
             SendNetCommand(new NetCommand(NetCommand.RequestType.PERSONALDATA, session));
         }

+ 1 - 0
ErgometerIPR/ErgometerServer/DoctorThread.cs

@@ -35,6 +35,7 @@ namespace ErgometerServer
                     case NetCommand.CommandType.LOGOUT:
                         running = false;
                         client.Close();
+                        server.backlog.Clear();
                         Console.WriteLine("Doctor logged out");
                         break;
                     case NetCommand.CommandType.CHAT:

+ 19 - 1
ErgometerIPR/ErgometerServer/Server.cs

@@ -20,6 +20,7 @@ namespace ErgometerServer
         public static List<ClientThread> clients = new List<ClientThread>();
         private DoctorThread doctor;
         public Dictionary<string, string> users;
+        public List<NetCommand> backlog = new List<NetCommand>();
 
         public Server()
         {
@@ -62,13 +63,30 @@ namespace ErgometerServer
             Thread thread = new Thread(new ThreadStart(doctor.run));
             thread.IsBackground = true;
             thread.Start();
+
+            foreach(NetCommand command in backlog)
+            {
+                SendToDoctor(command);
+                Thread.Sleep(5);
+            }
+        }
+
+        public void RemoveBacklogDisconnectedClient(int session)
+        {
+            for(int i=backlog.Count - 1; i>= 0; i--)
+            {
+                if (backlog[i].Session == session)
+                    backlog.RemoveAt(i);
+            }
         }
 
         public void SendToDoctor(NetCommand command)
         {
             if (doctor != null)
-            {
                 doctor.sendToDoctor(command);
+            else
+            {
+                backlog.Add(command);
             }
         }