Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/WcfService.cs @ 5526

Last change on this file since 5526 was 5526, checked in by cneumuel, 13 years ago

#1233

  • fixed handling of StateLog in DataLayer
  • extended unit tests
  • changed style of service calls to OKB-like style (using delegates)
  • added possibility that parent jobs can be finished immediately when child jobs are finished
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Common;
25using HeuristicLab.Services.Hive.Common;
26using HeuristicLab.Services.Hive.Common.DataTransfer;
27using HeuristicLab.Services.Hive.Common.ServiceContracts;
28
29namespace HeuristicLab.Clients.Hive.Slave {
30
31  /// <summary>
32  /// WcfService class is implemented as a Singleton and works as a communication Layer with the Server
33  /// </summary>
34  public class WcfService : MarshalByRefObject {
35    private static WcfService instance;
36    /// <summary>
37    /// Getter for the Instance of the WcfService
38    /// </summary>
39    /// <returns>the Instance of the WcfService class</returns>
40    public static WcfService Instance {
41      get {
42        if (instance == null) {
43          instance = new WcfService();
44          ServiceLocator.Instance.Username = "hiveslave";
45          ServiceLocator.Instance.Password = "hiveslave";
46        }
47        return instance;
48      }
49    }
50
51    public DateTime ConnectedSince { get; private set; }
52    public NetworkEnum.WcfConnState ConnState { get; private set; }
53
54    public event EventHandler Connected;
55    private void OnConnected() {
56      var handler = Connected;
57      if (handler != null) handler(this, EventArgs.Empty);
58    }
59
60    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
61    private void OnExceptionOccured(Exception e) {
62      var handler = ExceptionOccured;
63      if (handler != null) handler(this, new EventArgs<Exception>(e));
64    }
65
66    /// <summary>
67    /// Constructor
68    /// </summary>
69    private WcfService() {
70      ConnState = NetworkEnum.WcfConnState.Disconnected;
71    }
72
73    /// <summary>
74    /// Connects with the Server, registers the events and fires the Connected (and quiet possibly the ConnectionRestored) Event.
75    /// </summary>
76    public void Connect(HeuristicLab.Services.Hive.Common.DataTransfer.Slave slaveInfo) {
77      CallHiveService(service => {
78        ConnState = NetworkEnum.WcfConnState.Connected;
79        ConnectedSince = DateTime.Now;
80        service.Hello(slaveInfo);
81        OnConnected();
82      });
83    }
84
85    /// <summary>
86    /// Disconnects the Slave from the Server
87    /// </summary>
88    public void Disconnect() {
89      CallHiveService(service => {
90        service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
91        ConnState = NetworkEnum.WcfConnState.Disconnected;
92      });
93    }
94
95    /// <summary>
96    /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
97    /// </summary>
98    /// <param name="e">The Exception</param>
99    private void HandleNetworkError(Exception e) {
100      ConnState = NetworkEnum.WcfConnState.Failed;
101      OnExceptionOccured(e);
102    }
103
104    /// <summary>
105    /// Get a Job from the Server
106    /// </summary>
107    public Job GetJob(Guid jobId) {
108      return CallHiveService(s => s.GetJob(jobId));
109    }
110
111    public JobData GetJobData(Guid jobId) {
112      return CallHiveService(s => s.GetJobData(jobId));
113    }
114
115    public void UpdateJob(Job job) {
116      CallHiveService(s => s.UpdateJob(job));
117    }
118
119    /// <summary>
120    /// used on pause or if job finished to upload the job results
121    /// </summary>
122    /// <param name="job"></param>
123    /// <param name="jobData"></param>   
124    public void UpdateJobData(Job job, JobData jobData, Guid slaveId) {
125      CallHiveService(service => {
126        JobState before = job.State;
127        job.SetState(JobState.Transferring, slaveId, "");
128        service.UpdateJob(job);
129
130        service.UpdateJobData(job, jobData);
131
132        job.SetState(before, slaveId, "");
133        service.UpdateJob(job);
134      });
135    }
136
137    public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
138      return CallHiveService(s => s.Heartbeat(heartbeat));
139    }
140
141    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
142      return CallHiveService(s => s.GetPluginDatas(pluginIds));
143    }
144
145    public IEnumerable<Plugin> GetPlugins() {
146      return CallHiveService(s => s.GetPlugins());
147    }
148
149    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
150      return CallHiveService(s => s.AddChildJob(parentJobId, job, jobData));
151    }
152
153    public IEnumerable<JobData> GetChildJobs(Guid? parentJobId) {
154      return CallHiveService(service => {
155        IEnumerable<LightweightJob> msg = service.GetLightweightChildJobs(parentJobId, false, false);
156
157        List<JobData> jobs = new List<JobData>();
158        foreach (LightweightJob ljob in msg)
159          jobs.Add(service.GetJobData(ljob.Id));
160
161        return jobs;
162      });
163    }
164
165    public void DeleteChildJobs(Guid jobId) {
166      CallHiveService(s => s.DeleteChildJobs(jobId));
167    }
168
169    public void CallHiveService(Action<IHiveService> call) {
170      try {
171        ServiceLocator.Instance.CallHiveService(call);
172      }
173      catch (Exception ex) {
174        HandleNetworkError(ex);
175      }
176    }
177
178    private T CallHiveService<T>(Func<IHiveService, T> call) {
179      try {
180        return ServiceLocator.Instance.CallHiveService(call);
181      }
182      catch (Exception ex) {
183        HandleNetworkError(ex);
184        return default(T);
185      }
186    }
187  }
188}
Note: See TracBrowser for help on using the repository browser.