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