Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • code cleanups for slave review
  • added switch between privileged and unprivileged sandbox
  • removed childjob management because it's not used
File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Hive;
26using HeuristicLab.PluginInfrastructure;
27
28namespace HeuristicLab.Clients.Hive.SlaveCore {
29
30  /// <summary>
31  /// WcfService class is implemented as a singleton and works as a communication layer with the Hive server
32  /// </summary>
33  public class WcfService : MarshalByRefObject, IPluginProvider {
34    private static WcfService instance;
35    public DateTime ConnectedSince { get; private set; }
36    public NetworkEnum.WcfConnState ConnState { get; private set; }
37
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          ServiceLocator.Instance.Username = "hiveslave";
47          ServiceLocator.Instance.Password = "hiveslave";
48        }
49        return instance;
50      }
51    }
52
53    private WcfService() {
54      ConnState = NetworkEnum.WcfConnState.Disconnected;
55    }
56
57    #region Job Methods
58    public Job GetJob(Guid jobId) {
59      return CallHiveService(s => s.GetJob(jobId));
60    }
61
62    public void UpdateJob(Job job) {
63      CallHiveService(s => s.UpdateJob(job));
64    }
65
66    public Guid AddChildJob(Guid parentJobId, Job job, IJob jobDataObject) {
67      return CallHiveService(s => {
68        JobData jobData = new JobData();
69        IEnumerable<Type> types;
70        jobData.Data = PersistenceUtil.Serialize(jobDataObject, out types);
71        var plugins = new List<IPluginDescription>();
72        PluginUtil.CollectDeclaringPlugins(plugins, types);
73        job.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, new List<Plugin>(), new List<Plugin>(), plugins, false);
74        return s.AddChildJob(parentJobId, job, jobData);
75      });
76    }
77
78    public IEnumerable<JobData> GetChildJobs(Guid? parentJobId) {
79      return CallHiveService(service => {
80        IEnumerable<LightweightJob> msg = service.GetLightweightChildJobs(parentJobId, false, false);
81
82        List<JobData> jobs = new List<JobData>();
83        foreach (LightweightJob ljob in msg)
84          jobs.Add(service.GetJobData(ljob.Id));
85
86        return jobs;
87      });
88    }
89
90    public void DeleteChildJobs(Guid jobId) {
91      CallHiveService(s => s.DeleteChildJobs(jobId));
92    }
93    #endregion
94
95    #region JobData Methods
96    public JobData GetJobData(Guid jobId) {
97      return CallHiveService(s => s.GetJobData(jobId));
98    }
99
100    /// <summary>
101    /// Uploads the jobData and sets a new jobState (while correctly setting Transferring state)
102    /// </summary>
103    public void UpdateJobData(Job job, JobData jobData, Guid slaveId, JobState state) {
104      CallHiveService(service => {
105        service.UpdateJob(job);
106        job = service.UpdateJobState(job.Id, JobState.Transferring, slaveId, null, null);
107        service.UpdateJobData(job, jobData);
108        service.UpdateJobState(job.Id, state, slaveId, null, null);
109      });
110    }
111
112    public Job UpdateJobState(Guid jobId, JobState jobState, string exception) {
113      return CallHiveService(s => s.UpdateJobState(jobId, jobState, ConfigManager.Instance.GetClientInfo().Id, null, exception));
114    }
115    #endregion
116
117    #region Heartbeat Methods
118    public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
119      return CallHiveService(s => s.Heartbeat(heartbeat));
120    }
121    #endregion
122
123    #region Plugin Methods
124    public Plugin GetPlugin(Guid id) {
125      return CallHiveService(s => s.GetPlugin(id));
126    }
127
128    public IEnumerable<Plugin> GetPlugins() {
129      return CallHiveService(s => s.GetPlugins());
130    }
131
132    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
133      return CallHiveService(s => s.GetPluginDatas(pluginIds));
134    }
135    #endregion
136
137    #region Events
138    public event EventHandler Connected;
139    private void OnConnected() {
140      var handler = Connected;
141      if (handler != null) handler(this, EventArgs.Empty);
142    }
143
144    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
145    private void OnExceptionOccured(Exception e) {
146      var handler = ExceptionOccured;
147      if (handler != null) handler(this, new EventArgs<Exception>(e));
148    }
149    #endregion
150
151    #region Helpers
152    /// <summary>
153    /// Connects with the server, registers the events and fires the Connected event.
154    /// </summary>
155    public void Connect(Slave slaveInfo) {
156      CallHiveService(service => {
157        ConnState = NetworkEnum.WcfConnState.Connected;
158        ConnectedSince = DateTime.Now;
159        service.Hello(slaveInfo);
160        OnConnected();
161      });
162    }
163
164    /// <summary>
165    /// Disconnects the slave from the server
166    /// </summary>
167    public void Disconnect() {
168      CallHiveService(service => {
169        service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
170        ConnState = NetworkEnum.WcfConnState.Disconnected;
171      });
172    }
173
174    /// <summary>
175    /// Network communication error handler.
176    /// Every network error gets logged and the connection switches to faulted state
177    /// </summary>
178    private void HandleNetworkError(Exception e) {
179      ConnState = NetworkEnum.WcfConnState.Failed;
180      OnExceptionOccured(e);
181    }
182
183    public void CallHiveService(Action<IHiveService> call) {
184      try {
185        ServiceLocator.Instance.CallHiveService(call);
186      }
187      catch (Exception ex) {
188        HandleNetworkError(ex);
189      }
190    }
191
192    private T CallHiveService<T>(Func<IHiveService, T> call) {
193      try {
194        return ServiceLocator.Instance.CallHiveService(call);
195      }
196      catch (Exception ex) {
197        HandleNetworkError(ex);
198        return default(T);
199      }
200    }
201    #endregion
202  }
203}
Note: See TracBrowser for help on using the repository browser.