Client/MainForm.cs
author sl
Mon, 15 Dec 2014 18:52:42 +0100
changeset 73 926cdf23b485
parent 72 fd0bb39a7818
child 74 60d584bad780
permissions -rw-r--r--
Client now recovers from faulty states and timeout.
sl@18
     1
using System;
sl@18
     2
using System.Collections.Generic;
sl@18
     3
using System.ComponentModel;
sl@18
     4
using System.Data;
sl@18
     5
using System.Drawing;
sl@18
     6
using System.Linq;
sl@18
     7
using System.Text;
sl@18
     8
using System.Threading.Tasks;
sl@18
     9
using System.Windows.Forms;
sl@18
    10
using System.ServiceModel;
sl@18
    11
using System.ServiceModel.Channels;
sl@31
    12
using System.Diagnostics;
sl@55
    13
using SharpDisplay;
sl@20
    14
sl@18
    15
sl@18
    16
namespace SharpDisplayClient
sl@18
    17
{
sl@18
    18
    public partial class MainForm : Form
sl@18
    19
    {
sl@73
    20
        DisplayClient iClient;
sl@73
    21
sl@43
    22
        ContentAlignment Alignment;
sl@72
    23
        DataField iTextFieldTop;
sl@18
    24
sl@18
    25
        public MainForm()
sl@18
    26
        {
sl@18
    27
            InitializeComponent();
sl@43
    28
            Alignment = ContentAlignment.MiddleLeft;
sl@72
    29
            iTextFieldTop = new DataField(0);
sl@18
    30
        }
sl@18
    31
sl@18
    32
sl@18
    33
        private void MainForm_Load(object sender, EventArgs e)
sl@18
    34
        {
sl@73
    35
            iClient = new DisplayClient(this);
sl@73
    36
            iClient.Open();
sl@18
    37
sl@29
    38
            //Connect using unique name
sl@32
    39
            //string name = DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt");
sl@32
    40
            string name = "Client-" + (iClient.ClientCount() - 1);
sl@32
    41
            iClient.SetName(name);
sl@30
    42
            //Text = Text + ": " + name;
sl@32
    43
            Text = "[[" + name + "]]  " + iClient.SessionId;
sl@18
    44
sl@33
    45
            //
sl@33
    46
            textBoxTop.Text = iClient.Name;
sl@33
    47
            textBoxBottom.Text = iClient.SessionId;
sl@33
    48
sl@18
    49
        }
sl@21
    50
sl@31
    51
sl@60
    52
sl@31
    53
        public delegate void CloseConnectionDelegate();
sl@31
    54
        public delegate void CloseDelegate();
sl@31
    55
sl@31
    56
        /// <summary>
sl@60
    57
        ///
sl@31
    58
        /// </summary>
sl@31
    59
        public void CloseConnectionThreadSafe()
sl@21
    60
        {
sl@31
    61
            if (this.InvokeRequired)
sl@29
    62
            {
sl@31
    63
                //Not in the proper thread, invoke ourselves
sl@31
    64
                CloseConnectionDelegate d = new CloseConnectionDelegate(CloseConnectionThreadSafe);
sl@31
    65
                this.Invoke(d, new object[] { });
sl@29
    66
            }
sl@31
    67
            else
sl@31
    68
            {
sl@31
    69
                //We are in the proper thread
sl@31
    70
                if (IsClientReady())
sl@31
    71
                {
sl@31
    72
                    Trace.TraceInformation("Closing client: " + iClient.SessionId);
sl@31
    73
                    iClient.Close();
sl@31
    74
                    Trace.TraceInformation("Closed client: " + iClient.SessionId);
sl@31
    75
                }
sl@29
    76
sl@31
    77
                iClient = null;
sl@31
    78
            }
sl@26
    79
        }
sl@26
    80
sl@31
    81
        /// <summary>
sl@60
    82
        ///
sl@31
    83
        /// </summary>
sl@31
    84
        public void CloseThreadSafe()
sl@31
    85
        {
sl@31
    86
            if (this.InvokeRequired)
sl@31
    87
            {
sl@31
    88
                //Not in the proper thread, invoke ourselves
sl@31
    89
                CloseDelegate d = new CloseDelegate(CloseThreadSafe);
sl@31
    90
                this.Invoke(d, new object[] { });
sl@31
    91
            }
sl@31
    92
            else
sl@31
    93
            {
sl@31
    94
                //We are in the proper thread
sl@31
    95
                Close();
sl@31
    96
            }
sl@31
    97
        }
sl@31
    98
sl@31
    99
sl@26
   100
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@26
   101
        {
sl@31
   102
            CloseConnectionThreadSafe();
sl@29
   103
        }
sl@29
   104
sl@29
   105
        public bool IsClientReady()
sl@29
   106
        {
sl@73
   107
            return (iClient != null && iClient.IsReady());
sl@21
   108
        }
sl@43
   109
sl@43
   110
        private void buttonAlignLeft_Click(object sender, EventArgs e)
sl@43
   111
        {
sl@43
   112
            Alignment = ContentAlignment.MiddleLeft;
sl@43
   113
            textBoxTop.TextAlign = HorizontalAlignment.Left;
sl@43
   114
            textBoxBottom.TextAlign = HorizontalAlignment.Left;
sl@43
   115
        }
sl@43
   116
sl@43
   117
        private void buttonAlignCenter_Click(object sender, EventArgs e)
sl@43
   118
        {
sl@43
   119
            Alignment = ContentAlignment.MiddleCenter;
sl@43
   120
            textBoxTop.TextAlign = HorizontalAlignment.Center;
sl@43
   121
            textBoxBottom.TextAlign = HorizontalAlignment.Center;
sl@43
   122
        }
sl@43
   123
sl@43
   124
        private void buttonAlignRight_Click(object sender, EventArgs e)
sl@43
   125
        {
sl@43
   126
            Alignment = ContentAlignment.MiddleRight;
sl@43
   127
            textBoxTop.TextAlign = HorizontalAlignment.Right;
sl@43
   128
            textBoxBottom.TextAlign = HorizontalAlignment.Right;
sl@43
   129
        }
sl@43
   130
sl@43
   131
        private void buttonSetTopText_Click(object sender, EventArgs e)
sl@43
   132
        {
sl@43
   133
            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
sl@43
   134
            iTextFieldTop.Text = textBoxTop.Text;
sl@60
   135
            iTextFieldTop.Alignment = Alignment;
sl@43
   136
            iClient.SetText(iTextFieldTop);
sl@43
   137
        }
sl@43
   138
sl@43
   139
        private void buttonSetText_Click(object sender, EventArgs e)
sl@43
   140
        {
sl@43
   141
            //iClient.SetText(0,"Top");
sl@43
   142
            //iClient.SetText(1, "Bottom");
sl@43
   143
            //TextField top = new TextField(0, textBoxTop.Text, ContentAlignment.MiddleLeft);
sl@43
   144
sl@72
   145
            iClient.SetTexts(new DataField[]
sl@60
   146
            {
sl@72
   147
                new DataField(0, textBoxTop.Text, Alignment),
sl@72
   148
                new DataField(1, textBoxBottom.Text, Alignment)
sl@43
   149
            });
sl@43
   150
        }
sl@62
   151
sl@62
   152
        private void buttonLayoutUpdate_Click(object sender, EventArgs e)
sl@62
   153
        {
sl@65
   154
            //Define a 2 by 2 layout
sl@62
   155
            TableLayout layout = new TableLayout(2,2);
sl@65
   156
            //Second column only takes up 25%
sl@63
   157
            layout.Columns[1].Width = 25F;
sl@65
   158
            //Send layout to server
sl@62
   159
            iClient.SetLayout(layout);
sl@62
   160
        }
sl@67
   161
sl@68
   162
        private void buttonSetBitmap_Click(object sender, EventArgs e)
sl@67
   163
        {
sl@68
   164
            int x1 = 0;
sl@68
   165
            int y1 = 0;
sl@68
   166
            int x2 = 256;
sl@68
   167
            int y2 = 32;
sl@68
   168
sl@68
   169
            Bitmap bitmap = new Bitmap(x2,y2);
sl@67
   170
            Pen blackPen = new Pen(Color.Black, 3);
sl@67
   171
sl@67
   172
            // Draw line to screen.
sl@67
   173
            using (var graphics = Graphics.FromImage(bitmap))
sl@67
   174
            {
sl@67
   175
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@68
   176
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@67
   177
            }
sl@67
   178
sl@72
   179
            DataField field = new DataField(0, bitmap);
sl@70
   180
            field.ColumnSpan = 2;
sl@70
   181
            iClient.SetBitmap(field);
sl@70
   182
        }
sl@70
   183
sl@71
   184
        private void buttonBitmapLayout_Click(object sender, EventArgs e)
sl@71
   185
        {
sl@71
   186
            SetLayoutWithBitmap();
sl@71
   187
        }
sl@71
   188
sl@71
   189
        /// <summary>
sl@71
   190
        /// Define a layout with a bitmap field on the left and two lines of text on the right.
sl@71
   191
        /// </summary>
sl@71
   192
        private void SetLayoutWithBitmap()
sl@70
   193
        {
sl@70
   194
            //Define a 2 by 2 layout
sl@70
   195
            TableLayout layout = new TableLayout(2, 2);
sl@71
   196
            //First column only takes 25%
sl@70
   197
            layout.Columns[0].Width = 25F;
sl@73
   198
            //Second column takes up 75%
sl@70
   199
            layout.Columns[1].Width = 75F;
sl@70
   200
            //Send layout to server
sl@70
   201
            iClient.SetLayout(layout);
sl@70
   202
sl@70
   203
            //Set a bitmap for our first field
sl@70
   204
            int x1 = 0;
sl@70
   205
            int y1 = 0;
sl@70
   206
            int x2 = 64;
sl@70
   207
            int y2 = 64;
sl@70
   208
sl@70
   209
            Bitmap bitmap = new Bitmap(x2, y2);
sl@70
   210
            Pen blackPen = new Pen(Color.Black, 3);
sl@70
   211
sl@70
   212
            // Draw line to screen.
sl@70
   213
            using (var graphics = Graphics.FromImage(bitmap))
sl@70
   214
            {
sl@70
   215
                graphics.DrawLine(blackPen, x1, y1, x2, y2);
sl@70
   216
                graphics.DrawLine(blackPen, x1, y2, x2, y1);
sl@70
   217
            }
sl@70
   218
sl@71
   219
            //Create a bitmap field from the bitmap we just created
sl@72
   220
            DataField field = new DataField(0, bitmap);
sl@71
   221
            //We want our bitmap field to span across two rows
sl@70
   222
            field.RowSpan = 2;
sl@71
   223
            //Send it to our server
sl@70
   224
            iClient.SetBitmap(field);
sl@70
   225
sl@70
   226
            //Set texts
sl@72
   227
            iClient.SetTexts(new DataField[]
sl@70
   228
            {
sl@72
   229
                new DataField(1, textBoxTop.Text, Alignment),
sl@72
   230
                new DataField(2, textBoxBottom.Text, Alignment)
sl@70
   231
            });
sl@70
   232
sl@67
   233
        }
sl@18
   234
    }
sl@18
   235
}