Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5599 was 5599, checked in by ascheibe, 14 years ago

#1233

  • rename 'Slave' namespace to 'SlaveCore' (and assemblies etc) to avoid problems with 'Slave' class
  • use svcutil (OKB-style)
File size: 5.8 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    public event EventHandler Connected;
52    private void OnConnected() {
53      var handler = Connected;
54      if (handler != null) handler(this, EventArgs.Empty);
55    }
56
57    public event EventHandler<EventArgs<Exception>> ExceptionOccured;
58    private void OnExceptionOccured(Exception e) {
59      var handler = ExceptionOccured;
60      if (handler != null) handler(this, new EventArgs<Exception>(e));
61    }
62
63    /// <summary>
64    /// Constructor
65    /// </summary>
66    private WcfService() {
67      ConnState = NetworkEnum.WcfConnState.Disconnected;
68    }
69
70    /// <summary>
71    /// Connects with the Server, registers the events and fires the Connected (and quiet possibly the ConnectionRestored) Event.
72    /// </summary>
73    public void Connect(Slave slaveInfo) {
74      CallHiveService(service => {
75        ConnState = NetworkEnum.WcfConnState.Connected;
76        ConnectedSince = DateTime.Now;
77        service.Hello(slaveInfo);
78        OnConnected();
79      });
80    }
81
82    /// <summary>
83    /// Disconnects the Slave from the Server
84    /// </summary>
85    public void Disconnect() {
86      CallHiveService(service => {
87        service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
88        ConnState = NetworkEnum.WcfConnState.Disconnected;
89      });
90    }
91
92    /// <summary>
93    /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
94    /// </summary>
95    /// <param name="e">The Exception</param>
96    private void HandleNetworkError(Exception e) {
97      ConnState = NetworkEnum.WcfConnState.Failed;
98      OnExceptionOccured(e);
99    }
100
101    /// <summary>
102    /// Get a Job from the Server
103    /// </summary>
104    public Job GetJob(Guid jobId) {
105      return CallHiveService(s => s.GetJob(jobId));
106    }
107
108    public JobData GetJobData(Guid jobId) {
109      return CallHiveService(s => s.GetJobData(jobId));
110    }
111
112    public void UpdateJob(Job job) {
113      CallHiveService(s => s.UpdateJob(job));
114    }
115
116    /// <summary>
117    /// used on pause or if job finished to upload the job results
118    /// </summary>
119    /// <param name="job"></param>
120    /// <param name="jobData"></param>   
121    public void UpdateJobData(Job job, JobData jobData, Guid slaveId) {
122      CallHiveService(service => {
123        JobState before = job.State;
124        job.SetState(JobState.Transferring, slaveId, "");
125
126        service.UpdateJob(job);
127
128        service.UpdateJobData(job, jobData);
129
130        job.SetState(before, slaveId, "");
131        service.UpdateJob(job);
132      });
133    }
134
135    public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
136      return CallHiveService(s => s.Heartbeat(heartbeat));
137    }
138
139    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
140      return CallHiveService(s => s.GetPluginDatas(pluginIds));
141    }
142
143    public IEnumerable<Plugin> GetPlugins() {
144      return CallHiveService(s => s.GetPlugins());
145    }
146
147    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
148      return CallHiveService(s => s.AddChildJob(parentJobId, job, jobData));
149    }
150
151    public IEnumerable<JobData> GetChildJobs(Guid? parentJobId) {
152      return CallHiveService(service => {
153        IEnumerable<LightweightJob> msg = service.GetLightweightChildJobs(parentJobId, false, false);
154
155        List<JobData> jobs = new List<JobData>();
156        foreach (LightweightJob ljob in msg)
157          jobs.Add(service.GetJobData(ljob.Id));
158
159        return jobs;
160      });
161    }
162
163    public void DeleteChildJobs(Guid jobId) {
164      CallHiveService(s => s.DeleteChildJobs(jobId));
165    }
166
167    public void CallHiveService(Action<IHiveService> call) {
168      try {
169        ServiceLocator.Instance.CallHiveService(call);
170      }
171      catch (Exception ex) {
172        HandleNetworkError(ex);
173      }
174    }
175
176    private T CallHiveService<T>(Func<IHiveService, T> call) {
177      try {
178        return ServiceLocator.Instance.CallHiveService(call);
179      }
180      catch (Exception ex) {
181        HandleNetworkError(ex);
182        return default(T);
183      }
184    }
185  }
186}
Note: See TracBrowser for help on using the repository browser.