GUI/SharpDisplayClient.cs
branchMiniDisplay
changeset 451 3d8af0e778f4
parent 450 f2d8620e2434
child 452 79a3f5946a87
     1.1 --- a/GUI/SharpDisplayClient.cs	Tue Feb 03 10:14:18 2015 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,291 +0,0 @@
     1.4 -
     1.5 -using System;
     1.6 -using System.Collections.Generic;
     1.7 -using System.Linq;
     1.8 -using System.Text;
     1.9 -using System.Threading.Tasks;
    1.10 -using System.Windows.Forms;
    1.11 -using SharpDisplay;
    1.12 -using System.ServiceModel;
    1.13 -using System.ServiceModel.Channels;
    1.14 -
    1.15 -
    1.16 -namespace SharpDisplay
    1.17 -{
    1.18 -    /// <summary>
    1.19 -    ///
    1.20 -    /// </summary>
    1.21 -    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    1.22 -    public class Callback : ICallback, IDisposable
    1.23 -    {
    1.24 -		private DisplayClient DisplayClient { get; set; }
    1.25 -
    1.26 -		public Callback(DisplayClient aClient)
    1.27 -        {
    1.28 -            DisplayClient = aClient;
    1.29 -        }
    1.30 -
    1.31 -        public void OnConnected()
    1.32 -        {
    1.33 -            //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
    1.34 -            //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
    1.35 -
    1.36 -            MessageBox.Show("OnConnected()", "Client");
    1.37 -        }
    1.38 -
    1.39 -
    1.40 -        public void OnCloseOrder()
    1.41 -        {
    1.42 -			DisplayClient.Close();
    1.43 -            //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
    1.44 -            //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
    1.45 -
    1.46 -            //MessageBox.Show("OnServerClosing()", "Client");
    1.47 -            //MainForm.CloseConnectionThreadSafe();
    1.48 -            //MainForm.CloseThreadSafe();
    1.49 -        }
    1.50 -
    1.51 -        //From IDisposable
    1.52 -        public void Dispose()
    1.53 -        {
    1.54 -
    1.55 -        }
    1.56 -    }
    1.57 -
    1.58 -
    1.59 -    /// <summary>
    1.60 -    ///
    1.61 -    /// </summary>
    1.62 -    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
    1.63 -    public class Client : DuplexClientBase<IService>
    1.64 -    {
    1.65 -        public string SessionId { get { return InnerChannel.SessionId; } }
    1.66 -
    1.67 -        public Client(ICallback aCallback)
    1.68 -            : base(new InstanceContext(aCallback), new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
    1.69 -        { }
    1.70 -
    1.71 -        public void SetName(string aClientName)
    1.72 -        {
    1.73 -            Channel.SetName(aClientName);
    1.74 -        }
    1.75 -
    1.76 -        public void SetLayout(TableLayout aLayout)
    1.77 -        {
    1.78 -            Channel.SetLayout(aLayout);
    1.79 -        }
    1.80 -
    1.81 -        public void SetField(DataField aField)
    1.82 -        {
    1.83 -            Channel.SetField(aField);
    1.84 -        }
    1.85 -
    1.86 -        public void SetFields(System.Collections.Generic.IList<DataField> aFields)
    1.87 -        {
    1.88 -            Channel.SetFields(aFields);
    1.89 -        }
    1.90 -
    1.91 -        public int ClientCount()
    1.92 -        {
    1.93 -            return Channel.ClientCount();
    1.94 -        }
    1.95 -
    1.96 -        public bool IsReady()
    1.97 -        {
    1.98 -            return State == CommunicationState.Opened || State == CommunicationState.Created;
    1.99 -        }
   1.100 -    }
   1.101 -
   1.102 -
   1.103 -    /// <summary>
   1.104 -    /// Handle connection with our Sharp Display Server.
   1.105 -    /// If the connection is faulted it will attempt to restart it.
   1.106 -    /// </summary>
   1.107 -    public class DisplayClient
   1.108 -    {
   1.109 -        private Client iClient;
   1.110 -        private Callback iCallback;
   1.111 -        private bool resetingConnection = false;
   1.112 -
   1.113 -        //private MainForm MainForm { get; set; }
   1.114 -        public string SessionId { get { return iClient.SessionId; } }
   1.115 -        public string Name { get; private set; }
   1.116 -        private TableLayout Layout { get; set; }
   1.117 -        private System.Collections.Generic.IList<DataField> Fields { get; set; }
   1.118 -
   1.119 -
   1.120 -        public DisplayClient(/*MainForm aMainForm*/)
   1.121 -        {
   1.122 -            //MainForm = aMainForm;
   1.123 -            Name = "";
   1.124 -            Fields = new DataField[]{};
   1.125 -        }
   1.126 -
   1.127 -        /// <summary>
   1.128 -        /// Initialize our server connection.
   1.129 -        /// </summary>
   1.130 -        public void Open()
   1.131 -        {
   1.132 -            iCallback = new Callback(this);
   1.133 -            iClient = new Client(iCallback);
   1.134 -        }
   1.135 -
   1.136 -        /// <summary>
   1.137 -        /// Terminate our server connection.
   1.138 -        /// </summary>
   1.139 -        public void Close()
   1.140 -        {
   1.141 -            iClient.Close();
   1.142 -            iClient = null;
   1.143 -            iCallback.Dispose();
   1.144 -            iCallback = null;
   1.145 -        }
   1.146 -
   1.147 -        /// <summary>
   1.148 -        /// Tells whether a server connection is available.
   1.149 -        /// </summary>
   1.150 -        /// <returns>True if a server connection is available. False otherwise.</returns>
   1.151 -        public bool IsReady()
   1.152 -        {
   1.153 -            return (iClient != null && iCallback != null && iClient.IsReady());
   1.154 -        }
   1.155 -
   1.156 -        /// <summary>
   1.157 -        /// Check if our server connection is available and attempt reset it if it isn't.
   1.158 -        /// This is notably dealing with timed out connections.
   1.159 -        /// </summary>
   1.160 -        public void CheckConnection()
   1.161 -        {
   1.162 -            if (!IsReady() && !resetingConnection)
   1.163 -            {
   1.164 -                //Try to reconnect
   1.165 -                Open();
   1.166 -
   1.167 -                //Avoid stack overflow in case of persisting failure
   1.168 -                resetingConnection = true;
   1.169 -
   1.170 -                try
   1.171 -                {
   1.172 -                    //On reconnect there is a bunch of properties we need to reset
   1.173 -                    if (Name != "")
   1.174 -                    {
   1.175 -                        iClient.SetName(Name);
   1.176 -                    }
   1.177 -
   1.178 -                    SetLayout(Layout);
   1.179 -                    iClient.SetFields(Fields);
   1.180 -                }
   1.181 -                finally
   1.182 -                {
   1.183 -                    //Make sure our this state does not get out of sync
   1.184 -                    resetingConnection = true;
   1.185 -                }
   1.186 -            }
   1.187 -        }
   1.188 -
   1.189 -        public void SetName(string aClientName)
   1.190 -        {
   1.191 -            Name = aClientName;
   1.192 -            CheckConnection();
   1.193 -            iClient.SetName(aClientName);
   1.194 -        }
   1.195 -
   1.196 -
   1.197 -        public void SetLayout(TableLayout aLayout)
   1.198 -        {
   1.199 -            Layout = aLayout;
   1.200 -            CheckConnection();
   1.201 -            iClient.SetLayout(aLayout);
   1.202 -        }
   1.203 -
   1.204 -        /// <summary>
   1.205 -        /// Set the specified field.
   1.206 -        /// </summary>
   1.207 -        /// <param name="aField"></param>
   1.208 -        /// <returns>True if the specified field was set client side. False means you need to redefine all your fields using CreateFields.</returns>
   1.209 -        public bool SetField(DataField aField)
   1.210 -        {
   1.211 -            int i = 0;
   1.212 -            bool fieldFound = false;
   1.213 -            foreach (DataField field in Fields)
   1.214 -            {
   1.215 -                if (field.Index == aField.Index)
   1.216 -                {
   1.217 -                    //Update our field then
   1.218 -                    Fields[i] = aField;
   1.219 -                    fieldFound = true;
   1.220 -                    break;
   1.221 -                }
   1.222 -                i++;
   1.223 -            }
   1.224 -
   1.225 -            if (!fieldFound)
   1.226 -            {
   1.227 -                //Field not found, make to use SetFields with all your fields at least once after setting your layout.
   1.228 -                return false;
   1.229 -            }
   1.230 -
   1.231 -            CheckConnection();
   1.232 -            iClient.SetField(aField);
   1.233 -            return true;
   1.234 -        }
   1.235 -
   1.236 -        /// <summary>
   1.237 -        /// Use this function when updating existing fields.
   1.238 -        /// </summary>
   1.239 -        /// <param name="aFields"></param>
   1.240 -        public bool SetFields(System.Collections.Generic.IList<DataField> aFields)
   1.241 -        {
   1.242 -            int fieldFoundCount = 0;
   1.243 -            foreach (DataField fieldUpdate in aFields)
   1.244 -            {
   1.245 -                int i = 0;
   1.246 -                foreach (DataField existingField in Fields)
   1.247 -                {
   1.248 -                    if (existingField.Index == fieldUpdate.Index)
   1.249 -                    {
   1.250 -                        //Update our field then
   1.251 -                        Fields[i] = fieldUpdate;
   1.252 -                        fieldFoundCount++;
   1.253 -                        //Move on to the next field
   1.254 -                        break;
   1.255 -                    }
   1.256 -                    i++;
   1.257 -                }
   1.258 -            }
   1.259 -
   1.260 -            //
   1.261 -            if (fieldFoundCount!=aFields.Count)
   1.262 -            {
   1.263 -                //Field not found, make sure to use SetFields with all your fields at least once after setting your layout.
   1.264 -                return false;
   1.265 -            }
   1.266 -
   1.267 -            CheckConnection();
   1.268 -            iClient.SetFields(aFields);
   1.269 -            return true;
   1.270 -        }
   1.271 -
   1.272 -        /// <summary>
   1.273 -        /// Use this function when creating your fields.
   1.274 -        /// </summary>
   1.275 -        /// <param name="aFields"></param>
   1.276 -        public void CreateFields(System.Collections.Generic.IList<DataField> aFields)
   1.277 -        {
   1.278 -            Fields = aFields;
   1.279 -            CheckConnection();
   1.280 -            iClient.SetFields(aFields);
   1.281 -        }
   1.282 -
   1.283 -        public int ClientCount()
   1.284 -        {
   1.285 -            CheckConnection();
   1.286 -            return iClient.ClientCount();
   1.287 -        }
   1.288 -
   1.289 -
   1.290 -
   1.291 -    }
   1.292 -
   1.293 -
   1.294 -}