Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • updated jobstates documentation
  • enhanced ganttChart
  • fixed setting of jobstates
  • added option to force lifecycle-trigger (mainly for testing purposes)
File size: 6.1 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.GetUniqueMachineId(), 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    public IEnumerable<Plugin> GetPlugins() {
115      return CallHiveService(s => s.GetPlugins());
116    }
117
118    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
119      return CallHiveService(s => s.GetPluginDatas(pluginIds));
120    }
121    #endregion
122
123    #region Events
124    public event EventHandler Connected;
125    private void OnConnected() {
126      var handler = Connected;
127      if (handler != null) handler(this, EventArgs.Empty);
128    }
129
130    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
131    private void OnExceptionOccured(Exception e) {
132      var handler = ExceptionOccured;
133      if (handler != null) handler(this, new EventArgs<Exception>(e));
134    }
135    #endregion
136
137    #region Helpers
138    /// <summary>
139    /// Connects with the Server, registers the events and fires the Connected (and quiet possibly the ConnectionRestored) Event.
140    /// </summary>
141    public void Connect(Slave slaveInfo) {
142      CallHiveService(service => {
143        ConnState = NetworkEnum.WcfConnState.Connected;
144        ConnectedSince = DateTime.Now;
145        service.Hello(slaveInfo);
146        OnConnected();
147      });
148    }
149
150    /// <summary>
151    /// Disconnects the Slave from the Server
152    /// </summary>
153    public void Disconnect() {
154      CallHiveService(service => {
155        service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
156        ConnState = NetworkEnum.WcfConnState.Disconnected;
157      });
158    }
159
160    /// <summary>
161    /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
162    /// </summary>
163    /// <param name="e">The Exception</param>
164    private void HandleNetworkError(Exception e) {
165      ConnState = NetworkEnum.WcfConnState.Failed;
166      OnExceptionOccured(e);
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    #endregion
188  }
189}
Note: See TracBrowser for help on using the repository browser.