Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5156 was 5156, checked in by ascheibe, 13 years ago

#1233

  • Implemented communication interface between Slave and a Slave Client using Named Pipes and callbacks
  • Added new project for testing Slave - Client communication
  • Added some copyright info headers
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, IEnumerable<Guid> slaveGroupIds) {
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              pluginDatas.Add(data);
268            }
269          }
270        }
271      }
272      return pluginDatas;
273    }
274    #endregion
275
276    #region Slave Methods
277    public Guid AddSlave(Services.Hive.Common.DataTransfer.Slave slave) {
278      throw new NotImplementedException();
279    }
280
281    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
282      throw new NotImplementedException();
283    }
284
285    public Services.Hive.Common.DataTransfer.Slave GetSlave(Guid slaveId) {
286      throw new NotImplementedException();
287    }
288
289    public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
290      throw new NotImplementedException();
291    }
292
293    public IEnumerable<Services.Hive.Common.DataTransfer.Slave> GetSlaves() {
294      throw new NotImplementedException();
295    }
296
297    public IEnumerable<SlaveGroup> GetSlaveGroups() {
298      throw new NotImplementedException();
299    }
300
301    public void UpdateSlave(Services.Hive.Common.DataTransfer.Slave slave) {
302      throw new NotImplementedException();
303    }
304
305    public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
306      throw new NotImplementedException();
307    }
308
309    public void DeleteSlave(Guid slaveId) {
310      throw new NotImplementedException();
311    }
312
313    public void DeleteSlaveGroup(Guid slaveGroupId) {
314      throw new NotImplementedException();
315    }
316
317    public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
318      throw new NotImplementedException();
319    }
320
321    public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
322      throw new NotImplementedException();
323    }
324    #endregion
325  }
326}
Note: See TracBrowser for help on using the repository browser.