Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5526 was 5511, checked in by cneumuel, 14 years ago

#1233

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