Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Client.Communication/3.2/WcfService.cs @ 1579

Last change on this file since 1579 was 1579, checked in by mbecirov, 15 years ago

#528: WCF Service secured - you need to install the certificate in order to run the application properly!

File size: 9.8 KB
RevLine 
[1132]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[923]23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.ServiceModel;
27using HeuristicLab.Hive.Contracts.Interfaces;
28using HeuristicLab.Hive.Contracts;
29using HeuristicLab.Hive.Contracts.BusinessObjects;
30using HeuristicLab.Hive.Client.Common;
[993]31using HeuristicLab.Hive.Client.Communication.ServerService;
[1450]32using HeuristicLab.PluginInfrastructure;
[923]33
34namespace HeuristicLab.Hive.Client.Communication {
[1132]35  /// <summary>
36  /// WcfService class is implemented as a Singleton and works as a communication Layer with the Server
37  /// </summary>
[923]38  public class WcfService {
39    private static WcfService instance;
[1132]40    /// <summary>
41    /// Getter for the Instance of the WcfService
42    /// </summary>
43    /// <returns>the Instance of the WcfService class</returns>
[923]44    public static WcfService Instance {
45      get {
46        if (instance == null) {
47          instance = new WcfService();
48        }
49        return instance;
50      }
51    }
52
[932]53    public DateTime ConnectedSince { get; private set; }   
54    public NetworkEnum.WcfConnState ConnState { get; private set; }
55    public string ServerIP { get; private set; }
56    public int ServerPort { get; private set; }
[923]57
[924]58    public event EventHandler ConnectionRestored;   
[1081]59    public event EventHandler ServerChanged;
60    public event EventHandler Connected;   
[924]61
[993]62    public ClientCommunicatorClient proxy = null;
[923]63
[1132]64    /// <summary>
65    /// Constructor
66    /// </summary>
[923]67    private WcfService() {
[949]68      ConnState = NetworkEnum.WcfConnState.Disconnected;
[923]69    }
[1132]70
71    /// <summary>
72    /// Connects with the Server, registers the events and fires the Connected (and quiet possibly the ConnectionRestored) Event.
73    /// </summary>
[923]74    public void Connect() {
75      try {
[1081]76        proxy = new ClientCommunicatorClient(
[1579]77          WcfSettings.GetBinding(),
[1081]78          new EndpointAddress("net.tcp://" + ServerIP + ":" + ServerPort + "/HiveServer/ClientCommunicator")
79        );
[923]80
81        proxy.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(proxy_LoginCompleted);
[1366]82        proxy.SendJobCompleted += new EventHandler<SendJobCompletedEventArgs>(proxy_SendJobCompleted);
[1379]83        proxy.StoreFinishedJobResultCompleted += new EventHandler<StoreFinishedJobResultCompletedEventArgs>(proxy_StoreFinishedJobResultCompleted);
84        proxy.ProcessSnapshotCompleted += new EventHandler<ProcessSnapshotCompletedEventArgs>(proxy_ProcessSnapshotCompleted);
[1366]85        proxy.ProcessHeartBeatCompleted += new EventHandler<ProcessHeartBeatCompletedEventArgs>(proxy_ProcessHeartBeatCompleted);
[1082]86        proxy.Open();
87
[932]88        ConnState = NetworkEnum.WcfConnState.Connected;
[1097]89        ConnectedSince = DateTime.Now;
90       
[1082]91        if (Connected != null)
92          Connected(this, new EventArgs());                               
[1340]93        //Todo: This won't be hit. EVER       
[1097]94        if (ConnState == NetworkEnum.WcfConnState.Failed)
95          ConnectionRestored(this, new EventArgs());       
[923]96      }
[1371]97      catch (Exception ex) {     
98        HandleNetworkError(ex);
[923]99      }
100    }
101
[1379]102
103
[1132]104    /// <summary>
105    /// Changes the Connectionsettings (serverIP & serverPort) and reconnects
106    /// </summary>
107    /// <param name="serverIP">current Server IP</param>
108    /// <param name="serverPort">current Server Port</param>
[932]109    public void Connect(String serverIP, int serverPort) {
[993]110      String oldIp = this.ServerIP;
111      int oldPort = this.ServerPort;
[932]112      this.ServerIP = serverIP;
[944]113      this.ServerPort = serverPort;     
[923]114      Connect();
[993]115      if (oldIp != serverIP || oldPort != ServerPort)
[1036]116        if(ServerChanged != null)
117          ServerChanged(this, new EventArgs());
[923]118    }
[1132]119   
120    /// <summary>
121    /// Disconnects the Client from the Server
122    /// </summary>
[932]123    public void Disconnect() {
124      ConnState = NetworkEnum.WcfConnState.Disconnected;
125    }
126
[1132]127    /// <summary>
128    /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
129    /// </summary>
130    /// <param name="e">The Exception</param>
[1371]131    private void HandleNetworkError(Exception e) {
[932]132      ConnState = NetworkEnum.WcfConnState.Failed;
[1371]133      Logging.Instance.Error(this.ToString(), "exception: ", e);
[923]134    }
135
[1132]136   
137
138    /// <summary>
139    /// Methods for the Server Login
140    /// </summary>
[923]141    #region Login
142    public event System.EventHandler<LoginCompletedEventArgs> LoginCompleted;
143    public void LoginAsync(ClientInfo clientInfo) {
[932]144      if (ConnState == NetworkEnum.WcfConnState.Connected)
[923]145        proxy.LoginAsync(clientInfo);
146    }
147    private void proxy_LoginCompleted(object sender, LoginCompletedEventArgs e) {
148      if (e.Error == null)
149        LoginCompleted(sender, e);
150      else
[1371]151        HandleNetworkError(e.Error.InnerException);
[923]152    }
[1097]153
154    public void LoginSync(ClientInfo clientInfo) {
155      try {
156        if (ConnState == NetworkEnum.WcfConnState.Connected) {
157          Response res = proxy.Login(clientInfo);
[1255]158          ConnState = NetworkEnum.WcfConnState.Loggedin;
[1371]159          Logging.Instance.Info(this.ToString(), res.StatusMessage);
[1097]160        }
161      }
162      catch (Exception e) {
[1371]163        HandleNetworkError(e);
[1097]164      }
165    }
166
[923]167    #endregion
168
[1132]169    /// <summary>
170    /// Pull a Job from the Server
171    /// </summary>
[923]172    #region PullJob
[1367]173    public event System.EventHandler<SendJobCompletedEventArgs> SendJobCompleted;
174    public void SendJobAsync(Guid guid) {
[1255]175      if (ConnState == NetworkEnum.WcfConnState.Loggedin)       
[1366]176        proxy.SendJobAsync(guid);
[923]177    }
[1366]178    void proxy_SendJobCompleted(object sender, SendJobCompletedEventArgs e) {
[923]179      if (e.Error == null)
[1367]180        SendJobCompleted(sender, e);
[923]181      else
[1371]182        HandleNetworkError(e.Error);
[923]183    }
184    #endregion
185
[1132]186    /// <summary>
187    /// Send back finished Job Results
188    /// </summary>
[923]189    #region SendJobResults
[1379]190    public event System.EventHandler<StoreFinishedJobResultCompletedEventArgs> StoreFinishedJobResultCompleted;
[1449]191    public void StoreFinishedJobResultAsync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
[1255]192      if (ConnState == NetworkEnum.WcfConnState.Loggedin)
[1379]193        proxy.StoreFinishedJobResultAsync(clientId, jobId, result, percentage, exception, finished);
[923]194    }
[1379]195    private void proxy_StoreFinishedJobResultCompleted(object sender, StoreFinishedJobResultCompletedEventArgs e) {
[923]196      if (e.Error == null)
[1379]197        StoreFinishedJobResultCompleted(sender, e);
[923]198      else
[1371]199        HandleNetworkError(e.Error);
[923]200    }
201
202    #endregion
203
[1379]204    #region Processsnapshots
205    public event System.EventHandler<ProcessSnapshotCompletedEventArgs> ProcessSnapshotCompleted;
[1449]206    public void ProcessSnapshotAsync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
[1379]207      if(ConnState == NetworkEnum.WcfConnState.Loggedin)
208        proxy.ProcessSnapshotAsync(clientId, jobId, result, percentage, exception);
209    }
210    void proxy_ProcessSnapshotCompleted(object sender, ProcessSnapshotCompletedEventArgs e) {
211      if (e.Error == null)
212        ProcessSnapshotCompleted(sender, e);
213      else
214        HandleNetworkError(e.Error);
215    }   
216   
217    #endregion
218
[1132]219    /// <summary>
220    /// Methods for sending the periodically Heartbeat
221    /// </summary>
[923]222    #region Heartbeat
223
[1366]224    public event System.EventHandler<ProcessHeartBeatCompletedEventArgs> SendHeartBeatCompleted;
[923]225    public void SendHeartBeatAsync(HeartBeatData hbd) {
[1255]226      if (ConnState == NetworkEnum.WcfConnState.Loggedin)
[1366]227        proxy.ProcessHeartBeatAsync(hbd);
[923]228    }
229
[1366]230    private void proxy_ProcessHeartBeatCompleted(object sender, ProcessHeartBeatCompletedEventArgs e) {
[923]231      if (e.Error == null)
232        SendHeartBeatCompleted(sender, e);
233      else
[1371]234        HandleNetworkError(e.Error);
[923]235    }
236
[1097]237    #endregion 
[1271]238
239    /// <summary>
240    /// Send back finished and Stored Job Results
241    /// </summary>
[1379]242    /*#region SendJobResults
243    public event System.EventHandler<StoreFinishedJobResultCompletedEventArgs> ProcessStoredJobResultCompleted;
[1367]244    public void ProcessStoredJobResultAsync(Guid clientId, long jobId, byte[] result, double percentage, Exception exception, bool finished) {
[1271]245      if (ConnState == NetworkEnum.WcfConnState.Loggedin)
246        //TODO: some sort of algo for the stored jobs
[1366]247        proxy.ProcessJobResultAsync(clientId, jobId, result, percentage, exception, finished);
[1271]248    } 
[1379]249    #endregion  */
[1271]250
[1449]251    public ResponseResultReceived SendStoredJobResultsSync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
[1450]252      return proxy.StoreFinishedJobResult(clientId, jobId, result, percentage, exception);   
[1271]253    }
[1450]254
255    public List<CachedPlugin> RequestPlugins(List<PluginInfo> requestedPlugins) {
256      try {
257        return proxy.SendPlugins(requestedPlugins.ToArray()).Plugins;
258      }
259      catch (Exception e) {
260        HandleNetworkError(e);
261        return null;
262      }
263    }
264
[923]265  }
266}
Note: See TracBrowser for help on using the repository browser.