Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233 more renaming for more consistency

File size: 5.7 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.Clients.Hive.SlaveCore.Properties;
25using HeuristicLab.Common;
26
27namespace HeuristicLab.Clients.Hive.SlaveCore {
28
29  /// <summary>
30  /// WcfService class is implemented as a singleton and works as a communication layer with the Hive server
31  /// </summary>
32  public class WcfService : MarshalByRefObject, IPluginProvider {
33    private static WcfService instance;
34    public DateTime ConnectedSince { get; private set; }
35    public NetworkEnum.WcfConnState ConnState { get; private set; }
36
37    /// <summary>
38    /// Getter for the Instance of the WcfService
39    /// </summary>
40    /// <returns>the Instance of the WcfService class</returns>
41    public static WcfService Instance {
42      get {
43        if (instance == null) {
44          instance = new WcfService();
45          ServiceLocator.Instance.Username = Settings.Default.SlaveUser;
46          ServiceLocator.Instance.Password = Settings.Default.SlavePwd;
47        }
48        return instance;
49      }
50    }
51
52    private WcfService() {
53      ConnState = NetworkEnum.WcfConnState.Disconnected;
54    }
55
56    #region Task Methods
57    public Task GetJob(Guid jobId) {
58      return CallHiveService(s => s.GetJob(jobId));
59    }
60
61    public void UpdateJob(Task job) {
62      CallHiveService(s => s.UpdateJob(job));
63    }
64    #endregion
65
66    #region TaskData Methods
67    public TaskData GetJobData(Guid jobId) {
68      return CallHiveService(s => s.GetJobData(jobId));
69    }
70
71    /// <summary>
72    /// Uploads the taskData and sets a new jobState (while correctly setting Transferring state)
73    /// </summary>
74    public void UpdateJobData(Task job, TaskData jobData, Guid slaveId, TaskState state, string exception = "") {
75      CallHiveService(service => {
76        service.UpdateJob(job);
77        job = service.UpdateJobState(job.Id, TaskState.Transferring, slaveId, null, null);
78        HiveClient.TryAndRepeat(() => {
79          service.UpdateJobData(job, jobData);
80        }, Settings.Default.PluginDeletionRetries, "Could not upload jobdata.");
81        service.UpdateJobState(job.Id, state, slaveId, null, exception);
82      });
83    }
84
85    public Task UpdateJobState(Guid jobId, TaskState jobState, string exception) {
86      return CallHiveService(s => s.UpdateJobState(jobId, jobState, ConfigManager.Instance.GetClientInfo().Id, null, exception));
87    }
88    #endregion
89
90    #region Heartbeat Methods
91    public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
92      return CallHiveService(s => s.Heartbeat(heartbeat));
93    }
94    #endregion
95
96    #region Plugin Methods
97    public Plugin GetPlugin(Guid id) {
98      return CallHiveService(s => s.GetPlugin(id));
99    }
100
101    public IEnumerable<Plugin> GetPlugins() {
102      return CallHiveService(s => s.GetPlugins());
103    }
104
105    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
106      return CallHiveService(s => s.GetPluginDatas(pluginIds));
107    }
108    #endregion
109
110    #region Events
111    public event EventHandler Connected;
112    private void OnConnected() {
113      var handler = Connected;
114      if (handler != null) handler(this, EventArgs.Empty);
115    }
116
117    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
118    private void OnExceptionOccured(Exception e) {
119      var handler = ExceptionOccured;
120      if (handler != null) handler(this, new EventArgs<Exception>(e));
121    }
122    #endregion
123
124    #region Helpers
125    /// <summary>
126    /// Connects with the server, registers the events and fires the Connected event.
127    /// </summary>
128    public void Connect(Slave slaveInfo) {
129      CallHiveService(service => {
130        ConnState = NetworkEnum.WcfConnState.Connected;
131        ConnectedSince = DateTime.Now;
132        service.Hello(slaveInfo);
133        OnConnected();
134      });
135    }
136
137    /// <summary>
138    /// Disconnects the slave from the server
139    /// </summary>
140    public void Disconnect() {
141      CallHiveService(service => {
142        service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
143        ConnState = NetworkEnum.WcfConnState.Disconnected;
144      });
145    }
146
147    /// <summary>
148    /// Network communication error handler.
149    /// Every network error gets logged and the connection switches to faulted state
150    /// </summary>
151    private void HandleNetworkError(Exception e) {
152      ConnState = NetworkEnum.WcfConnState.Failed;
153      OnExceptionOccured(e);
154    }
155
156    public void CallHiveService(Action<IHiveService> call) {
157      try {
158        ServiceLocator.Instance.CallHiveService(call);
159      }
160      catch (Exception ex) {
161        HandleNetworkError(ex);
162      }
163    }
164
165    private T CallHiveService<T>(Func<IHiveService, T> call) {
166      try {
167        return ServiceLocator.Instance.CallHiveService(call);
168      }
169      catch (Exception ex) {
170        HandleNetworkError(ex);
171        return default(T);
172      }
173    }
174    #endregion
175  }
176}
Note: See TracBrowser for help on using the repository browser.