Client/Client.cs
author sl
Mon, 15 Dec 2014 19:44:41 +0100
changeset 74 60d584bad780
parent 73 926cdf23b485
child 77 042c1fa136b9
permissions -rw-r--r--
Removing some Text and Bitmap specific stuff and using generic Field instead.
sl@20
     1
using System;
sl@20
     2
using System.Collections.Generic;
sl@20
     3
using System.Linq;
sl@20
     4
using System.Text;
sl@20
     5
using System.Threading.Tasks;
sl@20
     6
using System.Windows.Forms;
sl@55
     7
using SharpDisplay;
sl@20
     8
using System.ServiceModel;
sl@20
     9
using System.ServiceModel.Channels;
sl@20
    10
sl@20
    11
sl@20
    12
namespace SharpDisplayClient
sl@20
    13
{
sl@25
    14
    /// <summary>
sl@25
    15
    ///
sl@25
    16
    /// </summary>
sl@31
    17
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
sl@55
    18
    public class Callback : ICallback, IDisposable
sl@20
    19
    {
sl@31
    20
        private MainForm MainForm { get; set; }
sl@31
    21
sl@31
    22
        public Callback(MainForm aMainForm)
sl@31
    23
        {
sl@31
    24
            MainForm = aMainForm;
sl@31
    25
        }
sl@31
    26
sl@20
    27
        public void OnConnected()
sl@20
    28
        {
sl@20
    29
            //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
sl@20
    30
            //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
sl@20
    31
sl@20
    32
            MessageBox.Show("OnConnected()", "Client");
sl@20
    33
        }
sl@20
    34
sl@20
    35
sl@32
    36
        public void OnCloseOrder()
sl@20
    37
        {
sl@20
    38
            //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
sl@20
    39
            //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
sl@20
    40
sl@21
    41
            //MessageBox.Show("OnServerClosing()", "Client");
sl@31
    42
            MainForm.CloseConnectionThreadSafe();
sl@31
    43
            MainForm.CloseThreadSafe();
sl@20
    44
        }
sl@25
    45
sl@25
    46
        //From IDisposable
sl@25
    47
        public void Dispose()
sl@25
    48
        {
sl@25
    49
sl@25
    50
        }
sl@20
    51
    }
sl@20
    52
sl@20
    53
sl@25
    54
    /// <summary>
sl@25
    55
    ///
sl@25
    56
    /// </summary>
sl@31
    57
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
sl@55
    58
    public class Client : DuplexClientBase<IService>
sl@20
    59
    {
sl@30
    60
        public string SessionId { get { return InnerChannel.SessionId; } }
sl@26
    61
sl@57
    62
        public Client(ICallback aCallback)
sl@57
    63
            : base(new InstanceContext(aCallback), new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
sl@20
    64
        { }
sl@20
    65
sl@32
    66
        public void SetName(string aClientName)
sl@20
    67
        {
sl@32
    68
            Channel.SetName(aClientName);
sl@26
    69
        }
sl@26
    70
sl@62
    71
        public void SetLayout(TableLayout aLayout)
sl@62
    72
        {
sl@62
    73
            Channel.SetLayout(aLayout);
sl@62
    74
        }
sl@62
    75
sl@74
    76
        public void SetField(DataField aField)
sl@20
    77
        {
sl@74
    78
            Channel.SetField(aField);
sl@20
    79
        }
sl@20
    80
sl@74
    81
        public void SetFields(System.Collections.Generic.IList<DataField> aFields)
sl@20
    82
        {
sl@74
    83
            Channel.SetFields(aFields);
sl@67
    84
        }
sl@67
    85
sl@32
    86
        public int ClientCount()
sl@32
    87
        {
sl@32
    88
            return Channel.ClientCount();
sl@32
    89
        }
sl@57
    90
sl@57
    91
        public bool IsReady()
sl@57
    92
        {
sl@74
    93
            return State == CommunicationState.Opened || State == CommunicationState.Created;
sl@57
    94
        }
sl@20
    95
    }
sl@73
    96
sl@73
    97
sl@73
    98
    /// <summary>
sl@73
    99
    ///
sl@73
   100
    /// </summary>
sl@73
   101
    public class DisplayClient
sl@73
   102
    {
sl@73
   103
        Client iClient;
sl@73
   104
        Callback iCallback;
sl@73
   105
        private MainForm MainForm { get; set; }
sl@73
   106
sl@73
   107
        public string SessionId { get { return iClient.SessionId; } }
sl@74
   108
        public string Name { get; private set; }
sl@74
   109
        private TableLayout Layout { get; set; }
sl@74
   110
        private System.Collections.Generic.IList<DataField> Fields { get; set; }
sl@74
   111
sl@73
   112
sl@73
   113
        public DisplayClient(MainForm aMainForm)
sl@73
   114
        {
sl@73
   115
            MainForm = aMainForm;
sl@73
   116
            Name = "";
sl@74
   117
            Fields = new DataField[]{};
sl@73
   118
        }
sl@73
   119
sl@73
   120
        public void Open()
sl@73
   121
        {
sl@73
   122
            iCallback = new Callback(MainForm);
sl@73
   123
            iClient = new Client(iCallback);
sl@73
   124
        }
sl@73
   125
sl@73
   126
        public void Close()
sl@73
   127
        {
sl@73
   128
            iClient.Close();
sl@73
   129
            iClient = null;
sl@73
   130
            iCallback.Dispose();
sl@73
   131
            iCallback = null;
sl@73
   132
        }
sl@73
   133
sl@73
   134
        public bool IsReady()
sl@73
   135
        {
sl@73
   136
            return (iClient != null && iCallback != null && iClient.IsReady());
sl@73
   137
        }
sl@73
   138
sl@73
   139
        public void CheckConnection()
sl@73
   140
        {
sl@73
   141
            if (!IsReady())
sl@73
   142
            {
sl@74
   143
                //Try to reconnect
sl@73
   144
                Open();
sl@74
   145
sl@74
   146
                //On reconnect there is a bunch of properties we need to set
sl@74
   147
                if (Name != "")
sl@74
   148
                {
sl@74
   149
                    iClient.SetName(Name);
sl@74
   150
                }
sl@74
   151
sl@74
   152
                SetLayout(Layout);
sl@74
   153
                SetFields(Fields);
sl@73
   154
            }
sl@73
   155
        }
sl@73
   156
sl@73
   157
        public void SetName(string aClientName)
sl@73
   158
        {
sl@73
   159
            Name = aClientName;
sl@73
   160
            CheckConnection();
sl@73
   161
            iClient.SetName(aClientName);
sl@73
   162
        }
sl@73
   163
sl@73
   164
sl@73
   165
        public void SetLayout(TableLayout aLayout)
sl@73
   166
        {
sl@74
   167
            Layout = aLayout;
sl@73
   168
            CheckConnection();
sl@73
   169
            iClient.SetLayout(aLayout);
sl@73
   170
        }
sl@73
   171
sl@74
   172
sl@74
   173
        public void SetField(DataField aField)
sl@73
   174
        {
sl@74
   175
            //TODO: Create fields if not present
sl@74
   176
            int i = 0;
sl@74
   177
            foreach (DataField field in Fields)
sl@74
   178
            {
sl@74
   179
                if (field.Index == aField.Index)
sl@74
   180
                {
sl@74
   181
                    //Update our field then
sl@74
   182
                    Fields[i] = aField;
sl@74
   183
                    break;
sl@74
   184
                }
sl@74
   185
                i++;
sl@74
   186
            }
sl@74
   187
sl@73
   188
            CheckConnection();
sl@74
   189
            iClient.SetField(aField);
sl@73
   190
        }
sl@73
   191
sl@74
   192
        public void SetFields(System.Collections.Generic.IList<DataField> aFields)
sl@73
   193
        {
sl@74
   194
            Fields = aFields;
sl@73
   195
            CheckConnection();
sl@74
   196
            iClient.SetFields(aFields);
sl@73
   197
        }
sl@73
   198
sl@73
   199
sl@73
   200
        public int ClientCount()
sl@73
   201
        {
sl@73
   202
            CheckConnection();
sl@73
   203
            return iClient.ClientCount();
sl@73
   204
        }
sl@73
   205
sl@73
   206
sl@73
   207
sl@73
   208
    }
sl@73
   209
sl@73
   210
sl@20
   211
}