Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1367 was 1367, checked in by kgrading, 16 years ago

second fix (updated internal interfaces) (#529)

File size: 8.7 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.ProcessJobResultCompleted += new EventHandler<ProcessJobResultCompletedEventArgs>(proxy_ProcessJobResultCompleted);
83        proxy.ProcessHeartBeatCompleted += new EventHandler<ProcessHeartBeatCompletedEventArgs>(proxy_ProcessHeartBeatCompleted);
84        proxy.Open();
85
86        ConnState = NetworkEnum.WcfConnState.Connected;
87        ConnectedSince = DateTime.Now;
88       
89        if (Connected != null)
90          Connected(this, new EventArgs());                               
91        //Todo: This won't be hit. EVER       
92        if (ConnState == NetworkEnum.WcfConnState.Failed)
93          ConnectionRestored(this, new EventArgs());       
94      }
95      catch (Exception ex) {
96        //Todo: Rename to HandleNetworkError
97        NetworkErrorHandling(ex);
98      }
99    }
100
101    /// <summary>
102    /// Changes the Connectionsettings (serverIP & serverPort) and reconnects
103    /// </summary>
104    /// <param name="serverIP">current Server IP</param>
105    /// <param name="serverPort">current Server Port</param>
106    public void Connect(String serverIP, int serverPort) {
107      String oldIp = this.ServerIP;
108      int oldPort = this.ServerPort;
109      this.ServerIP = serverIP;
110      this.ServerPort = serverPort;     
111      Connect();
112      if (oldIp != serverIP || oldPort != ServerPort)
113        if(ServerChanged != null)
114          ServerChanged(this, new EventArgs());
115    }
116   
117    /// <summary>
118    /// Disconnects the Client from the Server
119    /// </summary>
120    public void Disconnect() {
121      ConnState = NetworkEnum.WcfConnState.Disconnected;
122    }
123
124    /// <summary>
125    /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
126    /// </summary>
127    /// <param name="e">The Exception</param>
128    private void NetworkErrorHandling(Exception e) {
129      ConnState = NetworkEnum.WcfConnState.Failed;
130      Logging.GetInstance().Error(this.ToString(), "exception: ", e);
131    }
132
133   
134
135    /// <summary>
136    /// Methods for the Server Login
137    /// </summary>
138    #region Login
139    public event System.EventHandler<LoginCompletedEventArgs> LoginCompleted;
140    public void LoginAsync(ClientInfo clientInfo) {
141      if (ConnState == NetworkEnum.WcfConnState.Connected)
142        proxy.LoginAsync(clientInfo);
143    }
144    private void proxy_LoginCompleted(object sender, LoginCompletedEventArgs e) {
145      if (e.Error == null)
146        LoginCompleted(sender, e);
147      else
148        NetworkErrorHandling(e.Error.InnerException);
149    }
150
151    public void LoginSync(ClientInfo clientInfo) {
152      try {
153        if (ConnState == NetworkEnum.WcfConnState.Connected) {
154          Response res = proxy.Login(clientInfo);
155          ConnState = NetworkEnum.WcfConnState.Loggedin;
156          Logging.GetInstance().Info(this.ToString(), res.StatusMessage);
157        }
158      }
159      catch (Exception e) {
160        NetworkErrorHandling(e);
161      }
162    }
163
164    #endregion
165
166    /// <summary>
167    /// Pull a Job from the Server
168    /// </summary>
169    #region PullJob
170    public event System.EventHandler<SendJobCompletedEventArgs> SendJobCompleted;
171    public void SendJobAsync(Guid guid) {
172      if (ConnState == NetworkEnum.WcfConnState.Loggedin)       
173        proxy.SendJobAsync(guid);
174    }
175    void proxy_SendJobCompleted(object sender, SendJobCompletedEventArgs e) {
176      if (e.Error == null)
177        SendJobCompleted(sender, e);
178      else
179        NetworkErrorHandling(e.Error);
180    }
181    #endregion
182
183    /// <summary>
184    /// Send back finished Job Results
185    /// </summary>
186    #region SendJobResults
187    public event System.EventHandler<ProcessJobResultCompletedEventArgs> ProcessJobResultCompleted;
188    public void ProcessJobResultAsync(Guid clientId, long jobId, byte[] result, double percentage, Exception exception, bool finished) {
189      if (ConnState == NetworkEnum.WcfConnState.Loggedin)
190        proxy.ProcessJobResultAsync(clientId, jobId, result, percentage, exception, finished);
191    }
192    private void proxy_ProcessJobResultCompleted(object sender, ProcessJobResultCompletedEventArgs e) {
193      if (e.Error == null)
194        ProcessJobResultCompleted(sender, e);
195      else
196        NetworkErrorHandling(e.Error);
197    }
198
199    #endregion
200
201    /// <summary>
202    /// Methods for sending the periodically Heartbeat
203    /// </summary>
204    #region Heartbeat
205
206    public event System.EventHandler<ProcessHeartBeatCompletedEventArgs> SendHeartBeatCompleted;
207    public void SendHeartBeatAsync(HeartBeatData hbd) {
208      if (ConnState == NetworkEnum.WcfConnState.Loggedin)
209        proxy.ProcessHeartBeatAsync(hbd);
210    }
211
212    private void proxy_ProcessHeartBeatCompleted(object sender, ProcessHeartBeatCompletedEventArgs e) {
213      if (e.Error == null)
214        SendHeartBeatCompleted(sender, e);
215      else
216        NetworkErrorHandling(e.Error);
217    }
218
219    #endregion 
220
221    /// <summary>
222    /// Send back finished and Stored Job Results
223    /// </summary>
224    #region SendJobResults
225    public event System.EventHandler<ProcessJobResultCompletedEventArgs> ProcessStoredJobResultCompleted;
226    public void ProcessStoredJobResultAsync(Guid clientId, long jobId, byte[] result, double percentage, Exception exception, bool finished) {
227      if (ConnState == NetworkEnum.WcfConnState.Loggedin)
228        //TODO: some sort of algo for the stored jobs
229        proxy.ProcessJobResultAsync(clientId, jobId, result, percentage, exception, finished);
230    } 
231    #endregion
232
233    public ResponseResultReceived SendStoredJobResultsSync(Guid clientId, long jobId, byte[] result, double percentage, Exception exception, bool finished) {
234      return proxy.ProcessJobResult(clientId, jobId, result, percentage, exception, finished);
235    }
236  }
237}
Note: See TracBrowser for help on using the repository browser.