# HG changeset patch # User sl # Date 1419713534 -3600 # Node ID 150c4a9d7e265e906c00f557a9dc72ab9c7348ea # Parent 42d62953c9371017ccf7d953b6faa6d82b412fe1# Parent 9bcab0dfa376ed4c1ff058cc5f12920cb8e4a029 Merge diff -r 42d62953c937 -r 150c4a9d7e26 Client/Client.cs --- a/Client/Client.cs Sat Dec 27 21:50:15 2014 +0100 +++ b/Client/Client.cs Sat Dec 27 21:52:14 2014 +0100 @@ -96,14 +96,16 @@ /// - /// + /// Handle connection with our Sharp Display Server. + /// If the connection is faulted it will attempt to restart it. /// public class DisplayClient { - Client iClient; - Callback iCallback; + private Client iClient; + private Callback iCallback; + private bool resetingConnection = false; + private MainForm MainForm { get; set; } - public string SessionId { get { return iClient.SessionId; } } public string Name { get; private set; } private TableLayout Layout { get; set; } @@ -117,12 +119,18 @@ Fields = new DataField[]{}; } + /// + /// Initialize our server connection. + /// public void Open() { iCallback = new Callback(MainForm); iClient = new Client(iCallback); } + /// + /// Terminate our server connection. + /// public void Close() { iClient.Close(); @@ -131,26 +139,45 @@ iCallback = null; } + /// + /// Tells whether a server connection is available. + /// + /// True if a server connection is available. False otherwise. public bool IsReady() { return (iClient != null && iCallback != null && iClient.IsReady()); } + /// + /// Check if our server connection is available and attempt reset it if it isn't. + /// This is notably dealing with timed out connections. + /// public void CheckConnection() { - if (!IsReady()) + if (!IsReady() && !resetingConnection) { //Try to reconnect Open(); - //On reconnect there is a bunch of properties we need to set - if (Name != "") + //Avoid stack overflow in case of persisting failure + resetingConnection = true; + + try { - iClient.SetName(Name); + //On reconnect there is a bunch of properties we need to reset + if (Name != "") + { + iClient.SetName(Name); + } + + SetLayout(Layout); + iClient.SetFields(Fields); } - - SetLayout(Layout); - SetFields(Fields); + finally + { + //Make sure our this state does not get out of sync + resetingConnection = true; + } } } @@ -169,34 +196,85 @@ iClient.SetLayout(aLayout); } - - public void SetField(DataField aField) + /// + /// Set the specified field. + /// + /// + /// True if the specified field was set client side. False means you need to redefine all your fields using CreateFields. + public bool SetField(DataField aField) { - //TODO: Create fields if not present int i = 0; + bool fieldFound = false; foreach (DataField field in Fields) { if (field.Index == aField.Index) { //Update our field then Fields[i] = aField; + fieldFound = true; break; } i++; } + if (!fieldFound) + { + //Field not found, make to use SetFields with all your fields at least once after setting your layout. + return false; + } + CheckConnection(); iClient.SetField(aField); + return true; } - public void SetFields(System.Collections.Generic.IList aFields) + /// + /// Use this function when updating existing fields. + /// + /// + public bool SetFields(System.Collections.Generic.IList aFields) + { + int fieldFoundCount = 0; + foreach (DataField fieldUpdate in aFields) + { + int i = 0; + foreach (DataField existingField in Fields) + { + if (existingField.Index == fieldUpdate.Index) + { + //Update our field then + Fields[i] = fieldUpdate; + fieldFoundCount++; + //Move on to the next field + break; + } + i++; + } + } + + // + if (fieldFoundCount!=aFields.Count) + { + //Field not found, make sure to use SetFields with all your fields at least once after setting your layout. + return false; + } + + CheckConnection(); + iClient.SetFields(aFields); + return true; + } + + /// + /// Use this function when creating your fields. + /// + /// + public void CreateFields(System.Collections.Generic.IList aFields) { Fields = aFields; CheckConnection(); iClient.SetFields(aFields); } - public int ClientCount() { CheckConnection(); diff -r 42d62953c937 -r 150c4a9d7e26 Client/MainForm.Designer.cs --- a/Client/MainForm.Designer.cs Sat Dec 27 21:50:15 2014 +0100 +++ b/Client/MainForm.Designer.cs Sat Dec 27 21:52:14 2014 +0100 @@ -38,6 +38,8 @@ this.buttonLayoutUpdate = new System.Windows.Forms.Button(); this.buttonSetBitmap = new System.Windows.Forms.Button(); this.buttonBitmapLayout = new System.Windows.Forms.Button(); + this.buttonIndicatorsLayout = new System.Windows.Forms.Button(); + this.buttonUpdateTexts = new System.Windows.Forms.Button(); this.SuspendLayout(); // // buttonSetText @@ -134,11 +136,33 @@ this.buttonBitmapLayout.UseVisualStyleBackColor = true; this.buttonBitmapLayout.Click += new System.EventHandler(this.buttonBitmapLayout_Click); // + // buttonIndicatorsLayout + // + this.buttonIndicatorsLayout.Location = new System.Drawing.Point(94, 189); + this.buttonIndicatorsLayout.Name = "buttonIndicatorsLayout"; + this.buttonIndicatorsLayout.Size = new System.Drawing.Size(75, 35); + this.buttonIndicatorsLayout.TabIndex = 28; + this.buttonIndicatorsLayout.Text = "Indicators Layout "; + this.buttonIndicatorsLayout.UseVisualStyleBackColor = true; + this.buttonIndicatorsLayout.Click += new System.EventHandler(this.buttonIndicatorsLayout_Click); + // + // buttonUpdateTexts + // + this.buttonUpdateTexts.Location = new System.Drawing.Point(257, 189); + this.buttonUpdateTexts.Name = "buttonUpdateTexts"; + this.buttonUpdateTexts.Size = new System.Drawing.Size(75, 35); + this.buttonUpdateTexts.TabIndex = 29; + this.buttonUpdateTexts.Text = "Update Texts"; + this.buttonUpdateTexts.UseVisualStyleBackColor = true; + this.buttonUpdateTexts.Click += new System.EventHandler(this.buttonUpdateTexts_Click); + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(443, 252); + this.Controls.Add(this.buttonUpdateTexts); + this.Controls.Add(this.buttonIndicatorsLayout); this.Controls.Add(this.buttonBitmapLayout); this.Controls.Add(this.buttonSetBitmap); this.Controls.Add(this.buttonLayoutUpdate); @@ -170,6 +194,8 @@ private System.Windows.Forms.Button buttonLayoutUpdate; private System.Windows.Forms.Button buttonSetBitmap; private System.Windows.Forms.Button buttonBitmapLayout; + private System.Windows.Forms.Button buttonIndicatorsLayout; + private System.Windows.Forms.Button buttonUpdateTexts; } } diff -r 42d62953c937 -r 150c4a9d7e26 Client/MainForm.cs --- a/Client/MainForm.cs Sat Dec 27 21:50:15 2014 +0100 +++ b/Client/MainForm.cs Sat Dec 27 21:52:14 2014 +0100 @@ -134,16 +134,24 @@ //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft); iTextFieldTop.Text = textBoxTop.Text; iTextFieldTop.Alignment = Alignment; - iClient.SetField(iTextFieldTop); + bool res = iClient.SetField(iTextFieldTop); + + if (!res) + { + MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + } private void buttonSetText_Click(object sender, EventArgs e) { - //iClient.SetText(0,"Top"); - //iClient.SetText(1, "Bottom"); - //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft); + //Set one column two lines layout + TableLayout layout = new TableLayout(1, 2); + iClient.SetLayout(layout); - iClient.SetFields(new DataField[] + //Set our fields + iClient.CreateFields(new DataField[] { new DataField(0, textBoxTop.Text, Alignment), new DataField(1, textBoxBottom.Text, Alignment) @@ -178,7 +186,7 @@ } DataField field = new DataField(0, bitmap); - field.ColumnSpan = 2; + //field.ColumnSpan = 2; iClient.SetField(field); } @@ -221,10 +229,9 @@ DataField field = new DataField(0, bitmap); //We want our bitmap field to span across two rows field.RowSpan = 2; - iClient.SetField(field); //Set texts - iClient.SetFields(new DataField[] + iClient.CreateFields(new DataField[] { field, new DataField(1, textBoxTop.Text, Alignment), @@ -232,5 +239,78 @@ }); } + + private void buttonIndicatorsLayout_Click(object sender, EventArgs e) + { + //Define a 2 by 4 layout + TableLayout layout = new TableLayout(2, 4); + //First column + layout.Columns[0].Width = 87.5F; + //Second column + layout.Columns[1].Width = 12.5F; + //Send layout to server + iClient.SetLayout(layout); + + //Create a bitmap for our indicators field + int x1 = 0; + int y1 = 0; + int x2 = 32; + int y2 = 16; + + Bitmap bitmap = new Bitmap(x2, y2); + Pen blackPen = new Pen(Color.Black, 3); + + // Draw line to screen. + using (var graphics = Graphics.FromImage(bitmap)) + { + graphics.DrawLine(blackPen, x1, y1, x2, y2); + graphics.DrawLine(blackPen, x1, y2, x2, y1); + } + + //Create a bitmap field from the bitmap we just created + DataField indicator1 = new DataField(2, bitmap); + //Create a bitmap field from the bitmap we just created + DataField indicator2 = new DataField(3, bitmap); + //Create a bitmap field from the bitmap we just created + DataField indicator3 = new DataField(4, bitmap); + //Create a bitmap field from the bitmap we just created + DataField indicator4 = new DataField(5, bitmap); + + // + DataField textFieldTop = new DataField(0, textBoxTop.Text, Alignment); + textFieldTop.RowSpan = 2; + + DataField textFieldBottom = new DataField(1, textBoxBottom.Text, Alignment); + textFieldBottom.RowSpan = 2; + + + //Set texts + iClient.CreateFields(new DataField[] + { + textFieldTop, + indicator1, + indicator2, + textFieldBottom, + indicator3, + indicator4 + }); + + } + + private void buttonUpdateTexts_Click(object sender, EventArgs e) + { + + bool res = iClient.SetFields(new DataField[] + { + new DataField(0, textBoxTop.Text, Alignment), + new DataField(1, textBoxBottom.Text, Alignment) + }); + + if (!res) + { + MessageBox.Show("Create you fields first", "Field update error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + } } } diff -r 42d62953c937 -r 150c4a9d7e26 Server/MainForm.cs --- a/Server/MainForm.cs Sat Dec 27 21:50:15 2014 +0100 +++ b/Server/MainForm.cs Sat Dec 27 21:52:14 2014 +0100 @@ -25,9 +25,9 @@ //Delegates are used for our thread safe method public delegate void AddClientDelegate(string aSessionId, ICallback aCallback); public delegate void RemoveClientDelegate(string aSessionId); - public delegate void SetTextDelegate(string SessionId, DataField aField); + public delegate void SetFieldDelegate(string SessionId, DataField aField); + public delegate void SetFieldsDelegate(string SessionId, System.Collections.Generic.IList aFields); public delegate void SetLayoutDelegate(string SessionId, TableLayout aLayout); - public delegate void SetFieldsDelegate(string SessionId, System.Collections.Generic.IList aFields); public delegate void SetClientNameDelegate(string aSessionId, string aName); @@ -807,7 +807,7 @@ if (this.InvokeRequired) { //Not in the proper thread, invoke ourselves - SetTextDelegate d = new SetTextDelegate(SetClientFieldThreadSafe); + SetFieldDelegate d = new SetFieldDelegate(SetClientFieldThreadSafe); this.Invoke(d, new object[] { aSessionId, aField }); } else @@ -819,12 +819,12 @@ } /// - /// + /// /// /// /// private void SetClientField(string aSessionId, DataField aField) - { + { SetCurrentClient(aSessionId); ClientData client = iClients[aSessionId]; if (client != null) @@ -845,7 +845,7 @@ client.Fields[aField.Index] = aField; // if (aField.IsText && tableLayoutPanel.Controls[aField.Index] is MarqueeLabel) - { + { //Text field control already in place, just change the text MarqueeLabel label = (MarqueeLabel)tableLayoutPanel.Controls[aField.Index]; somethingChanged = (label.Text != aField.Text || label.TextAlign != aField.Alignment);