Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6202 was 6004, checked in by ascheibe, 13 years ago

#1233

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