Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1379 was 1379, checked in by kgrading, 15 years ago

splitted the sendfinishedjob / snapshot method in two seperate methods, fixed the locking, added real memory management (#529)

File size: 9.4 KB
Line 
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;
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;
31using HeuristicLab.Hive.Client.Communication.ServerService;
32
33namespace HeuristicLab.Hive.Client.Communication {
34  /// <summary>
35  /// WcfService class is implemented as a Singleton and works as a communication Layer with the Server
36  /// </summary>
37  public class WcfService {
38    private static WcfService instance;
39    /// <summary>
40    /// Getter for the Instance of the WcfService
41    /// </summary>
42    /// <returns>the Instance of the WcfService class</returns>
43    public static WcfService Instance {
44      get {
45        if (instance == null) {
46          instance = new WcfService();
47        }
48        return instance;
49      }
50    }
51
52    public DateTime ConnectedSince { get; private set; }   
53    public NetworkEnum.WcfConnState ConnState { get; private set; }
54    public string ServerIP { get; private set; }
55    public int ServerPort { get; private set; }
56
57    public event EventHandler ConnectionRestored;   
58    public event EventHandler ServerChanged;
59    public event EventHandler Connected;   
60
61    public ClientCommunicatorClient proxy = null;
62
63    /// <summary>
64    /// Constructor
65    /// </summary>
66    private WcfService() {
67      ConnState = NetworkEnum.WcfConnState.Disconnected;
68    }
69
70    /// <summary>
71    /// Connects with the Server, registers the events and fires the Connected (and quiet possibly the ConnectionRestored) Event.
72    /// </summary>
73    public void Connect() {
74      try {
75        proxy = new ClientCommunicatorClient(
76          new NetTcpBinding(),
77          new EndpointAddress("net.tcp://" + ServerIP + ":" + ServerPort + "/HiveServer/ClientCommunicator")
78        );
79
80        proxy.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(proxy_LoginCompleted);
81        proxy.SendJobCompleted += new EventHandler<SendJobCompletedEventArgs>(proxy_SendJobCompleted);
82        proxy.StoreFinishedJobResultCompleted += new EventHandler<StoreFinishedJobResultCompletedEventArgs>(proxy_StoreFinishedJobResultCompleted);
83        proxy.ProcessSnapshotCompleted += new EventHandler<ProcessSnapshotCompletedEventArgs>(proxy_ProcessSnapshotCompleted);
84        proxy.ProcessHeartBeatCompleted += new EventHandler<ProcessHeartBeatCompletedEventArgs>(proxy_ProcessHeartBeatCompleted);
85        proxy.Open();
86
87        ConnState = NetworkEnum.WcfConnState.Connected;
88        ConnectedSince = DateTime.Now;
89       
90        if (Connected != null)
91          Connected(this, new EventArgs());                               
92        //Todo: This won't be hit. EVER       
93        if (ConnState == NetworkEnum.WcfConnState.Failed)
94          ConnectionRestored(this, new EventArgs());       
95      }
96      catch (Exception ex) {     
97        HandleNetworkError(ex);
98      }
99    }
100
101
102
103    /// <summary>
104    /// Changes the Connectionsettings (serverIP & serverPort) and reconnects
105    /// </summary>
106    /// <param name="serverIP">current Server IP</param>
107    /// <param name="serverPort">current Server Port</param>
108    public void Connect(String serverIP, int serverPort) {
109      String oldIp = this.ServerIP;
110      int oldPort = this.ServerPort;
111      this.ServerIP = serverIP;
112      this.ServerPort = serverPort;     
113      Connect();
114      if (oldIp != serverIP || oldPort != ServerPort)
115        if(ServerChanged != null)
116          ServerChanged(this, new EventArgs());
117    }
118   
119    /// <summary>
120    /// Disconnects the Client from the Server
121    /// </summary>
122    public void Disconnect() {
123      ConnState = NetworkEnum.WcfConnState.Disconnected;
124    }
125
126    /// <summary>
127    /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
128    /// </summary>
129    /// <param name="e">The Exception</param>
130    private void HandleNetworkError(Exception e) {
131      ConnState = NetworkEnum.WcfConnState.Failed;
132      Logging.Instance.Error(this.ToString(), "exception: ", e);
133    }
134
135   
136
137    /// <summary>
138    /// Methods for the Server Login
139    /// </summary>
140    #region Login
141    public event System.EventHandler<LoginCompletedEventArgs> LoginCompleted;
142    public void LoginAsync(ClientInfo clientInfo) {
143      if (ConnState == NetworkEnum.WcfConnState.Connected)
144        proxy.LoginAsync(clientInfo);
145    }
146    private void proxy_LoginCompleted(object sender, LoginCompletedEventArgs e) {
147      if (e.Error == null)
148        LoginCompleted(sender, e);
149      else
150        HandleNetworkError(e.Error.InnerException);
151    }
152
153    public void LoginSync(ClientInfo clientInfo) {
154      try {
155        if (ConnState == NetworkEnum.WcfConnState.Connected) {
156          Response res = proxy.Login(clientInfo);
157          ConnState = NetworkEnum.WcfConnState.Loggedin;
158          Logging.Instance.Info(this.ToString(), res.StatusMessage);
159        }
160      }
161      catch (Exception e) {
162        HandleNetworkError(e);
163      }
164    }
165
166    #endregion
167
168    /// <summary>
169    /// Pull a Job from the Server
170    /// </summary>
171    #region PullJob
172    public event System.EventHandler<SendJobCompletedEventArgs> SendJobCompleted;
173    public void SendJobAsync(Guid guid) {
174      if (ConnState == NetworkEnum.WcfConnState.Loggedin)       
175        proxy.SendJobAsync(guid);
176    }
177    void proxy_SendJobCompleted(object sender, SendJobCompletedEventArgs e) {
178      if (e.Error == null)
179        SendJobCompleted(sender, e);
180      else
181        HandleNetworkError(e.Error);
182    }
183    #endregion
184
185    /// <summary>
186    /// Send back finished Job Results
187    /// </summary>
188    #region SendJobResults
189    public event System.EventHandler<StoreFinishedJobResultCompletedEventArgs> StoreFinishedJobResultCompleted;
190    public void StoreFinishedJobResultAsync(Guid clientId, long jobId, byte[] result, double percentage, Exception exception, bool finished) {
191      if (ConnState == NetworkEnum.WcfConnState.Loggedin)
192        proxy.StoreFinishedJobResultAsync(clientId, jobId, result, percentage, exception, finished);
193    }
194    private void proxy_StoreFinishedJobResultCompleted(object sender, StoreFinishedJobResultCompletedEventArgs e) {
195      if (e.Error == null)
196        StoreFinishedJobResultCompleted(sender, e);
197      else
198        HandleNetworkError(e.Error);
199    }
200
201    #endregion
202
203    #region Processsnapshots
204    public event System.EventHandler<ProcessSnapshotCompletedEventArgs> ProcessSnapshotCompleted;
205    public void ProcessSnapshotAsync(Guid clientId, long jobId, byte[] result, double percentage, Exception exception, bool finished) {
206      if(ConnState == NetworkEnum.WcfConnState.Loggedin)
207        proxy.ProcessSnapshotAsync(clientId, jobId, result, percentage, exception);
208    }
209    void proxy_ProcessSnapshotCompleted(object sender, ProcessSnapshotCompletedEventArgs e) {
210      if (e.Error == null)
211        ProcessSnapshotCompleted(sender, e);
212      else
213        HandleNetworkError(e.Error);
214    }   
215   
216    #endregion
217
218    /// <summary>
219    /// Methods for sending the periodically Heartbeat
220    /// </summary>
221    #region Heartbeat
222
223    public event System.EventHandler<ProcessHeartBeatCompletedEventArgs> SendHeartBeatCompleted;
224    public void SendHeartBeatAsync(HeartBeatData hbd) {
225      if (ConnState == NetworkEnum.WcfConnState.Loggedin)
226        proxy.ProcessHeartBeatAsync(hbd);
227    }
228
229    private void proxy_ProcessHeartBeatCompleted(object sender, ProcessHeartBeatCompletedEventArgs e) {
230      if (e.Error == null)
231        SendHeartBeatCompleted(sender, e);
232      else
233        HandleNetworkError(e.Error);
234    }
235
236    #endregion 
237
238    /// <summary>
239    /// Send back finished and Stored Job Results
240    /// </summary>
241    /*#region SendJobResults
242    public event System.EventHandler<StoreFinishedJobResultCompletedEventArgs> ProcessStoredJobResultCompleted;
243    public void ProcessStoredJobResultAsync(Guid clientId, long jobId, byte[] result, double percentage, Exception exception, bool finished) {
244      if (ConnState == NetworkEnum.WcfConnState.Loggedin)
245        //TODO: some sort of algo for the stored jobs
246        proxy.ProcessJobResultAsync(clientId, jobId, result, percentage, exception, finished);
247    } 
248    #endregion  */
249
250    public ResponseResultReceived SendStoredJobResultsSync(Guid clientId, long jobId, byte[] result, double percentage, Exception exception, bool finished) {
251      return proxy.StoreFinishedJobResult(clientId, jobId, result, percentage, exception);
252    }
253  }
254}
Note: See TracBrowser for help on using the repository browser.