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
RevLine 
[5105]1#region License Information
2/* HeuristicLab
[6371]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5105]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;
[6456]24using HeuristicLab.Clients.Hive.SlaveCore.Properties;
[5105]25using HeuristicLab.Common;
26
[5599]27namespace HeuristicLab.Clients.Hive.SlaveCore {
[5105]28
29  /// <summary>
[6371]30  /// WcfService class is implemented as a singleton and works as a communication layer with the Hive server
[5105]31  /// </summary>
[6357]32  public class WcfService : MarshalByRefObject, IPluginProvider {
[5105]33    private static WcfService instance;
[6371]34    public DateTime ConnectedSince { get; private set; }
35    public NetworkEnum.WcfConnState ConnState { get; private set; }
36
[5105]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();
[6456]45          ServiceLocator.Instance.Username = Settings.Default.SlaveUser;
46          ServiceLocator.Instance.Password = Settings.Default.SlavePwd;
[5105]47        }
48        return instance;
49      }
50    }
51
[5636]52    private WcfService() {
53      ConnState = NetworkEnum.WcfConnState.Disconnected;
54    }
55
[6721]56    #region Task Methods
57    public Task GetJob(Guid jobId) {
[5636]58      return CallHiveService(s => s.GetJob(jobId));
59    }
60
[6721]61    public void UpdateJob(Task job) {
[5636]62      CallHiveService(s => s.UpdateJob(job));
63    }
64    #endregion
65
[6721]66    #region TaskData Methods
67    public TaskData GetJobData(Guid jobId) {
[5636]68      return CallHiveService(s => s.GetJobData(jobId));
69    }
70
71    /// <summary>
[6725]72    /// Uploads the taskData and sets a new jobState (while correctly setting Transferring state)
[5636]73    /// </summary>
[6721]74    public void UpdateJobData(Task job, TaskData jobData, Guid slaveId, TaskState state, string exception = "") {
[5636]75      CallHiveService(service => {
76        service.UpdateJob(job);
[6721]77        job = service.UpdateJobState(job.Id, TaskState.Transferring, slaveId, null, null);
[6419]78        HiveClient.TryAndRepeat(() => {
79          service.UpdateJobData(job, jobData);
[6456]80        }, Settings.Default.PluginDeletionRetries, "Could not upload jobdata.");
[6381]81        service.UpdateJobState(job.Id, state, slaveId, null, exception);
[5636]82      });
83    }
84
[6721]85    public Task UpdateJobState(Guid jobId, TaskState jobState, string exception) {
[5790]86      return CallHiveService(s => s.UpdateJobState(jobId, jobState, ConfigManager.Instance.GetClientInfo().Id, null, exception));
[5636]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
[6004]97    public Plugin GetPlugin(Guid id) {
98      return CallHiveService(s => s.GetPlugin(id));
99    }
100
[5636]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
[5105]111    public event EventHandler Connected;
[5156]112    private void OnConnected() {
[5105]113      var handler = Connected;
114      if (handler != null) handler(this, EventArgs.Empty);
115    }
116
[5156]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    }
[5636]122    #endregion
[5156]123
[5636]124    #region Helpers
[5105]125    /// <summary>
[6371]126    /// Connects with the server, registers the events and fires the Connected event.
[5105]127    /// </summary>
[5599]128    public void Connect(Slave slaveInfo) {
[5526]129      CallHiveService(service => {
130        ConnState = NetworkEnum.WcfConnState.Connected;
131        ConnectedSince = DateTime.Now;
132        service.Hello(slaveInfo);
133        OnConnected();
134      });
[5105]135    }
136
137    /// <summary>
[6371]138    /// Disconnects the slave from the server
[5105]139    /// </summary>
140    public void Disconnect() {
[5526]141      CallHiveService(service => {
142        service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
143        ConnState = NetworkEnum.WcfConnState.Disconnected;
144      });
[5105]145    }
146
147    /// <summary>
[6371]148    /// Network communication error handler.
149    /// Every network error gets logged and the connection switches to faulted state
[5105]150    /// </summary>
151    private void HandleNetworkError(Exception e) {
152      ConnState = NetworkEnum.WcfConnState.Failed;
153      OnExceptionOccured(e);
154    }
155
[5526]156    public void CallHiveService(Action<IHiveService> call) {
157      try {
158        ServiceLocator.Instance.CallHiveService(call);
[5105]159      }
[5526]160      catch (Exception ex) {
161        HandleNetworkError(ex);
162      }
[5105]163    }
164
[5526]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      }
[5405]173    }
[5636]174    #endregion
[5105]175  }
176}
Note: See TracBrowser for help on using the repository browser.