Interface/Interface.cs
author StephaneLenclud
Tue, 26 May 2015 12:25:34 +0200
changeset 139 9e88c1a8cc1d
parent 77 042c1fa136b9
child 148 878904899ff0
permissions -rw-r--r--
Merge
     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         [DataMember]
    54         public List<ColumnStyle> Columns { get; set; }
    55 
    56         [DataMember]
    57         public List<RowStyle> Rows { get; set; }
    58     }
    59 
    60     /// <summary>
    61     /// Define a data field on our display.
    62     /// Data field can be either text or bitmap.
    63     /// </summary>
    64     [DataContract]
    65     public class DataField
    66     {
    67         public DataField()
    68         {
    69             Index = 0;
    70             ColumnSpan = 1;
    71             RowSpan = 1;
    72             //Text
    73             Text = "";
    74             Alignment = ContentAlignment.MiddleLeft;
    75             //Bitmap
    76             Bitmap = null;
    77         }
    78 
    79         //Text constructor
    80         public DataField(int aIndex, string aText = "", ContentAlignment aAlignment = ContentAlignment.MiddleLeft)
    81         {
    82             ColumnSpan = 1;
    83             RowSpan = 1;
    84             Index = aIndex;
    85             Text = aText;
    86             Alignment = aAlignment;
    87             //
    88             Bitmap = null;
    89         }
    90 
    91         //Bitmap constructor
    92         public DataField(int aIndex, Bitmap aBitmap)
    93         {
    94             ColumnSpan = 1;
    95             RowSpan = 1;
    96             Index = aIndex;
    97             Bitmap = aBitmap;
    98             //Text
    99             Text = "";
   100             Alignment = ContentAlignment.MiddleLeft;
   101         }
   102 
   103 
   104         //Generic layout properties
   105         [DataMember]
   106         public int Index { get; set; }
   107 
   108         [DataMember]
   109         public int Column { get; set; }
   110 
   111         [DataMember]
   112         public int Row { get; set; }
   113 
   114         [DataMember]
   115         public int ColumnSpan { get; set; }
   116 
   117         [DataMember]
   118         public int RowSpan { get; set; }
   119 
   120         //Text properties
   121         [DataMember]
   122         public string Text { get; set; }
   123 
   124         [DataMember]
   125         public ContentAlignment Alignment { get; set; }
   126 
   127         //Bitmap properties
   128         [DataMember]
   129         public Bitmap Bitmap { get; set; }
   130 
   131         //
   132         public bool IsBitmap { get{ return Bitmap!=null;} }
   133         //
   134         public bool IsText { get { return Bitmap == null; } }
   135         //
   136         public bool IsSameLayout(DataField aField)
   137         {
   138             return (aField.ColumnSpan == ColumnSpan && aField.RowSpan == RowSpan);
   139         }
   140     }
   141 
   142     /// <summary>
   143     /// Define our SharpDisplay service.
   144     /// Clients and servers must implement it to communicate with one another.
   145     /// Through this service clients can send requests to a server.
   146     /// Through this service a server session can receive requests from a client.
   147     /// </summary>
   148     [ServiceContract(   CallbackContract = typeof(ICallback), SessionMode = SessionMode.Required)]
   149     public interface IService
   150     {
   151         /// <summary>
   152         /// Set the name of this client.
   153         /// Name is a convenient way to recognize your client.
   154         /// Naming you client is not mandatory.
   155         /// In the absence of a name the session ID is often used instead.
   156         /// </summary>
   157         /// <param name="aClientName"></param>
   158         [OperationContract(IsOneWay = true)]
   159         void SetName(string aClientName);
   160 
   161         /// <summary>
   162         /// </summary>
   163         /// <param name="aLayout"></param>
   164         [OperationContract(IsOneWay = true)]
   165         void SetLayout(TableLayout aLayout);
   166 
   167         /// <summary>
   168         /// Set the given field on your display.
   169         /// Fields are often just lines of text or bitmaps.
   170         /// </summary>
   171         /// <param name="aTextFieldIndex"></param>
   172         [OperationContract(IsOneWay = true)]
   173         void SetField(DataField aField);
   174 
   175         /// <summary>
   176         /// Allows a client to set multiple fields at once.
   177         /// </summary>
   178         /// <param name="aFields"></param>
   179         [OperationContract(IsOneWay = true)]
   180         void SetFields(System.Collections.Generic.IList<DataField> aFields);
   181 
   182         /// <summary>
   183         /// Provides the number of clients currently connected
   184         /// </summary>
   185         /// <returns></returns>
   186         [OperationContract()]
   187         int ClientCount();
   188 
   189     }
   190 
   191     /// <summary>
   192     /// SharDisplay callback provides a means for a server to notify its clients.
   193     /// </summary>
   194     public interface ICallback
   195     {
   196         [OperationContract(IsOneWay = true)]
   197         void OnConnected();
   198 
   199         /// <summary>
   200         /// Tell our client to close its connection.
   201         /// Notably sent when the server is shutting down.
   202         /// </summary>
   203         [OperationContract(IsOneWay = true)]
   204         void OnCloseOrder();
   205     }
   206 
   207 }