Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5306 was 5266, checked in by ascheibe, 14 years ago

#1233 added FileName to PluginData

File size: 11.1 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, JobData jobDataDto) {
157      Console.WriteLine("Update Job called!");
158      ResultJobDatas.Add(jobDataDto);
159      ResultJobs.Add(jobDto);
160    }
161
162    public void DeleteJob(Guid jobId) {
163      // do nothing
164    }
165
166    public void DeleteChildJobs(Guid parentJobId) {
167      // do nothing
168    }
169    #endregion
170
171    #region JobControl Methods
172    int curJobIdx = 0;
173    public Job AquireJob(Guid slaveId) {
174      if (curJobIdx < jobs.Count) {
175        var job = jobs[curJobIdx];
176        job.SlaveId = slaveId;
177        curJobIdx++;
178        return job;
179      }
180      throw new Exception("No Jobs left on MockHiveService!");
181    }
182
183    public void StopJob(Guid jobId) {
184      // do nothing
185    }
186
187    public void PauseJob(Guid jobId) {
188      // do nothing
189    }
190    #endregion
191
192    #region HiveExperiment Methods
193    public HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment GetHiveExperiment(Guid id) {
194      return hiveExperiments.Where(he => he.Id == id).SingleOrDefault();
195    }
196
197    public IEnumerable<HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment> GetHiveExperiments() {
198      return hiveExperiments;
199    }
200
201    public Guid AddHiveExperiment(HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment hiveExperimentDto) {
202      hiveExperimentDto.Id = Guid.NewGuid();
203      hiveExperiments.Add(hiveExperimentDto);
204      return hiveExperimentDto.Id;
205    }
206
207    public void UpdateHiveExperiment(HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment hiveExperimentDto) {
208      // do nothing
209    }
210
211    public void DeleteHiveExperiment(Guid hiveExperimentId) {
212      // do nothing
213    }
214    #endregion
215
216    #region Login Methods
217    public void Hello(Guid slaveId, string name, int cores, int memory) {
218      // do nothing
219    }
220
221    public void GoodBye() {
222      // do nothing
223    }
224
225    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
226      if (Messages != null && cnt < Messages.Count) {
227        return Messages[cnt++];
228      } else {
229        return new List<MessageContainer>();
230      }
231    }
232
233    #endregion
234
235    #region Plugin Methods
236    public Guid AddPlugin(Plugin p, List<PluginData> pds) {
237      /*p.Id = Guid.NewGuid();
238      plugins.Add(p);
239      foreach (var pluginData in pds) {
240        pluginData.PluginId = p.Id;
241        pluginDatas.Add(pluginData);
242
243      }
244      return p.Id;*/
245      throw new NotImplementedException();
246
247    }
248
249    public IEnumerable<Plugin> GetPlugins() {
250      if (plugins == null)
251        plugins = ReadPluginsFromServerCache();
252      return plugins;
253    }
254
255    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
256      List<PluginData> pluginDatas = new List<PluginData>();
257
258      foreach (Guid guid in pluginIds) {
259        Plugin plugin = plugins.Find(p => p.Id == guid);
260        if (plugin != null) {
261          PluginDescription desc = pDescs.First(p => p.Name == plugin.Name && p.Version == plugin.Version);
262          if (desc != null) {
263            foreach (IPluginFile file in desc.Files) {
264              PluginData data = new PluginData();
265              data.PluginId = guid;
266              data.Data = File.ReadAllBytes(file.Name);
267              data.FileName = file.Name;
268              pluginDatas.Add(data);
269            }
270          }
271        }
272      }
273      return pluginDatas;
274    }
275    #endregion
276
277    #region Slave Methods
278    public Guid AddSlave(Services.Hive.Common.DataTransfer.Slave slave) {
279      throw new NotImplementedException();
280    }
281
282    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
283      throw new NotImplementedException();
284    }
285
286    public Services.Hive.Common.DataTransfer.Slave GetSlave(Guid slaveId) {
287      throw new NotImplementedException();
288    }
289
290    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
291      throw new NotImplementedException();
292    }
293
294    public IEnumerable<Services.Hive.Common.DataTransfer.Slave> GetSlaves() {
295      throw new NotImplementedException();
296    }
297
298    public IEnumerable<SlaveGroup> GetSlaveGroups() {
299      throw new NotImplementedException();
300    }
301
302    public void UpdateSlave(Services.Hive.Common.DataTransfer.Slave slave) {
303      throw new NotImplementedException();
304    }
305
306    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
307      throw new NotImplementedException();
308    }
309
310    public void DeleteSlave(Guid slaveId) {
311      throw new NotImplementedException();
312    }
313
314    public void DeleteSlaveGroup(Guid slaveGroupId) {
315      throw new NotImplementedException();
316    }
317
318    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
319      throw new NotImplementedException();
320    }
321
322    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
323      throw new NotImplementedException();
324    }
325    #endregion
326  }
327}
Note: See TracBrowser for help on using the repository browser.