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.Slave.Common;
|
---|
29 | using HeuristicLab.PluginInfrastructure;
|
---|
30 | using System.IO;
|
---|
31 | using System.Runtime.Serialization.Formatters.Binary;
|
---|
32 | using HeuristicLab.Tracing;
|
---|
33 | using HeuristicLab.Hive.Contracts;
|
---|
34 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Hive.Slave.Communication {
|
---|
37 | using Service = HeuristicLab.Hive.Slave.Communication.ServerService;
|
---|
38 | using HeuristicLab.Hive.Slave.Communication.ServerService;
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// WcfService class is implemented as a Singleton and works as a communication Layer with the Server
|
---|
42 | /// </summary>
|
---|
43 | public class WcfService {
|
---|
44 | private static WcfService instance;
|
---|
45 | /// <summary>
|
---|
46 | /// Getter for the Instance of the WcfService
|
---|
47 | /// </summary>
|
---|
48 | /// <returns>the Instance of the WcfService class</returns>
|
---|
49 | public static WcfService Instance {
|
---|
50 | get {
|
---|
51 | if (instance == null) {
|
---|
52 | Logger.Debug("New WcfService Instance created");
|
---|
53 | instance = new WcfService();
|
---|
54 | }
|
---|
55 | return instance;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public DateTime ConnectedSince { get; private set; }
|
---|
60 | public NetworkEnum.WcfConnState ConnState { get; private set; }
|
---|
61 | public string ServerIP { get; private set; }
|
---|
62 | public int ServerPort { get; private set; }
|
---|
63 |
|
---|
64 | public event EventHandler ConnectionRestored;
|
---|
65 | public event EventHandler ServerChanged;
|
---|
66 | public event EventHandler Connected;
|
---|
67 |
|
---|
68 | public ClientFacadeClient proxy = null;
|
---|
69 |
|
---|
70 | /// <summary>
|
---|
71 | /// Constructor
|
---|
72 | /// </summary>
|
---|
73 | private WcfService() {
|
---|
74 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /// <summary>
|
---|
78 | /// Connects with the Server, registers the events and fires the Connected (and quiet possibly the ConnectionRestored) Event.
|
---|
79 | /// </summary>
|
---|
80 | public void Connect() {
|
---|
81 | try {
|
---|
82 | Logger.Debug("Starting the Connection Process");
|
---|
83 | if (String.Empty.Equals(ServerIP) || ServerPort == 0) {
|
---|
84 | Logger.Info("No Server IP or Port set!");
|
---|
85 | return;
|
---|
86 | }
|
---|
87 |
|
---|
88 | Logger.Debug("Creating the new connection proxy");
|
---|
89 | proxy = new ClientFacadeClient(
|
---|
90 | HeuristicLab.Hive.Contracts.WcfSettings.GetStreamedBinding(),
|
---|
91 | new EndpointAddress("net.tcp://" + ServerIP + ":" + ServerPort + "/HiveServer/ClientCommunicator")
|
---|
92 | );
|
---|
93 | Logger.Debug("Created the new connection proxy");
|
---|
94 |
|
---|
95 | Logger.Debug("Registring new Events");
|
---|
96 | proxy.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(proxy_LoginCompleted);
|
---|
97 | proxy.SendStreamedJobCompleted += new EventHandler<SendStreamedJobCompletedEventArgs>(proxy_SendStreamedJobCompleted);
|
---|
98 | proxy.StoreFinishedJobResultStreamedCompleted += new EventHandler<StoreFinishedJobResultStreamedCompletedEventArgs>(proxy_StoreFinishedJobResultStreamedCompleted);
|
---|
99 | proxy.ProcessSnapshotStreamedCompleted += new EventHandler<ProcessSnapshotStreamedCompletedEventArgs>(proxy_ProcessSnapshotStreamedCompleted);
|
---|
100 | proxy.ProcessHeartBeatCompleted += new EventHandler<ProcessHeartBeatCompletedEventArgs>(proxy_ProcessHeartBeatCompleted);
|
---|
101 | Logger.Debug("Registered new Events");
|
---|
102 | Logger.Debug("Opening the Connection");
|
---|
103 | proxy.Open();
|
---|
104 | Logger.Debug("Opened the Connection");
|
---|
105 |
|
---|
106 | ConnState = NetworkEnum.WcfConnState.Connected;
|
---|
107 | ConnectedSince = DateTime.Now;
|
---|
108 |
|
---|
109 | if (Connected != null) {
|
---|
110 | Logger.Debug("Calling the connected Event");
|
---|
111 | Connected(this, new EventArgs());
|
---|
112 | //Todo: This won't be hit. EVER
|
---|
113 | }
|
---|
114 | if (ConnState == NetworkEnum.WcfConnState.Failed)
|
---|
115 | ConnectionRestored(this, new EventArgs());
|
---|
116 | } catch (Exception ex) {
|
---|
117 | HandleNetworkError(ex);
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /// <summary>
|
---|
123 | /// Changes the Connectionsettings (serverIP & serverPort) and reconnects
|
---|
124 | /// </summary>
|
---|
125 | /// <param name="serverIP">current Server IP</param>
|
---|
126 | /// <param name="serverPort">current Server Port</param>
|
---|
127 | public void Connect(String serverIP, int serverPort) {
|
---|
128 | Logger.Debug("Called Connected with " + serverIP + ":" + serverPort);
|
---|
129 | String oldIp = this.ServerIP;
|
---|
130 | int oldPort = this.ServerPort;
|
---|
131 | this.ServerIP = serverIP;
|
---|
132 | this.ServerPort = serverPort;
|
---|
133 | Connect();
|
---|
134 | if (oldIp != serverIP || oldPort != ServerPort)
|
---|
135 | if (ServerChanged != null)
|
---|
136 | ServerChanged(this, new EventArgs());
|
---|
137 | }
|
---|
138 |
|
---|
139 | public void SetIPAndPort(String serverIP, int serverPort) {
|
---|
140 | Logger.Debug("Called with " + serverIP + ":" + serverPort);
|
---|
141 | this.ServerIP = serverIP;
|
---|
142 | this.ServerPort = serverPort;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /// <summary>
|
---|
146 | /// Disconnects the Client from the Server
|
---|
147 | /// </summary>
|
---|
148 | public void Disconnect() {
|
---|
149 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /// <summary>
|
---|
153 | /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
|
---|
154 | /// </summary>
|
---|
155 | /// <param name="e">The Exception</param>
|
---|
156 | private void HandleNetworkError(Exception e) {
|
---|
157 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
158 | Logger.Error("Network exception occurred: " + e);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 | /// <summary>
|
---|
164 | /// Methods for the Server Login
|
---|
165 | /// </summary>
|
---|
166 | #region Login
|
---|
167 | public event System.EventHandler<LoginCompletedEventArgs> LoginCompleted;
|
---|
168 | public void LoginAsync(ClientDto clientInfo) {
|
---|
169 | if (ConnState == NetworkEnum.WcfConnState.Connected) {
|
---|
170 | Logger.Debug("STARTED: Login Async");
|
---|
171 | proxy.LoginAsync(clientInfo);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | private void proxy_LoginCompleted(object sender, LoginCompletedEventArgs e) {
|
---|
175 | if (e.Error == null) {
|
---|
176 | Logger.Debug("ENDED: Login Async");
|
---|
177 | LoginCompleted(sender, e);
|
---|
178 | } else
|
---|
179 | HandleNetworkError(e.Error.InnerException);
|
---|
180 | }
|
---|
181 |
|
---|
182 | public void LoginSync(ClientDto clientInfo) {
|
---|
183 | try {
|
---|
184 | if (ConnState == NetworkEnum.WcfConnState.Connected) {
|
---|
185 | Logger.Debug("STARTED: Login Sync");
|
---|
186 | HeuristicLab.Hive.Contracts.Response res = proxy.Login(clientInfo);
|
---|
187 | if (!res.Success) {
|
---|
188 | Logger.Error("FAILED: Login Failed! " + res.StatusMessage);
|
---|
189 | throw new Exception(res.StatusMessage);
|
---|
190 | } else {
|
---|
191 | Logger.Info("ENDED: Login succeeded" + res.StatusMessage);
|
---|
192 | ConnState = NetworkEnum.WcfConnState.Loggedin;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | } catch (Exception e) {
|
---|
196 | HandleNetworkError(e);
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | #endregion
|
---|
201 |
|
---|
202 | /// <summary>
|
---|
203 | /// Pull a Job from the Server
|
---|
204 | /// </summary>
|
---|
205 | #region PullJob
|
---|
206 | public event System.EventHandler<SendJobCompletedEventArgs> SendJobCompleted;
|
---|
207 | public void SendJobAsync(Guid guid) {
|
---|
208 | if (ConnState == NetworkEnum.WcfConnState.Loggedin) {
|
---|
209 | Logger.Debug("STARTED: Fetching of Jobs from Server for Client");
|
---|
210 | proxy.SendStreamedJobAsync(guid);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | void proxy_SendStreamedJobCompleted(object sender, SendStreamedJobCompletedEventArgs e) {
|
---|
215 | if (e.Error == null) {
|
---|
216 | Logger.Debug("ENDED: Fetching of Jobs from Server for Client");
|
---|
217 | Stream stream = null;
|
---|
218 | MemoryStream memStream = null;
|
---|
219 |
|
---|
220 | try {
|
---|
221 | stream = (Stream)e.Result;
|
---|
222 |
|
---|
223 | //first deserialize the response
|
---|
224 | BinaryFormatter formatter = new BinaryFormatter();
|
---|
225 | ResponseJob response = (ResponseJob)formatter.Deserialize(stream);
|
---|
226 |
|
---|
227 | //second deserialize the BLOB
|
---|
228 | memStream = new MemoryStream();
|
---|
229 |
|
---|
230 | byte[] buffer = new byte[3024];
|
---|
231 | int read = 0;
|
---|
232 | while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) {
|
---|
233 | memStream.Write(buffer, 0, read);
|
---|
234 | }
|
---|
235 |
|
---|
236 | memStream.Close();
|
---|
237 |
|
---|
238 | SendJobCompletedEventArgs completedEventArgs = new SendJobCompletedEventArgs(new object[] { response, memStream.GetBuffer() }, e.Error, e.Cancelled, e.UserState);
|
---|
239 | SendJobCompleted(sender, completedEventArgs);
|
---|
240 | } catch (Exception ex) {
|
---|
241 | Logger.Error(ex);
|
---|
242 | } finally {
|
---|
243 | if (stream != null)
|
---|
244 | stream.Dispose();
|
---|
245 |
|
---|
246 | if (memStream != null)
|
---|
247 | memStream.Dispose();
|
---|
248 | }
|
---|
249 | } else
|
---|
250 | HandleNetworkError(e.Error);
|
---|
251 | }
|
---|
252 |
|
---|
253 | #endregion
|
---|
254 |
|
---|
255 | /// <summary>
|
---|
256 | /// Send back finished Job Results
|
---|
257 | /// </summary>
|
---|
258 | #region SendJobResults
|
---|
259 | public event System.EventHandler<StoreFinishedJobResultCompletedEventArgs> StoreFinishedJobResultCompleted;
|
---|
260 | public void StoreFinishedJobResultAsync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
|
---|
261 | if (ConnState == NetworkEnum.WcfConnState.Loggedin) {
|
---|
262 | Logger.Debug("STARTED: Sending back the finished job results");
|
---|
263 | Logger.Debug("Building stream");
|
---|
264 | Stream stream = GetStreamedJobResult(clientId, jobId, result, percentage, exception);
|
---|
265 | Logger.Debug("Builded stream");
|
---|
266 | Logger.Debug("Making the call");
|
---|
267 | proxy.StoreFinishedJobResultStreamedAsync(stream, stream);
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | private void proxy_StoreFinishedJobResultStreamedCompleted(object sender, StoreFinishedJobResultStreamedCompletedEventArgs e) {
|
---|
272 | Logger.Debug("Finished storing the job");
|
---|
273 | Stream stream = (Stream)e.UserState;
|
---|
274 | if (stream != null) {
|
---|
275 | Logger.Debug("Stream not null, disposing it");
|
---|
276 | stream.Dispose();
|
---|
277 | }
|
---|
278 | if (e.Error == null) {
|
---|
279 | StoreFinishedJobResultCompletedEventArgs args = new StoreFinishedJobResultCompletedEventArgs(new object[] { e.Result }, e.Error, e.Cancelled, e.UserState);
|
---|
280 | Logger.Debug("calling the Finished Job Event");
|
---|
281 | StoreFinishedJobResultCompleted(sender, args);
|
---|
282 | Logger.Debug("ENDED: Sending back the finished job results");
|
---|
283 | } else {
|
---|
284 | HandleNetworkError(e.Error);
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | #endregion
|
---|
289 |
|
---|
290 | #region Processsnapshots
|
---|
291 | public event System.EventHandler<ProcessSnapshotCompletedEventArgs> ProcessSnapshotCompleted;
|
---|
292 | public void ProcessSnapshotAsync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
|
---|
293 | if (ConnState == NetworkEnum.WcfConnState.Loggedin) {
|
---|
294 | Stream stream = GetStreamedJobResult(clientId, jobId, result, percentage, exception);
|
---|
295 | proxy.ProcessSnapshotStreamedAsync(stream, stream);
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | void proxy_ProcessSnapshotStreamedCompleted(object sender, ProcessSnapshotStreamedCompletedEventArgs e) {
|
---|
300 | Stream stream =
|
---|
301 | (Stream)e.UserState;
|
---|
302 | if (stream != null)
|
---|
303 | stream.Dispose();
|
---|
304 |
|
---|
305 | if (e.Error == null) {
|
---|
306 | ProcessSnapshotCompletedEventArgs args =
|
---|
307 | new ProcessSnapshotCompletedEventArgs(
|
---|
308 | new object[] { e.Result }, e.Error, e.Cancelled, e.UserState);
|
---|
309 |
|
---|
310 | ProcessSnapshotCompleted(sender, args);
|
---|
311 | } else
|
---|
312 | HandleNetworkError(e.Error);
|
---|
313 | }
|
---|
314 |
|
---|
315 | #endregion
|
---|
316 |
|
---|
317 | /// <summary>
|
---|
318 | /// Methods for sending the periodically Heartbeat
|
---|
319 | /// </summary>
|
---|
320 | #region Heartbeat
|
---|
321 |
|
---|
322 | public event System.EventHandler<ProcessHeartBeatCompletedEventArgs> ProcessHeartBeatCompleted;
|
---|
323 | public void ProcessHeartBeatAsync(HeartBeatData hbd) {
|
---|
324 | if (ConnState == NetworkEnum.WcfConnState.Loggedin)
|
---|
325 | Logger.Debug("STARTING: sending heartbeat");
|
---|
326 | proxy.ProcessHeartBeatAsync(hbd);
|
---|
327 | }
|
---|
328 |
|
---|
329 | private void proxy_ProcessHeartBeatCompleted(object sender, ProcessHeartBeatCompletedEventArgs e) {
|
---|
330 | if (e.Error == null && e.Result.Success) {
|
---|
331 | ProcessHeartBeatCompleted(sender, e);
|
---|
332 | Logger.Debug("ENDED: sending heartbeats");
|
---|
333 | } else {
|
---|
334 | try {
|
---|
335 | Logger.Error("Error: " + e.Result.StatusMessage);
|
---|
336 | } catch (Exception ex) {
|
---|
337 | Logger.Error("Error: ", ex);
|
---|
338 | }
|
---|
339 | HandleNetworkError(e.Error);
|
---|
340 | }
|
---|
341 | }
|
---|
342 |
|
---|
343 | #endregion
|
---|
344 |
|
---|
345 | /// <summary>
|
---|
346 | /// Send back finished and Stored Job Results
|
---|
347 | /// </summary>
|
---|
348 | private Stream GetStreamedJobResult(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception) {
|
---|
349 | JobResult jobResult = new JobResult();
|
---|
350 | jobResult.ClientId = clientId;
|
---|
351 | jobResult.JobId = jobId;
|
---|
352 | jobResult.Percentage = percentage;
|
---|
353 | jobResult.Exception = exception != null ? exception.Message : "";
|
---|
354 |
|
---|
355 | MultiStream stream = new MultiStream();
|
---|
356 |
|
---|
357 | //first send result
|
---|
358 | stream.AddStream(new StreamedObject<JobResult>(jobResult));
|
---|
359 |
|
---|
360 | //second stream the job binary data
|
---|
361 | MemoryStream memStream = new MemoryStream(result, false);
|
---|
362 | stream.AddStream(memStream);
|
---|
363 |
|
---|
364 | return stream;
|
---|
365 | }
|
---|
366 |
|
---|
367 | public ResponseResultReceived StoreFinishedJobResultsSync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
|
---|
368 | return proxy.StoreFinishedJobResultStreamed(GetStreamedJobResult(clientId, jobId, result, percentage, exception));
|
---|
369 | }
|
---|
370 |
|
---|
371 | public Response IsJobStillNeeded(Guid jobId) {
|
---|
372 | try {
|
---|
373 | Logger.Debug("STARTING: Sync call: IsJobStillNeeded");
|
---|
374 | Response res = proxy.IsJobStillNeeded(jobId);
|
---|
375 | Logger.Debug("ENDED: Sync call: IsJobStillNeeded");
|
---|
376 | return res;
|
---|
377 | } catch (Exception e) {
|
---|
378 | HandleNetworkError(e);
|
---|
379 | return null;
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | public ResponseResultReceived ProcessSnapshotSync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception) {
|
---|
384 | try {
|
---|
385 | return proxy.ProcessSnapshotStreamed(GetStreamedJobResult(clientId, jobId, result, percentage, exception));
|
---|
386 | } catch (Exception e) {
|
---|
387 | HandleNetworkError(e);
|
---|
388 | return null;
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | public List<HeuristicLab.PluginInfrastructure.CachedHivePluginInfoDto> RequestPlugins(List<HivePluginInfoDto> requestedPlugins) {
|
---|
393 | try {
|
---|
394 | Logger.Debug("STARTED: Requesting Plugins for Job");
|
---|
395 | Logger.Debug("STARTED: Getting the stream");
|
---|
396 | Stream stream = proxy.SendStreamedPlugins(requestedPlugins.ToArray());
|
---|
397 | Logger.Debug("ENDED: Getting the stream");
|
---|
398 | BinaryFormatter formatter =
|
---|
399 | new BinaryFormatter();
|
---|
400 | Logger.Debug("STARTED: Deserializing the stream");
|
---|
401 | ResponsePlugin response = (ResponsePlugin)formatter.Deserialize(stream);
|
---|
402 | Logger.Debug("ENDED: Deserializing the stream");
|
---|
403 | if (stream != null)
|
---|
404 | stream.Dispose();
|
---|
405 | return response.Plugins;
|
---|
406 | } catch (Exception e) {
|
---|
407 | HandleNetworkError(e);
|
---|
408 | return null;
|
---|
409 | }
|
---|
410 | }
|
---|
411 |
|
---|
412 | public void Logout(Guid guid) {
|
---|
413 | try {
|
---|
414 | Logger.Debug("STARTED: Logout");
|
---|
415 | proxy.Logout(guid);
|
---|
416 | Logger.Debug("ENDED: Logout");
|
---|
417 | } catch (Exception e) {
|
---|
418 | HandleNetworkError(e);
|
---|
419 | }
|
---|
420 | }
|
---|
421 |
|
---|
422 | public ResponseCalendar GetCalendarSync(Guid clientId) {
|
---|
423 | try {
|
---|
424 | Logger.Debug("STARTED: Syncing Calendars");
|
---|
425 | ResponseCalendar cal = proxy.GetCalendar(clientId);
|
---|
426 | Logger.Debug("ENDED: Syncing Calendars");
|
---|
427 | return cal;
|
---|
428 | } catch (Exception e) {
|
---|
429 | HandleNetworkError(e);
|
---|
430 | return null;
|
---|
431 | }
|
---|
432 | }
|
---|
433 |
|
---|
434 | public Response SetCalendarStatus(Guid clientId, CalendarState state) {
|
---|
435 | try {
|
---|
436 | Logger.Debug("STARTED: Setting Calendar status to: " + state);
|
---|
437 | Response resp = proxy.SetCalendarStatus(clientId, state);
|
---|
438 | Logger.Debug("ENDED: Setting Calendar status to: " + state);
|
---|
439 | return resp;
|
---|
440 | } catch (Exception e) {
|
---|
441 | HandleNetworkError(e);
|
---|
442 | return null;
|
---|
443 | }
|
---|
444 | }
|
---|
445 | }
|
---|
446 | } |
---|