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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using System.ServiceModel;
|
---|
27 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
28 | using HeuristicLab.Hive.Contracts;
|
---|
29 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
30 | using HeuristicLab.Hive.Client.Common;
|
---|
31 | using HeuristicLab.Hive.Client.Communication.ServerService;
|
---|
32 |
|
---|
33 | namespace 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 | Logging.GetInstance().Info(this.ToString(), res.StatusMessage);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | catch (Exception e) {
|
---|
158 | NetworkErrorHandling(e);
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | #endregion
|
---|
163 |
|
---|
164 | /// <summary>
|
---|
165 | /// Pull a Job from the Server
|
---|
166 | /// </summary>
|
---|
167 | #region PullJob
|
---|
168 | public event System.EventHandler<PullJobCompletedEventArgs> PullJobCompleted;
|
---|
169 | public void PullJobAsync(Guid guid) {
|
---|
170 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
171 | proxy.PullJobAsync(guid);
|
---|
172 | }
|
---|
173 | void proxy_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
|
---|
174 | if (e.Error == null)
|
---|
175 | PullJobCompleted(sender, e);
|
---|
176 | else
|
---|
177 | NetworkErrorHandling(e.Error);
|
---|
178 | }
|
---|
179 | #endregion
|
---|
180 |
|
---|
181 | /// <summary>
|
---|
182 | /// Send back finished Job Results
|
---|
183 | /// </summary>
|
---|
184 | #region SendJobResults
|
---|
185 | public event System.EventHandler<SendJobResultCompletedEventArgs> SendJobResultCompleted;
|
---|
186 | public void SendJobResultAsync(Guid clientId, long jobId, byte[] result, double percentage, Exception exception, bool finished) {
|
---|
187 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
188 | proxy.SendJobResultAsync(clientId, jobId, result, percentage, exception, finished);
|
---|
189 | }
|
---|
190 | private void proxy_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
|
---|
191 | if (e.Error == null)
|
---|
192 | SendJobResultCompleted(sender, e);
|
---|
193 | else
|
---|
194 | NetworkErrorHandling(e.Error);
|
---|
195 | }
|
---|
196 |
|
---|
197 | #endregion
|
---|
198 |
|
---|
199 | /// <summary>
|
---|
200 | /// Methods for sending the periodically Heartbeat
|
---|
201 | /// </summary>
|
---|
202 | #region Heartbeat
|
---|
203 |
|
---|
204 | public event System.EventHandler<SendHeartBeatCompletedEventArgs> SendHeartBeatCompleted;
|
---|
205 | public void SendHeartBeatAsync(HeartBeatData hbd) {
|
---|
206 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
207 | proxy.SendHeartBeatAsync(hbd);
|
---|
208 | }
|
---|
209 |
|
---|
210 | private void proxy_SendHeartBeatCompleted(object sender, SendHeartBeatCompletedEventArgs e) {
|
---|
211 | if (e.Error == null)
|
---|
212 | SendHeartBeatCompleted(sender, e);
|
---|
213 | else
|
---|
214 | NetworkErrorHandling(e.Error);
|
---|
215 | }
|
---|
216 |
|
---|
217 | #endregion
|
---|
218 | }
|
---|
219 | }
|
---|