Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

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