GUI/SharpDisplayClient.cs
author StephaneLenclud
Mon, 22 Sep 2014 21:59:11 +0200
branchMiniDisplay
changeset 446 0cb7b9f6a6f8
parent 445 fe4c711fd7f8
child 447 af40a92d480d
permissions -rw-r--r--
Quick and dirty usage of our new SharpDisplay layout for packed mode.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Drawing;
    14 using System.Text;
    15 using System.Diagnostics;
    16 using System.Windows.Forms;
    17 using System.Windows;
    18 using OpenHardwareMonitor.Hardware;
    19 using OpenHardwareMonitor.Utilities;
    20 using System.Runtime.InteropServices;
    21 using UacHelpers;
    22 //
    23 using System.ServiceModel;
    24 using System.Runtime.Serialization;
    25 using SharpDisplay;
    26 
    27 namespace SharpDisplay
    28 {
    29 
    30 
    31 
    32     /// <summary>
    33     /// For client to specify a specific layout.
    34     /// </summary>
    35     [DataContract]
    36     public class TableLayout
    37     {
    38         public TableLayout()
    39         {
    40             Columns = new List<ColumnStyle>();
    41             Rows = new List<RowStyle>();
    42             Cells = new List<DataField>();
    43         }
    44 
    45         public TableLayout(int aColumnCount, int aRowCount)
    46         {
    47             Columns = new List<ColumnStyle>();
    48             Rows = new List<RowStyle>();
    49 
    50             for (int i = 0; i < aColumnCount; i++)
    51             {
    52                 Columns.Add(new ColumnStyle(SizeType.Percent, 100 / aColumnCount));
    53             }
    54 
    55             for (int i = 0; i < aRowCount; i++)
    56             {
    57                 Rows.Add(new RowStyle(SizeType.Percent, 100 / aRowCount));
    58             }
    59         }
    60 
    61         [DataMember]
    62         public List<DataField> Cells { get; set; }
    63 
    64         [DataMember]
    65         public List<ColumnStyle> Columns { get; set; }
    66 
    67         [DataMember]
    68         public List<RowStyle> Rows { get; set; }
    69 
    70     }
    71 
    72     /// <summary>
    73     ///
    74     /// </summary>
    75     [DataContract]
    76     public class DataField
    77     {
    78         [DataMember]
    79         public int Column { get; set; }
    80 
    81         [DataMember]
    82         public int Row { get; set; }
    83 
    84         [DataMember]
    85         public int ColumnSpan { get; set; }
    86 
    87         [DataMember]
    88         public int RowSpan { get; set; }
    89 
    90     }
    91 
    92 
    93     /// <summary>
    94     /// TextField can be send to our server to be displayed on the screen.
    95     /// </summary>
    96     [DataContract]
    97     public class TextField : DataField
    98     {
    99         public TextField()
   100         {
   101             Index = 0;
   102             Text = "";
   103             Alignment = ContentAlignment.MiddleLeft;
   104         }
   105 
   106         public TextField(int aIndex, string aText = "", ContentAlignment aAlignment = ContentAlignment.MiddleLeft)
   107         {
   108             Index = aIndex;
   109             Text = aText;
   110             Alignment = aAlignment;
   111         }
   112 
   113         [DataMember]
   114         public int Index { get; set; }
   115 
   116         [DataMember]
   117         public string Text { get; set; }
   118 
   119         [DataMember]
   120         public ContentAlignment Alignment { get; set; }
   121     }
   122 
   123     /// <summary>
   124     /// Define our SharpDisplay service.
   125     /// Clients and servers must implement it to communicate with one another.
   126     /// Through this service clients can send requests to a server.
   127     /// Through this service a server session can receive requests from a client.
   128     /// </summary>
   129     [ServiceContract(CallbackContract = typeof(ICallback), SessionMode = SessionMode.Required)]
   130     public interface IService
   131     {
   132         /// <summary>
   133         /// Set the name of this client.
   134         /// Name is a convenient way to recognize your client.
   135         /// Naming you client is not mandatory.
   136         /// In the absence of a name the session ID is often used instead.
   137         /// </summary>
   138         /// <param name="aClientName"></param>
   139         [OperationContract(IsOneWay = true)]
   140         void SetName(string aClientName);
   141 
   142 
   143         /// <summary>
   144         /// </summary>
   145         /// <param name="aLayout"></param>
   146         [OperationContract(IsOneWay = true)]
   147         void SetLayout(TableLayout aLayout);
   148 
   149         /// <summary>
   150         /// Put the given text in the given field on your display.
   151         /// Fields are often just lines of text.
   152         /// </summary>
   153         /// <param name="aTextFieldIndex"></param>
   154         [OperationContract(IsOneWay = true)]
   155         void SetText(TextField aTextField);
   156 
   157         /// <summary>
   158         /// Allows a client to set multiple text fields at once.
   159         /// </summary>
   160         /// <param name="aTexts"></param>
   161         [OperationContract(IsOneWay = true)]
   162         void SetTexts(System.Collections.Generic.IList<TextField> aTextFields);
   163 
   164         /// <summary>
   165         /// Provides the number of clients currently connected
   166         /// </summary>
   167         /// <returns></returns>
   168         [OperationContract()]
   169         int ClientCount();
   170 
   171     }
   172 
   173     /// <summary>
   174     /// SharDisplay callback provides a means for a server to notify its clients.
   175     /// </summary>
   176     public interface ICallback
   177     {
   178         [OperationContract(IsOneWay = true)]
   179         void OnConnected();
   180 
   181         /// <summary>
   182         /// Tell our client to close its connection.
   183         /// Notably sent when the server is shutting down.
   184         /// </summary>
   185         [OperationContract(IsOneWay = true)]
   186         void OnCloseOrder();
   187     }
   188 
   189 
   190 
   191 }
   192 
   193 
   194 
   195 namespace SharpDisplay
   196 {
   197 
   198     /// <summary>
   199     ///
   200     /// </summary>
   201     [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
   202     public class Client : DuplexClientBase<IService>
   203     {
   204         public string Name { get; set; }
   205         public string SessionId { get { return InnerChannel.SessionId; } }
   206 
   207         public Client(ICallback aCallback)
   208             : base(new InstanceContext(aCallback), new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
   209         { }
   210 
   211         public void SetName(string aClientName)
   212         {
   213             Name = aClientName;
   214             Channel.SetName(aClientName);
   215         }
   216 
   217         public void SetLayout(TableLayout aLayout)
   218         {
   219             Channel.SetLayout(aLayout);
   220         }
   221 
   222         public void SetText(TextField aTextField)
   223         {
   224             Channel.SetText(aTextField);
   225         }
   226 
   227         public void SetTexts(System.Collections.Generic.IList<TextField> aTextFields)
   228         {
   229             Channel.SetTexts(aTextFields);
   230         }
   231 
   232         public int ClientCount()
   233         {
   234             return Channel.ClientCount();
   235         }
   236 
   237         public bool IsReady()
   238         {
   239             return State == CommunicationState.Opened;
   240         }
   241 
   242     }
   243 
   244 
   245 }