Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Hive.Slave/3.3/WcfService.cs @ 12416

Last change on this file since 12416 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 6.0 KB
RevLine 
[6983]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6983]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();
[7132]44          HiveServiceLocator.Instance.Username = HeuristicLab.Clients.Hive.SlaveCore.Properties.Settings.Default.SlaveUser;
45          HiveServiceLocator.Instance.Password = HeuristicLab.Clients.Hive.SlaveCore.Properties.Settings.Default.SlavePwd;
[6983]46        }
47        return instance;
48      }
49    }
50
51    private WcfService() {
52      ConnState = NetworkEnum.WcfConnState.Disconnected;
53    }
54
55    #region Task Methods
56    public Task GetTask(Guid taskId) {
57      return CallHiveService(s => s.GetTask(taskId));
58    }
59
60    public void UpdateTask(Task task) {
61      CallHiveService(s => s.UpdateTask(task));
62    }
63    #endregion
64
65    #region TaskData Methods
66    public TaskData GetTaskData(Guid taskId) {
67      return CallHiveService(s => s.GetTaskData(taskId));
68    }
69
70    /// <summary>
71    /// Uploads the taskData and sets a new taskState (while correctly setting Transferring state)
72    /// </summary>
73    public void UpdateTaskData(Task task, TaskData taskData, Guid slaveId, TaskState state, string exception = "") {
74      CallHiveService(service => {
75        service.UpdateTask(task);
76        task = service.UpdateTaskState(task.Id, TaskState.Transferring, slaveId, null, null);
77        HiveClient.TryAndRepeat(() => {
78          service.UpdateTaskData(task, taskData);
79        }, HeuristicLab.Clients.Hive.SlaveCore.Properties.Settings.Default.PluginDeletionRetries, "Could not upload jobdata.");
80        service.UpdateTaskState(task.Id, state, slaveId, null, exception);
81      });
82    }
83
84    public Task UpdateJobState(Guid taskId, TaskState taskState, string exception) {
85      return CallHiveService(s => s.UpdateTaskState(taskId, taskState, 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    public int GetNewHeartbeatInterval(Guid id) {
147      int ret = -1;
148      CallHiveService(s => ret = s.GetNewHeartbeatInterval(id));
149      return ret;
150    }
151
152    /// <summary>
153    /// Network communication error handler.
154    /// Every network error gets logged and the connection switches to faulted state
155    /// </summary>
156    private void HandleNetworkError(Exception e) {
157      ConnState = NetworkEnum.WcfConnState.Failed;
158      OnExceptionOccured(e);
159    }
160
161    public void CallHiveService(Action<IHiveService> call) {
162      try {
[7132]163        HiveServiceLocator.Instance.CallHiveService(call);
[6983]164      }
165      catch (Exception ex) {
166        HandleNetworkError(ex);
167      }
168    }
169
170    private T CallHiveService<T>(Func<IHiveService, T> call) {
171      try {
[7132]172        return HiveServiceLocator.Instance.CallHiveService(call);
[6983]173      }
174      catch (Exception ex) {
175        HandleNetworkError(ex);
176        return default(T);
177      }
178    }
179    #endregion
180  }
181}
Note: See TracBrowser for help on using the repository browser.