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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.PluginInfrastructure;
|
---|
27 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Clients.Hive.SlaveCore.Tests {
|
---|
30 | internal class MockHiveService : IHiveService {
|
---|
31 | private static List<Job> jobs;
|
---|
32 | private static List<JobData> jobDatas;
|
---|
33 | private static List<HiveExperiment> hiveExperiments;
|
---|
34 | private static List<Plugin> plugins = null;
|
---|
35 | public List<Job> ResultJobs { get; set; }
|
---|
36 | public List<JobData> ResultJobDatas { get; set; }
|
---|
37 | private static List<PluginDescription> pDescs = null;
|
---|
38 | private static int cnt = 0;
|
---|
39 |
|
---|
40 | public List<List<MessageContainer>> Messages { get; set; }
|
---|
41 | public static string ServerPluginCachePath { get; set; }
|
---|
42 | public static string ServerConfigFilePath { get; set; }
|
---|
43 |
|
---|
44 | static MockHiveService() {
|
---|
45 | ServerPluginCachePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PluginCacheServer");
|
---|
46 | ServerConfigFilePath = Path.Combine(ServerPluginCachePath, "HeuristicLab 3.3.exe.config");
|
---|
47 |
|
---|
48 | if (plugins == null)
|
---|
49 | plugins = ReadPluginsFromServerCache();
|
---|
50 |
|
---|
51 |
|
---|
52 | //TODO: reuse as fallback?
|
---|
53 | /*jobs = new List<Job>();
|
---|
54 | jobs.Add(new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 });
|
---|
55 | jobs.Add(new Job { Id = Guid.NewGuid(), JobState = JobState.Offline, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 100, ParentJobId = jobs.First().Id });
|
---|
56 | jobs.Add(new Job { Id = Guid.NewGuid(), JobState = JobState.Offline, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 1024, ParentJobId = jobs.First().Id });
|
---|
57 | jobs.Add(new Job { Id = Guid.NewGuid(), JobState = JobState.Offline, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 4096, ParentJobId = jobs.First().Id });
|
---|
58 | byte[] data = PersistenceUtil.Serialize(new MockJob(400, false));
|
---|
59 | jobDatas = new List<JobData>();
|
---|
60 | foreach (var job in jobs) {
|
---|
61 | job.PluginsNeededIds = new List<Guid>();
|
---|
62 | job.PluginsNeededIds.AddRange(plugins.Select(a => a.Id));
|
---|
63 | jobDatas.Add(new JobData() { JobId = job.Id, Data = data });
|
---|
64 | }
|
---|
65 |
|
---|
66 | hiveExperiments = new List<HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment>();
|
---|
67 | hiveExperiments.Add(new HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment() { Id = Guid.NewGuid(), Name = "Hive Exp 1", Description = "", RootJobId = jobs[0].Id });*/
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void updateJobs(List<Job> js, MockJob m) {
|
---|
71 | jobs = js;
|
---|
72 | byte[] data = PersistenceUtil.Serialize(m);
|
---|
73 |
|
---|
74 | jobDatas = new List<JobData>();
|
---|
75 | foreach (var job in jobs) {
|
---|
76 | job.PluginsNeededIds = new List<Guid>();
|
---|
77 | job.PluginsNeededIds.AddRange(plugins.Select(a => a.Id));
|
---|
78 | jobDatas.Add(new JobData() { JobId = job.Id, Data = data });
|
---|
79 | }
|
---|
80 |
|
---|
81 | hiveExperiments = new List<HiveExperiment>();
|
---|
82 | hiveExperiments.Add(new HiveExperiment() { Id = Guid.NewGuid(), Name = "Hive Exp 1", Description = "", RootJobId = jobs[0].Id });
|
---|
83 |
|
---|
84 | ResultJobDatas = new List<JobData>();
|
---|
85 | ResultJobs = new List<Job>();
|
---|
86 | }
|
---|
87 |
|
---|
88 | private static List<Plugin> ReadPluginsFromServerCache() {
|
---|
89 | PluginManager pm = new PluginManager(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PluginCacheServer"));
|
---|
90 | pm.DiscoverAndCheckPlugins();
|
---|
91 | pDescs = pm.Plugins.ToList();
|
---|
92 | plugins = new List<Plugin>();
|
---|
93 | foreach (PluginDescription d in pDescs) {
|
---|
94 | Plugin p = new Plugin();
|
---|
95 | p.Name = d.Name;
|
---|
96 | p.Version = d.Version;
|
---|
97 | p.Id = Guid.NewGuid();
|
---|
98 | plugins.Add(p);
|
---|
99 |
|
---|
100 | }
|
---|
101 | return plugins;
|
---|
102 | }
|
---|
103 |
|
---|
104 | #region Job Methods
|
---|
105 | public Guid AddJob(Job job, JobData jobData, List<Guid> slaveGroupIds) {
|
---|
106 | job.Id = Guid.NewGuid();
|
---|
107 | jobData.JobId = job.Id;
|
---|
108 | jobs.Add(job);
|
---|
109 | jobDatas.Add(jobData);
|
---|
110 | return job.Id;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
|
---|
114 | job.Id = Guid.NewGuid();
|
---|
115 | job.ParentJobId = parentJobId;
|
---|
116 | jobData.JobId = job.Id;
|
---|
117 | jobs.Add(job);
|
---|
118 | jobDatas.Add(jobData);
|
---|
119 | return job.Id;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public PluginData GetConfigurationFile() {
|
---|
123 | byte[] cf = File.ReadAllBytes(ServerConfigFilePath);
|
---|
124 | PluginData pd = new PluginData();
|
---|
125 | pd.Data = cf;
|
---|
126 | return pd;
|
---|
127 |
|
---|
128 | }
|
---|
129 |
|
---|
130 | public Job GetJob(Guid jobId) {
|
---|
131 | //MockHBM.SendHeartbeat();
|
---|
132 | return jobs.Where(j => j.Id == jobId).SingleOrDefault();
|
---|
133 | }
|
---|
134 |
|
---|
135 | public List<Job> GetJobs() {
|
---|
136 | return jobs;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public List<LightweightJob> GetLightweightJobs(List<Guid> jobIds) {
|
---|
140 | return jobs.Select(j => new LightweightJob(j)).ToList(); // not real
|
---|
141 | }
|
---|
142 |
|
---|
143 | public List<LightweightJob> GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
|
---|
144 | return jobs.Where(j => j.ParentJobId.HasValue).Select(j => new LightweightJob(j)).ToList();
|
---|
145 | }
|
---|
146 |
|
---|
147 | public JobData GetJobData(Guid jobId) {
|
---|
148 | JobData ret = jobDatas.Where(jd => jd.JobId == jobId).SingleOrDefault();
|
---|
149 | //MockHBM.SendHeartbeat();
|
---|
150 | return ret;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void UpdateJob(Job jobDto) {
|
---|
154 | Console.WriteLine("Update Job called!");
|
---|
155 | ResultJobs.Add(jobDto);
|
---|
156 | }
|
---|
157 |
|
---|
158 | public void UpdateJobData(Job jobDto, JobData jobDataDto) {
|
---|
159 | Console.WriteLine("Update JobData called!");
|
---|
160 | ResultJobDatas.Add(jobDataDto);
|
---|
161 | ResultJobs.Add(jobDto);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public void DeleteJob(Guid jobId) {
|
---|
165 | // do nothing
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void DeleteChildJobs(Guid parentJobId) {
|
---|
169 | // do nothing
|
---|
170 | }
|
---|
171 |
|
---|
172 | public Job UpdateJobState(Guid jobId, JobState jobState, Guid? slaveId, Guid? userId, string exception) {
|
---|
173 | throw new NotImplementedException();
|
---|
174 | }
|
---|
175 | #endregion
|
---|
176 |
|
---|
177 | #region JobControl Methods
|
---|
178 | int curJobIdx = 0;
|
---|
179 | public Job AquireJob(Guid slaveId) {
|
---|
180 | if (curJobIdx < jobs.Count) {
|
---|
181 | var job = jobs[curJobIdx];
|
---|
182 | // job.SlaveId = slaveId; // commented out because of change to StateLog
|
---|
183 | curJobIdx++;
|
---|
184 | return job;
|
---|
185 | }
|
---|
186 | throw new Exception("No Jobs left on MockHiveService!");
|
---|
187 | }
|
---|
188 |
|
---|
189 | public void StopJob(Guid jobId) {
|
---|
190 | // do nothing
|
---|
191 | }
|
---|
192 |
|
---|
193 | public void PauseJob(Guid jobId) {
|
---|
194 | // do nothing
|
---|
195 | }
|
---|
196 |
|
---|
197 | public void RestartJob(Guid jobId) {
|
---|
198 | // do nothing
|
---|
199 | }
|
---|
200 | #endregion
|
---|
201 |
|
---|
202 | #region HiveExperiment Methods
|
---|
203 | public HiveExperiment GetHiveExperiment(Guid id) {
|
---|
204 | return hiveExperiments.Where(he => he.Id == id).SingleOrDefault();
|
---|
205 | }
|
---|
206 |
|
---|
207 | public List<HiveExperiment> GetHiveExperiments() {
|
---|
208 | return hiveExperiments;
|
---|
209 | }
|
---|
210 |
|
---|
211 | public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
|
---|
212 | hiveExperimentDto.Id = Guid.NewGuid();
|
---|
213 | hiveExperiments.Add(hiveExperimentDto);
|
---|
214 | return hiveExperimentDto.Id;
|
---|
215 | }
|
---|
216 |
|
---|
217 | public List<HiveExperiment> GetAllHiveExperiments() {
|
---|
218 | return hiveExperiments;
|
---|
219 | }
|
---|
220 |
|
---|
221 | public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
|
---|
222 | // do nothing
|
---|
223 | }
|
---|
224 |
|
---|
225 | public void DeleteHiveExperiment(Guid hiveExperimentId) {
|
---|
226 | // do nothing
|
---|
227 | }
|
---|
228 | #endregion
|
---|
229 |
|
---|
230 | #region Login Methods
|
---|
231 | public void Hello(Slave slaveInfo) {
|
---|
232 | // do nothing
|
---|
233 | }
|
---|
234 |
|
---|
235 | public void GoodBye(Guid id) {
|
---|
236 | // do nothing
|
---|
237 | }
|
---|
238 |
|
---|
239 | public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
|
---|
240 | if (Messages != null && cnt < Messages.Count) {
|
---|
241 | return Messages[cnt++];
|
---|
242 | } else {
|
---|
243 | return new List<MessageContainer>();
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | public Guid GetResourceId(string resourceName) {
|
---|
248 | // todo
|
---|
249 | return Guid.Empty;
|
---|
250 | }
|
---|
251 |
|
---|
252 | #endregion
|
---|
253 |
|
---|
254 | #region Plugin Methods
|
---|
255 | public Guid AddPlugin(Plugin p, List<PluginData> pds) {
|
---|
256 | /*p.Id = Guid.NewGuid();
|
---|
257 | plugins.Add(p);
|
---|
258 | foreach (var pluginData in pds) {
|
---|
259 | pluginData.PluginId = p.Id;
|
---|
260 | pluginDatas.Add(pluginData);
|
---|
261 |
|
---|
262 | }
|
---|
263 | return p.Id;*/
|
---|
264 | throw new NotImplementedException();
|
---|
265 |
|
---|
266 | }
|
---|
267 |
|
---|
268 | public Plugin GetPlugin(Guid pluginId) {
|
---|
269 | if (plugins == null)
|
---|
270 | plugins = ReadPluginsFromServerCache();
|
---|
271 | return plugins.SingleOrDefault(x => x.Id == pluginId);
|
---|
272 | }
|
---|
273 |
|
---|
274 | public List<Plugin> GetPlugins() {
|
---|
275 | if (plugins == null)
|
---|
276 | plugins = ReadPluginsFromServerCache();
|
---|
277 | return plugins;
|
---|
278 | }
|
---|
279 |
|
---|
280 | public List<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
281 | List<PluginData> pluginDatas = new List<PluginData>();
|
---|
282 |
|
---|
283 | foreach (Guid guid in pluginIds) {
|
---|
284 | Plugin plugin = plugins.Find(p => p.Id == guid);
|
---|
285 | if (plugin != null) {
|
---|
286 | PluginDescription desc = pDescs.First(p => p.Name == plugin.Name && p.Version == plugin.Version);
|
---|
287 | if (desc != null) {
|
---|
288 | foreach (IPluginFile file in desc.Files) {
|
---|
289 | PluginData data = new PluginData();
|
---|
290 | data.PluginId = guid;
|
---|
291 | data.Data = File.ReadAllBytes(file.Name);
|
---|
292 | data.FileName = file.Name;
|
---|
293 | pluginDatas.Add(data);
|
---|
294 | }
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 | return pluginDatas;
|
---|
299 | }
|
---|
300 | #endregion
|
---|
301 |
|
---|
302 | #region Slave Methods
|
---|
303 | public Guid AddSlave(Slave slave) {
|
---|
304 | throw new NotImplementedException();
|
---|
305 | }
|
---|
306 |
|
---|
307 | public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
|
---|
308 | throw new NotImplementedException();
|
---|
309 | }
|
---|
310 |
|
---|
311 | public Slave GetSlave(Guid slaveId) {
|
---|
312 | throw new NotImplementedException();
|
---|
313 | }
|
---|
314 |
|
---|
315 | public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
|
---|
316 | throw new NotImplementedException();
|
---|
317 | }
|
---|
318 |
|
---|
319 | public List<Slave> GetSlaves() {
|
---|
320 | throw new NotImplementedException();
|
---|
321 | }
|
---|
322 |
|
---|
323 | public List<SlaveGroup> GetSlaveGroups() {
|
---|
324 | throw new NotImplementedException();
|
---|
325 | }
|
---|
326 |
|
---|
327 | public void UpdateSlave(Slave slave) {
|
---|
328 | throw new NotImplementedException();
|
---|
329 | }
|
---|
330 |
|
---|
331 | public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
|
---|
332 | throw new NotImplementedException();
|
---|
333 | }
|
---|
334 |
|
---|
335 | public void DeleteSlave(Guid slaveId) {
|
---|
336 | throw new NotImplementedException();
|
---|
337 | }
|
---|
338 |
|
---|
339 | public void DeleteSlaveGroup(Guid slaveGroupId) {
|
---|
340 | throw new NotImplementedException();
|
---|
341 | }
|
---|
342 |
|
---|
343 | public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
344 | throw new NotImplementedException();
|
---|
345 | }
|
---|
346 |
|
---|
347 | public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
|
---|
348 | throw new NotImplementedException();
|
---|
349 | }
|
---|
350 | #endregion
|
---|
351 |
|
---|
352 | #region Lifecycle Methods
|
---|
353 | public void TriggerLifecycle(bool force) {
|
---|
354 | throw new NotImplementedException();
|
---|
355 | }
|
---|
356 | #endregion
|
---|
357 | #region Appointment Methods
|
---|
358 | public Guid AddAppointment(Appointment appointment) {
|
---|
359 | throw new NotImplementedException();
|
---|
360 | }
|
---|
361 |
|
---|
362 | public void DeleteAppointment(Guid appointmentId) {
|
---|
363 | throw new NotImplementedException();
|
---|
364 | }
|
---|
365 |
|
---|
366 | public void UpdateAppointment(Appointment appointment) {
|
---|
367 | throw new NotImplementedException();
|
---|
368 | }
|
---|
369 |
|
---|
370 | public List<Appointment> GetScheduleForResource(Guid resourceId) {
|
---|
371 | throw new NotImplementedException();
|
---|
372 | }
|
---|
373 | #endregion
|
---|
374 | }
|
---|
375 | }
|
---|