Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests/Mocks/MockHiveService.cs @ 6006

Last change on this file since 6006 was 6006, checked in by cneumuel, 13 years ago

#1233

  • changed relationship between Job and HiveExperiment. There is no more HiveExperiment.RootJobId, instead there is Job.HiveExperimentId.
  • One HiveExperiment can now have multiple Experiments.
  • TreeView supports multiple root nodes
  • HiveEngine creates a HiveExperiment for each set of jobs, so jobs cannot be without an parent experiment anymore (no more loose jobs)
  • updated ExperimentManager binaries
File size: 12.0 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 System.IO;
25using System.Linq;
26using HeuristicLab.PluginInfrastructure;
27using HeuristicLab.PluginInfrastructure.Manager;
28
29namespace 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 = "" });
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 List<LightweightJob> GetLightweightExperimentJobs(Guid experimentId) {
148      throw new NotImplementedException();
149    }
150
151    public JobData GetJobData(Guid jobId) {
152      JobData ret = jobDatas.Where(jd => jd.JobId == jobId).SingleOrDefault();
153      //MockHBM.SendHeartbeat();
154      return ret;
155    }
156
157    public void UpdateJob(Job jobDto) {
158      Console.WriteLine("Update Job called!");
159      ResultJobs.Add(jobDto);
160    }
161
162    public void UpdateJobData(Job jobDto, JobData jobDataDto) {
163      Console.WriteLine("Update JobData called!");
164      ResultJobDatas.Add(jobDataDto);
165      ResultJobs.Add(jobDto);
166    }
167
168    public void DeleteJob(Guid jobId) {
169      // do nothing
170    }
171
172    public void DeleteChildJobs(Guid parentJobId) {
173      // do nothing
174    }
175
176    public Job UpdateJobState(Guid jobId, JobState jobState, Guid? slaveId, Guid? userId, string exception) {
177      throw new NotImplementedException();
178    }
179    #endregion
180
181    #region JobControl Methods
182    int curJobIdx = 0;
183    public Job AquireJob(Guid slaveId) {
184      if (curJobIdx < jobs.Count) {
185        var job = jobs[curJobIdx];
186        // job.SlaveId = slaveId; // commented out because of change to StateLog
187        curJobIdx++;
188        return job;
189      }
190      throw new Exception("No Jobs left on MockHiveService!");
191    }
192
193    public void StopJob(Guid jobId) {
194      // do nothing
195    }
196
197    public void PauseJob(Guid jobId) {
198      // do nothing
199    }
200
201    public void RestartJob(Guid jobId) {
202      // do nothing
203    }
204    #endregion
205
206    #region HiveExperiment Methods
207    public HiveExperiment GetHiveExperiment(Guid id) {
208      return hiveExperiments.Where(he => he.Id == id).SingleOrDefault();
209    }
210
211    public List<HiveExperiment> GetHiveExperiments() {
212      return hiveExperiments;
213    }
214
215    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
216      hiveExperimentDto.Id = Guid.NewGuid();
217      hiveExperiments.Add(hiveExperimentDto);
218      return hiveExperimentDto.Id;
219    }
220
221    public List<HiveExperiment> GetAllHiveExperiments() {
222      return hiveExperiments;
223    }
224
225    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
226      // do nothing
227    }
228
229    public void DeleteHiveExperiment(Guid hiveExperimentId) {
230      // do nothing
231    }
232    #endregion
233
234    #region Login Methods
235    public void Hello(Slave slaveInfo) {
236      // do nothing
237    }
238
239    public void GoodBye(Guid id) {
240      // do nothing     
241    }
242
243    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
244      if (Messages != null && cnt < Messages.Count) {
245        return Messages[cnt++];
246      } else {
247        return new List<MessageContainer>();
248      }
249    }
250
251    public Guid GetResourceId(string resourceName) {
252      // todo
253      return Guid.Empty;
254    }
255
256    #endregion
257
258    #region Plugin Methods
259    public Guid AddPlugin(Plugin p, List<PluginData> pds) {
260      /*p.Id = Guid.NewGuid();
261      plugins.Add(p);
262      foreach (var pluginData in pds) {
263        pluginData.PluginId = p.Id;
264        pluginDatas.Add(pluginData);
265
266      }
267      return p.Id;*/
268      throw new NotImplementedException();
269
270    }
271
272    public Plugin GetPlugin(Guid pluginId) {
273      if (plugins == null)
274        plugins = ReadPluginsFromServerCache();
275      return plugins.SingleOrDefault(x => x.Id == pluginId);
276    }
277
278    public List<Plugin> GetPlugins() {
279      if (plugins == null)
280        plugins = ReadPluginsFromServerCache();
281      return plugins;
282    }
283
284    public List<PluginData> GetPluginDatas(List<Guid> pluginIds) {
285      List<PluginData> pluginDatas = new List<PluginData>();
286
287      foreach (Guid guid in pluginIds) {
288        Plugin plugin = plugins.Find(p => p.Id == guid);
289        if (plugin != null) {
290          PluginDescription desc = pDescs.First(p => p.Name == plugin.Name && p.Version == plugin.Version);
291          if (desc != null) {
292            foreach (IPluginFile file in desc.Files) {
293              PluginData data = new PluginData();
294              data.PluginId = guid;
295              data.Data = File.ReadAllBytes(file.Name);
296              data.FileName = file.Name;
297              pluginDatas.Add(data);
298            }
299          }
300        }
301      }
302      return pluginDatas;
303    }
304    #endregion
305
306    #region Slave Methods
307    public Guid AddSlave(Slave slave) {
308      throw new NotImplementedException();
309    }
310
311    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
312      throw new NotImplementedException();
313    }
314
315    public Slave GetSlave(Guid slaveId) {
316      throw new NotImplementedException();
317    }
318
319    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
320      throw new NotImplementedException();
321    }
322
323    public List<Slave> GetSlaves() {
324      throw new NotImplementedException();
325    }
326
327    public List<SlaveGroup> GetSlaveGroups() {
328      throw new NotImplementedException();
329    }
330
331    public void UpdateSlave(Slave slave) {
332      throw new NotImplementedException();
333    }
334
335    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
336      throw new NotImplementedException();
337    }
338
339    public void DeleteSlave(Guid slaveId) {
340      throw new NotImplementedException();
341    }
342
343    public void DeleteSlaveGroup(Guid slaveGroupId) {
344      throw new NotImplementedException();
345    }
346
347    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
348      throw new NotImplementedException();
349    }
350
351    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
352      throw new NotImplementedException();
353    }
354    #endregion
355
356    #region Lifecycle Methods
357    public void TriggerLifecycle(bool force) {
358      throw new NotImplementedException();
359    }
360    #endregion
361    #region Appointment Methods
362    public Guid AddAppointment(Appointment appointment) {
363      throw new NotImplementedException();
364    }
365
366    public void DeleteAppointment(Guid appointmentId) {
367      throw new NotImplementedException();
368    }
369
370    public void UpdateAppointment(Appointment appointment) {
371      throw new NotImplementedException();
372    }
373
374    public List<Appointment> GetScheduleForResource(Guid resourceId) {
375      throw new NotImplementedException();
376    }
377    #endregion
378  }
379}
Note: See TracBrowser for help on using the repository browser.