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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Hive;
|
---|
26 | using HeuristicLab.PluginInfrastructure;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Clients.Hive.SlaveCore {
|
---|
29 |
|
---|
30 | /// <summary>
|
---|
31 | /// WcfService class is implemented as a singleton and works as a communication layer with the Hive server
|
---|
32 | /// </summary>
|
---|
33 | public class WcfService : MarshalByRefObject, IPluginProvider {
|
---|
34 | private static WcfService instance;
|
---|
35 | public DateTime ConnectedSince { get; private set; }
|
---|
36 | public NetworkEnum.WcfConnState ConnState { get; private set; }
|
---|
37 |
|
---|
38 | /// <summary>
|
---|
39 | /// Getter for the Instance of the WcfService
|
---|
40 | /// </summary>
|
---|
41 | /// <returns>the Instance of the WcfService class</returns>
|
---|
42 | public static WcfService Instance {
|
---|
43 | get {
|
---|
44 | if (instance == null) {
|
---|
45 | instance = new WcfService();
|
---|
46 | ServiceLocator.Instance.Username = "hiveslave";
|
---|
47 | ServiceLocator.Instance.Password = "hiveslave";
|
---|
48 | }
|
---|
49 | return instance;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | private WcfService() {
|
---|
54 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
55 | }
|
---|
56 |
|
---|
57 | #region Job Methods
|
---|
58 | public Job GetJob(Guid jobId) {
|
---|
59 | return CallHiveService(s => s.GetJob(jobId));
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void UpdateJob(Job job) {
|
---|
63 | CallHiveService(s => s.UpdateJob(job));
|
---|
64 | }
|
---|
65 |
|
---|
66 | public Guid AddChildJob(Guid parentJobId, Job job, IJob jobDataObject) {
|
---|
67 | return CallHiveService(s => {
|
---|
68 | JobData jobData = new JobData();
|
---|
69 | IEnumerable<Type> types;
|
---|
70 | jobData.Data = PersistenceUtil.Serialize(jobDataObject, out types);
|
---|
71 | var plugins = new List<IPluginDescription>();
|
---|
72 | PluginUtil.CollectDeclaringPlugins(plugins, types);
|
---|
73 | job.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, new List<Plugin>(), new List<Plugin>(), plugins, false);
|
---|
74 | return s.AddChildJob(parentJobId, job, jobData);
|
---|
75 | });
|
---|
76 | }
|
---|
77 |
|
---|
78 | public IEnumerable<JobData> GetChildJobs(Guid? parentJobId) {
|
---|
79 | return CallHiveService(service => {
|
---|
80 | IEnumerable<LightweightJob> msg = service.GetLightweightChildJobs(parentJobId, false, false);
|
---|
81 |
|
---|
82 | List<JobData> jobs = new List<JobData>();
|
---|
83 | foreach (LightweightJob ljob in msg)
|
---|
84 | jobs.Add(service.GetJobData(ljob.Id));
|
---|
85 |
|
---|
86 | return jobs;
|
---|
87 | });
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void DeleteChildJobs(Guid jobId) {
|
---|
91 | CallHiveService(s => s.DeleteChildJobs(jobId));
|
---|
92 | }
|
---|
93 | #endregion
|
---|
94 |
|
---|
95 | #region JobData Methods
|
---|
96 | public JobData GetJobData(Guid jobId) {
|
---|
97 | return CallHiveService(s => s.GetJobData(jobId));
|
---|
98 | }
|
---|
99 |
|
---|
100 | /// <summary>
|
---|
101 | /// Uploads the jobData and sets a new jobState (while correctly setting Transferring state)
|
---|
102 | /// </summary>
|
---|
103 | public void UpdateJobData(Job job, JobData jobData, Guid slaveId, JobState state, string exception = "") {
|
---|
104 | CallHiveService(service => {
|
---|
105 | service.UpdateJob(job);
|
---|
106 | job = service.UpdateJobState(job.Id, JobState.Transferring, slaveId, null, null);
|
---|
107 | HiveClient.TryAndRepeat(() => {
|
---|
108 | service.UpdateJobData(job, jobData);
|
---|
109 | }, 3, "Could not upload jobdata.");
|
---|
110 | service.UpdateJobState(job.Id, state, slaveId, null, exception);
|
---|
111 | });
|
---|
112 | }
|
---|
113 |
|
---|
114 | public Job UpdateJobState(Guid jobId, JobState jobState, string exception) {
|
---|
115 | return CallHiveService(s => s.UpdateJobState(jobId, jobState, ConfigManager.Instance.GetClientInfo().Id, null, exception));
|
---|
116 | }
|
---|
117 | #endregion
|
---|
118 |
|
---|
119 | #region Heartbeat Methods
|
---|
120 | public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
|
---|
121 | return CallHiveService(s => s.Heartbeat(heartbeat));
|
---|
122 | }
|
---|
123 | #endregion
|
---|
124 |
|
---|
125 | #region Plugin Methods
|
---|
126 | public Plugin GetPlugin(Guid id) {
|
---|
127 | return CallHiveService(s => s.GetPlugin(id));
|
---|
128 | }
|
---|
129 |
|
---|
130 | public IEnumerable<Plugin> GetPlugins() {
|
---|
131 | return CallHiveService(s => s.GetPlugins());
|
---|
132 | }
|
---|
133 |
|
---|
134 | public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
135 | return CallHiveService(s => s.GetPluginDatas(pluginIds));
|
---|
136 | }
|
---|
137 | #endregion
|
---|
138 |
|
---|
139 | #region Events
|
---|
140 | public event EventHandler Connected;
|
---|
141 | private void OnConnected() {
|
---|
142 | var handler = Connected;
|
---|
143 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
144 | }
|
---|
145 |
|
---|
146 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
147 | private void OnExceptionOccured(Exception e) {
|
---|
148 | var handler = ExceptionOccured;
|
---|
149 | if (handler != null) handler(this, new EventArgs<Exception>(e));
|
---|
150 | }
|
---|
151 | #endregion
|
---|
152 |
|
---|
153 | #region Helpers
|
---|
154 | /// <summary>
|
---|
155 | /// Connects with the server, registers the events and fires the Connected event.
|
---|
156 | /// </summary>
|
---|
157 | public void Connect(Slave slaveInfo) {
|
---|
158 | CallHiveService(service => {
|
---|
159 | ConnState = NetworkEnum.WcfConnState.Connected;
|
---|
160 | ConnectedSince = DateTime.Now;
|
---|
161 | service.Hello(slaveInfo);
|
---|
162 | OnConnected();
|
---|
163 | });
|
---|
164 | }
|
---|
165 |
|
---|
166 | /// <summary>
|
---|
167 | /// Disconnects the slave from the server
|
---|
168 | /// </summary>
|
---|
169 | public void Disconnect() {
|
---|
170 | CallHiveService(service => {
|
---|
171 | service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
|
---|
172 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
173 | });
|
---|
174 | }
|
---|
175 |
|
---|
176 | /// <summary>
|
---|
177 | /// Network communication error handler.
|
---|
178 | /// Every network error gets logged and the connection switches to faulted state
|
---|
179 | /// </summary>
|
---|
180 | private void HandleNetworkError(Exception e) {
|
---|
181 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
182 | OnExceptionOccured(e);
|
---|
183 | }
|
---|
184 |
|
---|
185 | public void CallHiveService(Action<IHiveService> call) {
|
---|
186 | try {
|
---|
187 | ServiceLocator.Instance.CallHiveService(call);
|
---|
188 | }
|
---|
189 | catch (Exception ex) {
|
---|
190 | HandleNetworkError(ex);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | private T CallHiveService<T>(Func<IHiveService, T> call) {
|
---|
195 | try {
|
---|
196 | return ServiceLocator.Instance.CallHiveService(call);
|
---|
197 | }
|
---|
198 | catch (Exception ex) {
|
---|
199 | HandleNetworkError(ex);
|
---|
200 | return default(T);
|
---|
201 | }
|
---|
202 | }
|
---|
203 | #endregion
|
---|
204 | }
|
---|
205 | } |
---|