Prevents switching clients too often.
authorStephaneLenclud
Fri, 19 Jun 2015 17:12:06 +0200
changeset 1416f1da2b5c2ec
parent 140 4dff57d255c9
child 142 45afadb954ba
Prevents switching clients too often.
Server/ClientData.cs
Server/MainForm.cs
     1.1 --- a/Server/ClientData.cs	Thu Jun 11 17:21:15 2015 +0200
     1.2 +++ b/Server/ClientData.cs	Fri Jun 19 17:12:06 2015 +0200
     1.3 @@ -28,5 +28,8 @@
     1.4          public List<DataField> Fields { get; set; }
     1.5          public TableLayout Layout { get; set; }
     1.6          public ICallback Callback { get; set; }
     1.7 +
     1.8 +        //Client management
     1.9 +        public DateTime LastSwitchTime { get; set; }
    1.10      }
    1.11  }
     2.1 --- a/Server/MainForm.cs	Thu Jun 11 17:21:15 2015 +0200
     2.2 +++ b/Server/MainForm.cs	Fri Jun 19 17:12:06 2015 +0200
     2.3 @@ -555,7 +555,7 @@
     2.4          /// This will take care of applying our client layout and set data fields.
     2.5          /// </summary>
     2.6          /// <param name="aSessionId"></param>
     2.7 -        void SetCurrentClient(string aSessionId)
     2.8 +        void SetCurrentClient(string aSessionId, bool aForce=false)
     2.9          {
    2.10              if (aSessionId == iCurrentClientSessionId)
    2.11              {
    2.12 @@ -564,8 +564,20 @@
    2.13                  return;
    2.14              }
    2.15  
    2.16 +
    2.17 +            //Check when was the last time we switched to that client
    2.18 +            double lastSwitchToClientSecondsAgo = (DateTime.Now - iClients[aSessionId].LastSwitchTime).TotalSeconds;
    2.19 +            //TODO: put that hard coded delay in settings
    2.20 +            if (!aForce && (lastSwitchToClientSecondsAgo < 10))
    2.21 +            {
    2.22 +                //Don't switch clients too often
    2.23 +                return;
    2.24 +            }
    2.25 +
    2.26              //Set current client ID.
    2.27              iCurrentClientSessionId = aSessionId;
    2.28 +            //Set the time we last switched to that client
    2.29 +            iClients[aSessionId].LastSwitchTime = DateTime.Now;
    2.30              //Fetch and set current client data.
    2.31              iCurrentClientData = iClients[aSessionId];
    2.32              //Apply layout and set data fields.
    2.33 @@ -1320,7 +1332,20 @@
    2.34  
    2.35          private void treeViewClients_AfterSelect(object sender, TreeViewEventArgs e)
    2.36          {
    2.37 +            //Root node must have at least one child
    2.38 +            if (e.Node.Nodes.Count == 0)
    2.39 +            {
    2.40 +                return;
    2.41 +            }
    2.42  
    2.43 +            //If the selected node is the root node of a client then switch to it
    2.44 +            string sessionId=e.Node.Nodes[0].Text; //First child of a root node is the sessionId
    2.45 +            if (iClients.ContainsKey(sessionId)) //Check that's actually what we are looking at
    2.46 +            {
    2.47 +                //We have a valid session just switch to that client
    2.48 +                SetCurrentClient(sessionId,true);
    2.49 +            }
    2.50 +            
    2.51          }
    2.52  
    2.53  
    2.54 @@ -1405,8 +1430,8 @@
    2.55                  ClientData client = iClients[aSessionId];
    2.56                  if (client != null)
    2.57                  {
    2.58 +                    //Set our client layout then
    2.59                      client.Layout = aLayout;
    2.60 -                    UpdateTableLayoutPanel(client);
    2.61                      //
    2.62                      UpdateClientTreeViewNode(client);
    2.63                  }
    2.64 @@ -1440,63 +1465,69 @@
    2.65          /// <param name="aSessionId"></param>
    2.66          /// <param name="aField"></param>
    2.67          private void SetClientField(string aSessionId, DataField aField)
    2.68 -        {
    2.69 -            SetCurrentClient(aSessionId);
    2.70 +        {   
    2.71 +            //TODO: should check if the field actually changed?
    2.72 +
    2.73              ClientData client = iClients[aSessionId];
    2.74 -            if (client != null)
    2.75 +            bool layoutChanged = false;
    2.76 +
    2.77 +            //Make sure all our fields are in place
    2.78 +            while (client.Fields.Count < (aField.Index + 1))
    2.79              {
    2.80 -                bool somethingChanged = false;
    2.81 +                //Add a text field with proper index
    2.82 +                client.Fields.Add(new DataField(client.Fields.Count));
    2.83 +                layoutChanged = true;
    2.84 +            }
    2.85  
    2.86 -                //Make sure all our fields are in place
    2.87 -                while (client.Fields.Count < (aField.Index + 1))
    2.88 +            if (client.Fields[aField.Index].IsSameLayout(aField))
    2.89 +            {
    2.90 +                //Same layout just update our field
    2.91 +                client.Fields[aField.Index] = aField;
    2.92 +                //If we are updating a field in our current client we need to update it in our panel
    2.93 +                if (aSessionId == iCurrentClientSessionId)
    2.94                  {
    2.95 -                    //Add a text field with proper index
    2.96 -                    client.Fields.Add(new DataField(client.Fields.Count));
    2.97 -                    somethingChanged = true;
    2.98 -                }
    2.99 -
   2.100 -                if (client.Fields[aField.Index].IsSameLayout(aField))
   2.101 -                {
   2.102 -                    //Same layout just update our field
   2.103 -                    client.Fields[aField.Index] = aField;
   2.104 -                    //
   2.105 -                    if (aField.IsText && tableLayoutPanel.Controls[aField.Index] is MarqueeLabel)
   2.106 +                    if (aField.IsText && aField.Index < tableLayoutPanel.Controls.Count && tableLayoutPanel.Controls[aField.Index] is MarqueeLabel)
   2.107                      {
   2.108                          //Text field control already in place, just change the text
   2.109                          MarqueeLabel label = (MarqueeLabel)tableLayoutPanel.Controls[aField.Index];
   2.110 -                        somethingChanged = (label.Text != aField.Text || label.TextAlign != aField.Alignment);
   2.111 +                        layoutChanged = (label.Text != aField.Text || label.TextAlign != aField.Alignment);
   2.112                          label.Text = aField.Text;
   2.113                          label.TextAlign = aField.Alignment;
   2.114                      }
   2.115 -                    else if (aField.IsBitmap && tableLayoutPanel.Controls[aField.Index] is PictureBox)
   2.116 +                    else if (aField.IsBitmap && aField.Index < tableLayoutPanel.Controls.Count && tableLayoutPanel.Controls[aField.Index] is PictureBox)
   2.117                      {
   2.118 -                        somethingChanged = true; //TODO: Bitmap comp or should we leave that to clients?
   2.119 +                        layoutChanged = true; //TODO: Bitmap comp or should we leave that to clients?
   2.120                          //Bitmap field control already in place just change the bitmap
   2.121                          PictureBox pictureBox = (PictureBox)tableLayoutPanel.Controls[aField.Index];
   2.122                          pictureBox.Image = aField.Bitmap;
   2.123                      }
   2.124                      else
   2.125                      {
   2.126 -                        somethingChanged = true;
   2.127 -                        //The requested control in our layout it not of the correct type
   2.128 -                        //Wrong control type, re-create them all
   2.129 -                        UpdateTableLayoutPanel(iCurrentClientData);
   2.130 +                        layoutChanged = true;
   2.131                      }
   2.132                  }
   2.133 -                else
   2.134 +            }
   2.135 +            else
   2.136 +            {
   2.137 +                layoutChanged = true;
   2.138 +                //Different layout, need to rebuild it
   2.139 +                client.Fields[aField.Index] = aField;
   2.140 +            }
   2.141 +
   2.142 +            //
   2.143 +            if (layoutChanged)
   2.144 +            {
   2.145 +                UpdateClientTreeViewNode(client);
   2.146 +                //Our layout has changed, if we are already the current client we need to update our panel
   2.147 +                if (aSessionId == iCurrentClientSessionId)
   2.148                  {
   2.149 -                    somethingChanged = true;
   2.150 -                    //Different layout, need to rebuild it
   2.151 -                    client.Fields[aField.Index] = aField;
   2.152 +                    //Apply layout and set data fields.
   2.153                      UpdateTableLayoutPanel(iCurrentClientData);
   2.154                  }
   2.155 +            }
   2.156  
   2.157 -                //
   2.158 -                if (somethingChanged)
   2.159 -                {
   2.160 -                    UpdateClientTreeViewNode(client);
   2.161 -                }
   2.162 -            }
   2.163 +            //When a client field is set we try switching to this client to present the new information to our user
   2.164 +            SetCurrentClient(aSessionId);
   2.165          }
   2.166  
   2.167          /// <summary>
   2.168 @@ -1646,6 +1677,7 @@
   2.169              TableLayout layout = aClient.Layout;
   2.170              int fieldCount = 0;
   2.171  
   2.172 +            //First clean our current panel
   2.173              tableLayoutPanel.Controls.Clear();
   2.174              tableLayoutPanel.RowStyles.Clear();
   2.175              tableLayoutPanel.ColumnStyles.Clear();