Interface/Interface.cs
author StephaneLenclud
Tue, 01 Sep 2015 20:57:06 +0200
changeset 150 728fe28168e2
parent 138 426cc984fd18
permissions -rw-r--r--
Optical Drive Eject: ground work.
     1 //
     2 // Define a public API for both SharpDisplay client and server to use.
     3 //
     4 
     5 using System;
     6 using System.Collections.Generic;
     7 using System.Linq;
     8 using System.Text;
     9 using System.Threading.Tasks;
    10 using System.ServiceModel;
    11 using System.Collections;
    12 using System.Drawing;
    13 using System.Runtime.Serialization;
    14 using System.Windows.Forms;
    15 
    16 
    17 namespace SharpDisplay
    18 {
    19     /// <summary>
    20     /// For client to specify a specific layout.
    21     /// A table layout is sent from client to server and defines data fields layout on our display.
    22     /// </summary>
    23     [DataContract]
    24     public class TableLayout
    25     {
    26         public TableLayout()
    27         {
    28             Columns = new List<ColumnStyle>();
    29             Rows = new List<RowStyle>();
    30         }
    31 
    32         /// <summary>
    33         /// Construct our table layout.
    34         /// </summary>
    35         /// <param name="aColumnCount">Number of column in our table.</param>
    36         /// <param name="aRowCount">Number of rows in our table.</param>
    37         public TableLayout(int aColumnCount, int aRowCount)
    38         {
    39             Columns = new List<ColumnStyle>();
    40             Rows = new List<RowStyle>();
    41 
    42             for (int i = 0; i < aColumnCount; i++)
    43             {
    44                 Columns.Add(new ColumnStyle(SizeType.Percent, 100 / aColumnCount));
    45             }
    46 
    47             for (int i = 0; i < aRowCount; i++)
    48             {
    49                 Rows.Add(new RowStyle(SizeType.Percent, 100 / aRowCount));
    50             }
    51         }
    52 
    53         /// <summary>
    54         /// Compare two TableLayout object.
    55         /// </summary>
    56         /// <returns>Tells whether both layout are identical.</returns>
    57         public bool IsSameAs(TableLayout aTableLayout)
    58         {
    59             //Check rows and columns counts
    60             if (Columns.Count != aTableLayout.Columns.Count || Rows.Count != aTableLayout.Rows.Count)
    61             {
    62                 return false;
    63             }
    64 
    65             //Compare each columns
    66             for (int i=0;i<Columns.Count;i++)
    67             {
    68                 if (Columns[i].SizeType != aTableLayout.Columns[i].SizeType)
    69                 {
    70                     return false;
    71                 }
    72 
    73                 if (Columns[i].Width != aTableLayout.Columns[i].Width)
    74                 {
    75                     return false;
    76                 }
    77             }
    78 
    79             //Compare each columns
    80             for (int i = 0; i < Rows.Count; i++)
    81             {
    82                 if (Rows[i].SizeType != aTableLayout.Rows[i].SizeType)
    83                 {
    84                     return false;
    85                 }
    86 
    87                 if (Rows[i].Height != aTableLayout.Rows[i].Height)
    88                 {
    89                     return false;
    90                 }
    91             }
    92 
    93             //Both rows and columns have the same content.
    94             return true;
    95         }
    96 
    97         [DataMember]
    98         public List<ColumnStyle> Columns { get; set; }
    99 
   100         [DataMember]
   101         public List<RowStyle> Rows { get; set; }
   102     }
   103 
   104     /// <summary>
   105     /// Define a data field on our display.
   106     /// Data field can be either text or bitmap.
   107     /// </summary>
   108     [DataContract]
   109     public class DataField
   110     {
   111         public DataField()
   112         {
   113             Index = 0;
   114             ColumnSpan = 1;
   115             RowSpan = 1;
   116             //Text
   117             Text = "";
   118             Alignment = ContentAlignment.MiddleLeft;
   119             //Bitmap
   120             Bitmap = null;
   121         }
   122 
   123         //Text constructor
   124         public DataField(int aIndex, string aText = "", ContentAlignment aAlignment = ContentAlignment.MiddleLeft)
   125         {
   126             ColumnSpan = 1;
   127             RowSpan = 1;
   128             Index = aIndex;
   129             Text = aText;
   130             Alignment = aAlignment;
   131             //
   132             Bitmap = null;
   133         }
   134 
   135         //Bitmap constructor
   136         public DataField(int aIndex, Bitmap aBitmap)
   137         {
   138             ColumnSpan = 1;
   139             RowSpan = 1;
   140             Index = aIndex;
   141             Bitmap = aBitmap;
   142             //Text
   143             Text = "";
   144             Alignment = ContentAlignment.MiddleLeft;
   145         }
   146 
   147 
   148         //Generic layout properties
   149         [DataMember]
   150         public int Index { get; set; }
   151 
   152         [DataMember]
   153         public int Column { get; set; }
   154 
   155         [DataMember]
   156         public int Row { get; set; }
   157 
   158         [DataMember]
   159         public int ColumnSpan { get; set; }
   160 
   161         [DataMember]
   162         public int RowSpan { get; set; }
   163 
   164         //Text properties
   165         [DataMember]
   166         public string Text { get; set; }
   167 
   168         [DataMember]
   169         public ContentAlignment Alignment { get; set; }
   170 
   171         //Bitmap properties
   172         [DataMember]
   173         public Bitmap Bitmap { get; set; }
   174 
   175         //
   176         public bool IsBitmap { get{ return Bitmap!=null;} }
   177         //
   178         public bool IsText { get { return Bitmap == null; } }
   179         //
   180         public bool IsSameLayout(DataField aField)
   181         {
   182             return (aField.ColumnSpan == ColumnSpan && aField.RowSpan == RowSpan);
   183         }
   184     }
   185 
   186     /// <summary>
   187     /// Define our SharpDisplay service.
   188     /// Clients and servers must implement it to communicate with one another.
   189     /// Through this service clients can send requests to a server.
   190     /// Through this service a server session can receive requests from a client.
   191     /// </summary>
   192     [ServiceContract(   CallbackContract = typeof(ICallback), SessionMode = SessionMode.Required)]
   193     public interface IService
   194     {
   195         /// <summary>
   196         /// Set the name of this client.
   197         /// Name is a convenient way to recognize your client.
   198         /// Naming you client is not mandatory.
   199         /// In the absence of a name the session ID is often used instead.
   200         /// </summary>
   201         /// <param name="aClientName"></param>
   202         [OperationContract(IsOneWay = true)]
   203         void SetName(string aClientName);
   204 
   205         /// <summary>
   206         /// </summary>
   207         /// <param name="aLayout"></param>
   208         [OperationContract(IsOneWay = true)]
   209         void SetLayout(TableLayout aLayout);
   210 
   211         /// <summary>
   212         /// Set the given field on your display.
   213         /// Fields are often just lines of text or bitmaps.
   214         /// </summary>
   215         /// <param name="aTextFieldIndex"></param>
   216         [OperationContract(IsOneWay = true)]
   217         void SetField(DataField aField);
   218 
   219         /// <summary>
   220         /// Allows a client to set multiple fields at once.
   221         /// </summary>
   222         /// <param name="aFields"></param>
   223         [OperationContract(IsOneWay = true)]
   224         void SetFields(System.Collections.Generic.IList<DataField> aFields);
   225 
   226         /// <summary>
   227         /// Provides the number of clients currently connected
   228         /// </summary>
   229         /// <returns></returns>
   230         [OperationContract()]
   231         int ClientCount();
   232 
   233     }
   234 
   235     /// <summary>
   236     /// SharDisplay callback provides a means for a server to notify its clients.
   237     /// </summary>
   238     public interface ICallback
   239     {
   240         [OperationContract(IsOneWay = true)]
   241         void OnConnected();
   242 
   243         /// <summary>
   244         /// Tell our client to close its connection.
   245         /// Notably sent when the server is shutting down.
   246         /// </summary>
   247         [OperationContract(IsOneWay = true)]
   248         void OnCloseOrder();
   249     }
   250 
   251 }