Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Hive_Management_Console_Refactoring_Ticket508/HeuristicLab.Hive.Client.Communication/WcfService.cs @ 1479

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

added loggedin state for #474

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