Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

  • renamed UptimeCalendar and Appointment to Downtime
  • added service methods to delete plugins and get plugin by hash
  • made reverted TransactionManager change, made it non-static and added interface
  • moved magic numbers to application settings
File size: 5.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;
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 Hive server
30  /// </summary>
31  public class WcfService : MarshalByRefObject, IPluginProvider {
32    private static WcfService instance;
33    public DateTime ConnectedSince { get; private set; }
34    public NetworkEnum.WcfConnState ConnState { get; private set; }
35
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    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    #endregion
64
65    #region JobData Methods
66    public JobData GetJobData(Guid jobId) {
67      return CallHiveService(s => s.GetJobData(jobId));
68    }
69
70    /// <summary>
71    /// Uploads the jobData and sets a new jobState (while correctly setting Transferring state)
72    /// </summary>
73    public void UpdateJobData(Job job, JobData jobData, Guid slaveId, JobState state, string exception = "") {
74      CallHiveService(service => {
75        service.UpdateJob(job);
76        job = service.UpdateJobState(job.Id, JobState.Transferring, slaveId, null, null);
77        HiveClient.TryAndRepeat(() => {
78          service.UpdateJobData(job, jobData);
79        }, 3, "Could not upload jobdata.");
80        service.UpdateJobState(job.Id, state, slaveId, null, exception);
81      });
82    }
83
84    public Job UpdateJobState(Guid jobId, JobState jobState, string exception) {
85      return CallHiveService(s => s.UpdateJobState(jobId, jobState, ConfigManager.Instance.GetClientInfo().Id, null, exception));
86    }
87    #endregion
88
89    #region Heartbeat Methods
90    public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
91      return CallHiveService(s => s.Heartbeat(heartbeat));
92    }
93    #endregion
94
95    #region Plugin Methods
96    public Plugin GetPlugin(Guid id) {
97      return CallHiveService(s => s.GetPlugin(id));
98    }
99
100    public IEnumerable<Plugin> GetPlugins() {
101      return CallHiveService(s => s.GetPlugins());
102    }
103
104    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
105      return CallHiveService(s => s.GetPluginDatas(pluginIds));
106    }
107    #endregion
108
109    #region Events
110    public event EventHandler Connected;
111    private void OnConnected() {
112      var handler = Connected;
113      if (handler != null) handler(this, EventArgs.Empty);
114    }
115
116    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
117    private void OnExceptionOccured(Exception e) {
118      var handler = ExceptionOccured;
119      if (handler != null) handler(this, new EventArgs<Exception>(e));
120    }
121    #endregion
122
123    #region Helpers
124    /// <summary>
125    /// Connects with the server, registers the events and fires the Connected event.
126    /// </summary>
127    public void Connect(Slave slaveInfo) {
128      CallHiveService(service => {
129        ConnState = NetworkEnum.WcfConnState.Connected;
130        ConnectedSince = DateTime.Now;
131        service.Hello(slaveInfo);
132        OnConnected();
133      });
134    }
135
136    /// <summary>
137    /// Disconnects the slave from the server
138    /// </summary>
139    public void Disconnect() {
140      CallHiveService(service => {
141        service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
142        ConnState = NetworkEnum.WcfConnState.Disconnected;
143      });
144    }
145
146    /// <summary>
147    /// Network communication error handler.
148    /// Every network error gets logged and the connection switches to faulted state
149    /// </summary>
150    private void HandleNetworkError(Exception e) {
151      ConnState = NetworkEnum.WcfConnState.Failed;
152      OnExceptionOccured(e);
153    }
154
155    public void CallHiveService(Action<IHiveService> call) {
156      try {
157        ServiceLocator.Instance.CallHiveService(call);
158      }
159      catch (Exception ex) {
160        HandleNetworkError(ex);
161      }
162    }
163
164    private T CallHiveService<T>(Func<IHiveService, T> call) {
165      try {
166        return ServiceLocator.Instance.CallHiveService(call);
167      }
168      catch (Exception ex) {
169        HandleNetworkError(ex);
170        return default(T);
171      }
172    }
173    #endregion
174  }
175}
Note: See TracBrowser for help on using the repository browser.