#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using HeuristicLab.PluginInfrastructure;
using HeuristicLab.PluginInfrastructure.Manager;
using HeuristicLab.Services.Hive.Common;
using HeuristicLab.Services.Hive.Common.DataTransfer;
using HeuristicLab.Services.Hive.Common.ServiceContracts;
namespace HeuristicLab.Clients.Hive.Slave.Tests {
internal class MockHiveService : IHiveService {
private static List jobs;
private static List jobDatas;
private static List hiveExperiments;
private static List plugins = null;
public List ResultJobs { get; set; }
public List ResultJobDatas { get; set; }
private static IEnumerable pDescs = null;
private static int cnt = 0;
public List> Messages { get; set; }
public static string ServerPluginCachePath { get; set; }
public static string ServerConfigFilePath { get; set; }
static MockHiveService() {
ServerPluginCachePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PluginCacheServer");
ServerConfigFilePath = Path.Combine(ServerPluginCachePath, "HeuristicLab 3.3.exe.config");
if (plugins == null)
plugins = ReadPluginsFromServerCache();
//TODO: reuse as fallback?
/*jobs = new List();
jobs.Add(new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 });
jobs.Add(new Job { Id = Guid.NewGuid(), JobState = JobState.Offline, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 100, ParentJobId = jobs.First().Id });
jobs.Add(new Job { Id = Guid.NewGuid(), JobState = JobState.Offline, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 1024, ParentJobId = jobs.First().Id });
jobs.Add(new Job { Id = Guid.NewGuid(), JobState = JobState.Offline, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 4096, ParentJobId = jobs.First().Id });
byte[] data = PersistenceUtil.Serialize(new MockJob(400, false));
jobDatas = new List();
foreach (var job in jobs) {
job.PluginsNeededIds = new List();
job.PluginsNeededIds.AddRange(plugins.Select(a => a.Id));
jobDatas.Add(new JobData() { JobId = job.Id, Data = data });
}
hiveExperiments = new List();
hiveExperiments.Add(new HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment() { Id = Guid.NewGuid(), Name = "Hive Exp 1", Description = "", RootJobId = jobs[0].Id });*/
}
public void updateJobs(List js, MockJob m) {
jobs = js;
byte[] data = PersistenceUtil.Serialize(m);
jobDatas = new List();
foreach (var job in jobs) {
job.PluginsNeededIds = new List();
job.PluginsNeededIds.AddRange(plugins.Select(a => a.Id));
jobDatas.Add(new JobData() { JobId = job.Id, Data = data });
}
hiveExperiments = new List();
hiveExperiments.Add(new HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment() { Id = Guid.NewGuid(), Name = "Hive Exp 1", Description = "", RootJobId = jobs[0].Id });
ResultJobDatas = new List();
ResultJobs = new List();
}
private static List ReadPluginsFromServerCache() {
PluginManager pm = new PluginManager(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PluginCacheServer"));
pm.DiscoverAndCheckPlugins();
pDescs = pm.Plugins;
plugins = new List();
foreach (PluginDescription d in pDescs) {
Plugin p = new Plugin();
p.Name = d.Name;
p.Version = d.Version;
p.Id = Guid.NewGuid();
plugins.Add(p);
}
return plugins;
}
#region Job Methods
public Guid AddJob(Job job, JobData jobData, IEnumerable slaveGroupIds) {
job.Id = Guid.NewGuid();
jobData.JobId = job.Id;
jobs.Add(job);
jobDatas.Add(jobData);
return job.Id;
}
public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
job.Id = Guid.NewGuid();
job.ParentJobId = parentJobId;
jobData.JobId = job.Id;
jobs.Add(job);
jobDatas.Add(jobData);
return job.Id;
}
public PluginData GetConfigurationFile() {
byte[] cf = File.ReadAllBytes(ServerConfigFilePath);
PluginData pd = new PluginData();
pd.Data = cf;
return pd;
}
public Job GetJob(Guid jobId) {
//MockHBM.SendHeartbeat();
return jobs.Where(j => j.Id == jobId).SingleOrDefault();
}
public IEnumerable GetJobs() {
return jobs;
}
public IEnumerable GetLightweightJobs(IEnumerable jobIds) {
return jobs.Select(j => new LightweightJob(j)); // not real
}
public IEnumerable GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
return jobs.Where(j => j.ParentJobId.HasValue).Select(j => new LightweightJob(j));
}
public JobData GetJobData(Guid jobId) {
JobData ret = jobDatas.Where(jd => jd.JobId == jobId).SingleOrDefault();
//MockHBM.SendHeartbeat();
return ret;
}
public void UpdateJob(Job jobDto) {
Console.WriteLine("Update Job called!");
ResultJobs.Add(jobDto);
}
public void UpdateJobData(Job jobDto, JobData jobDataDto) {
Console.WriteLine("Update JobData called!");
ResultJobDatas.Add(jobDataDto);
ResultJobs.Add(jobDto);
}
public void DeleteJob(Guid jobId) {
// do nothing
}
public void DeleteChildJobs(Guid parentJobId) {
// do nothing
}
#endregion
#region JobControl Methods
int curJobIdx = 0;
public Job AquireJob(Guid slaveId) {
if (curJobIdx < jobs.Count) {
var job = jobs[curJobIdx];
// job.SlaveId = slaveId; // commented out because of change to StateLog
curJobIdx++;
return job;
}
throw new Exception("No Jobs left on MockHiveService!");
}
public void StopJob(Guid jobId) {
// do nothing
}
public void PauseJob(Guid jobId) {
// do nothing
}
#endregion
#region HiveExperiment Methods
public HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment GetHiveExperiment(Guid id) {
return hiveExperiments.Where(he => he.Id == id).SingleOrDefault();
}
public IEnumerable GetHiveExperiments() {
return hiveExperiments;
}
public Guid AddHiveExperiment(HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment hiveExperimentDto) {
hiveExperimentDto.Id = Guid.NewGuid();
hiveExperiments.Add(hiveExperimentDto);
return hiveExperimentDto.Id;
}
public void UpdateHiveExperiment(HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment hiveExperimentDto) {
// do nothing
}
public void DeleteHiveExperiment(Guid hiveExperimentId) {
// do nothing
}
#endregion
#region Login Methods
public void Hello(HeuristicLab.Services.Hive.Common.DataTransfer.Slave slaveInfo) {
// do nothing
}
public void GoodBye(Guid id) {
// do nothing
}
public List Heartbeat(Heartbeat heartbeat) {
if (Messages != null && cnt < Messages.Count) {
return Messages[cnt++];
} else {
return new List();
}
}
public Guid GetResourceId(string resourceName) {
// todo
return Guid.Empty;
}
#endregion
#region Plugin Methods
public Guid AddPlugin(Plugin p, List pds) {
/*p.Id = Guid.NewGuid();
plugins.Add(p);
foreach (var pluginData in pds) {
pluginData.PluginId = p.Id;
pluginDatas.Add(pluginData);
}
return p.Id;*/
throw new NotImplementedException();
}
public IEnumerable GetPlugins() {
if (plugins == null)
plugins = ReadPluginsFromServerCache();
return plugins;
}
public IEnumerable GetPluginDatas(List pluginIds) {
List pluginDatas = new List();
foreach (Guid guid in pluginIds) {
Plugin plugin = plugins.Find(p => p.Id == guid);
if (plugin != null) {
PluginDescription desc = pDescs.First(p => p.Name == plugin.Name && p.Version == plugin.Version);
if (desc != null) {
foreach (IPluginFile file in desc.Files) {
PluginData data = new PluginData();
data.PluginId = guid;
data.Data = File.ReadAllBytes(file.Name);
data.FileName = file.Name;
pluginDatas.Add(data);
}
}
}
}
return pluginDatas;
}
#endregion
#region Slave Methods
public Guid AddSlave(Services.Hive.Common.DataTransfer.Slave slave) {
throw new NotImplementedException();
}
public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
throw new NotImplementedException();
}
public Services.Hive.Common.DataTransfer.Slave GetSlave(Guid slaveId) {
throw new NotImplementedException();
}
public SlaveGroup GetSlaveGroup(Guid slaveGroupId) {
throw new NotImplementedException();
}
public IEnumerable GetSlaves() {
throw new NotImplementedException();
}
public IEnumerable GetSlaveGroups() {
throw new NotImplementedException();
}
public void UpdateSlave(Services.Hive.Common.DataTransfer.Slave slave) {
throw new NotImplementedException();
}
public void UpdateSlaveGroup(SlaveGroup slaveGroup) {
throw new NotImplementedException();
}
public void DeleteSlave(Guid slaveId) {
throw new NotImplementedException();
}
public void DeleteSlaveGroup(Guid slaveGroupId) {
throw new NotImplementedException();
}
public void AddResourceToGroup(Guid slaveGroupId, Guid resourceId) {
throw new NotImplementedException();
}
public void RemoveResourceFromGroup(Guid slaveGroupId, Guid resourceId) {
throw new NotImplementedException();
}
#endregion
}
}