Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5264 was 5156, checked in by ascheibe, 14 years ago

#1233

  • Implemented communication interface between Slave and a Slave Client using Named Pipes and callbacks
  • Added new project for testing Slave - Client communication
  • Added some copyright info headers
File size: 8.0 KB
RevLine 
[5105]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;
[5156]24using HeuristicLab.Clients.Common;
25using HeuristicLab.Clients.Hive.Slave.Properties;
[5105]26using HeuristicLab.Common;
[5156]27using HeuristicLab.Services.Hive.Common;
28using HeuristicLab.Services.Hive.Common.DataTransfer;
[5105]29using HeuristicLab.Services.Hive.Common.ServiceContracts;
30
31namespace HeuristicLab.Clients.Hive.Salve {
32
33  /// <summary>
34  /// WcfService class is implemented as a Singleton and works as a communication Layer with the Server
35  /// </summary>
36  public class WcfService : MarshalByRefObject {
37    private static WcfService instance;
38    /// <summary>
39    /// Getter for the Instance of the WcfService
40    /// </summary>
41    /// <returns>the Instance of the WcfService class</returns>
42    public static WcfService Instance {
43      get {
44        if (instance == null) {
45          instance = new WcfService();
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;
[5156]55    private void OnConnected() {
[5105]56      var handler = Connected;
57      if (handler != null) handler(this, EventArgs.Empty);
58    }
59
[5156]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
[5105]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      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
78        try {
79          ConnState = NetworkEnum.WcfConnState.Connected;
80          ConnectedSince = DateTime.Now;
81          service.Obj.Hello(Settings.Default.Guid, slaveInfo.Name, slaveInfo.Cores.Value, slaveInfo.Memory.Value);
82          OnConnected();
83        }
84        catch (Exception ex) {
85          HandleNetworkError(ex);
86        }
87      }
88    }
89
90    /// <summary>
91    /// Disconnects the Slave from the Server
92    /// </summary>
93    public void Disconnect() {
94      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
95        try {
96          service.Obj.GoodBye();
97          ConnState = NetworkEnum.WcfConnState.Disconnected;
98        }
99        catch (Exception ex) {
100          HandleNetworkError(ex);
101        }
102      }
103    }
104
105    /// <summary>
106    /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
107    /// </summary>
108    /// <param name="e">The Exception</param>
109    private void HandleNetworkError(Exception e) {
110      ConnState = NetworkEnum.WcfConnState.Failed;
111      OnExceptionOccured(e);
112    }
113
114    /// <summary>
115    /// Aquire a Job from the Server, return the Job
116    /// </summary>
117    public Job AquireJob() {
118      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
119        try {
120          Job job = service.Obj.AquireJob(Settings.Default.Guid);
121          return job;
122        }
123        catch (Exception ex) {
124          HandleNetworkError(ex);
125          return null;
126        }
127      }
128    }
129
130    public JobData GetJobData(Guid jobId) {
131      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
132        try {
133          JobData jobData = service.Obj.GetJobData(jobId);
134          return jobData;
135        }
136        catch (Exception ex) {
137          HandleNetworkError(ex);
138          return null;
139        }
140      }
141    }
142
143    /// <summary>
144    /// used on pause or if job finished to upload the job results
145    /// </summary>
146    /// <param name="job"></param>
147    /// <param name="jobData"></param>   
148    public void UpdateJob(Job job, JobData jobData) {
149      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
150        try {
151          service.Obj.UpdateJob(job, jobData);
152        }
153        catch (Exception ex) {
154          HandleNetworkError(ex);
155        }
156      }
157    }
158
159    public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
160      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
161        try {
162          List<MessageContainer> msg = service.Obj.Heartbeat(heartbeat);
163          return msg;
164        }
165        catch (Exception ex) {
166          HandleNetworkError(ex);
167          return null;
168        }
169      }
170    }
171
172    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
173      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
174        try {
175          IEnumerable<PluginData> msg = service.Obj.GetPluginDatas(pluginIds);
176          return msg;
177        }
178        catch (Exception ex) {
179          HandleNetworkError(ex);
180          return null;
181        }
182      }
183    }
184
185    public IEnumerable<Plugin> GetPlugins() {
186      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
187        try {
188          IEnumerable<Plugin> msg = service.Obj.GetPlugins();
189          return msg;
190        }
191        catch (Exception ex) {
192          HandleNetworkError(ex);
193          return null;
194        }
195      }
196    }
197
198    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
199      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
200        try {
201          Guid msg = service.Obj.AddChildJob(parentJobId, job, jobData);
202          return msg;
203        }
204        catch (Exception ex) {
205          HandleNetworkError(ex);
206          return new Guid();
207        }
208      }
209    }
210
211    public IEnumerable<JobData> GetChildJobs(Guid? parentJobId) {
212      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
213        try {
214          IEnumerable<LightweightJob> msg = service.Obj.GetLightweightChildJobs(parentJobId, false, false);
215
216          List<JobData> jobs = new List<JobData>();
217          foreach (LightweightJob ljob in msg)
218            jobs.Add(service.Obj.GetJobData(ljob.Id));
219
220          return jobs;
221        }
222        catch (Exception ex) {
223          HandleNetworkError(ex);
224          return null;
225        }
226      }
227    }
228
229    public void DeleteChildJobs(Guid jobId) {
230      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
231        try {
232          service.Obj.DeleteChildJobs(jobId);
233        }
234        catch (Exception ex) {
235          HandleNetworkError(ex);
236        }
237      }
238    }
239
240    public PluginData GetConfigurationFile() {
241      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
242        try {
243          PluginData msg = service.Obj.GetConfigurationFile();
244          return msg;
245        }
246        catch (Exception ex) {
247          HandleNetworkError(ex);
248          return null;
249        }
250      }
251    }
252  }
253}
Note: See TracBrowser for help on using the repository browser.