GUI/SharpDisplayClient.cs
author sl
Sun, 21 Sep 2014 21:55:36 +0200
changeset 403 1d10b3a8a235
child 404 7b7fad708159
permissions -rw-r--r--
Support for SharDisplayManager.
     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     //That contract need to be in the same namespace than the original assembly
    30     //otherwise our parameter won't make to the server.
    31     //See: http://stackoverflow.com/questions/14956377/passing-an-object-using-datacontract-in-wcf/25455292#25455292
    32     [DataContract]
    33     public class TextField
    34     {
    35         public TextField()
    36         {
    37             Index = 0;
    38             Text = "";
    39             Alignment = ContentAlignment.MiddleLeft;
    40         }
    41 
    42         public TextField(int aIndex, string aText = "", ContentAlignment aAlignment = ContentAlignment.MiddleLeft)
    43         {
    44             Index = aIndex;
    45             Text = aText;
    46             Alignment = aAlignment;
    47         }
    48 
    49         [DataMember]
    50         public int Index { get; set; }
    51 
    52         [DataMember]
    53         public string Text { get; set; }
    54 
    55         [DataMember]
    56         public ContentAlignment Alignment { get; set; }
    57     }
    58 
    59 
    60     [ServiceContract(CallbackContract = typeof(ICallback), SessionMode = SessionMode.Required)]
    61     public interface IService
    62     {
    63         /// <summary>
    64         /// Set the name of this client.
    65         /// Name is a convenient way to recognize your client.
    66         /// Naming you client is not mandatory.
    67         /// In the absence of a name the session ID is often used instead.
    68         /// </summary>
    69         /// <param name="aClientName"></param>
    70         [OperationContract(IsOneWay = true)]
    71         void SetName(string aClientName);
    72 
    73         /// <summary>
    74         /// Put the given text in the given field on your display.
    75         /// Fields are often just lines of text.
    76         /// </summary>
    77         /// <param name="aTextFieldIndex"></param>
    78         [OperationContract(IsOneWay = true)]
    79         void SetText(TextField aTextField);
    80 
    81         /// <summary>
    82         /// Allows a client to set multiple text fields at once.
    83         /// </summary>
    84         /// <param name="aTexts"></param>
    85         [OperationContract(IsOneWay = true)]
    86         void SetTexts(System.Collections.Generic.IList<TextField> aTextFields);
    87 
    88         /// <summary>
    89         /// Provides the number of clients currently connected
    90         /// </summary>
    91         /// <returns></returns>
    92         [OperationContract()]
    93         int ClientCount();
    94 
    95     }
    96 
    97 
    98     public interface ICallback
    99     {
   100         [OperationContract(IsOneWay = true)]
   101         void OnConnected();
   102 
   103         /// <summary>
   104         /// Tell our client to close its connection.
   105         /// Notably sent when the server is shutting down.
   106         /// </summary>
   107         [OperationContract(IsOneWay = true)]
   108         void OnCloseOrder();
   109     }
   110 }
   111 
   112 
   113 
   114 namespace SharpDisplay
   115 {
   116 
   117     /// <summary>
   118     ///
   119     /// </summary>
   120     [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
   121     public class Client : DuplexClientBase<IService>
   122     {
   123         public string Name { get; set; }
   124         public string SessionId { get { return InnerChannel.SessionId; } }
   125 
   126         public Client(ICallback aCallback)
   127             : base(new InstanceContext(aCallback), new NetTcpBinding(SecurityMode.None, true), new EndpointAddress("net.tcp://localhost:8001/DisplayService"))
   128         { }
   129 
   130         public void SetName(string aClientName)
   131         {
   132             Name = aClientName;
   133             Channel.SetName(aClientName);
   134         }
   135 
   136         public void SetText(TextField aTextField)
   137         {
   138             Channel.SetText(aTextField);
   139         }
   140 
   141         public void SetTexts(System.Collections.Generic.IList<TextField> aTextFields)
   142         {
   143             Channel.SetTexts(aTextFields);
   144         }
   145 
   146         public int ClientCount()
   147         {
   148             return Channel.ClientCount();
   149         }
   150 
   151         public bool IsReady()
   152         {
   153             return State == CommunicationState.Opened;
   154         }
   155 
   156     }
   157 
   158 
   159 }