Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive/3.4/HiveService.cs @ 5078

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

#1233

  • removed dash from solution file name
  • reorganized MessageContainer.MessageTypes
  • added server test project
File size: 8.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ServiceModel;
6using HeuristicLab.Services.Hive.Common.ServiceContracts;
7using HeuristicLab.Services.Hive.Common.DataTransfer;
8using System.IO;
9using System.Security.Permissions;
10using System.Data.Linq;
11using HeuristicLab.Services.Hive.Common;
12using System.Transactions;
13
14namespace HeuristicLab.Services.Hive {
15
16  /// <summary>
17  /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
18  /// </summary>
19  public class HiveService : IHiveService {
20    private DataAccess.IHiveDao dao {
21      get { return ServiceLocator.Instance.HiveDao; }
22    }
23    private HeuristicLab.Services.Hive.DataAccess.TransactionManager trans {
24      get { return ServiceLocator.Instance.TransactionManager; }
25    }
26    private IAuthorizationManager auth {
27      get { return ServiceLocator.Instance.AuthorizationManager; }
28    }
29    private Hive hive {
30      get { return ServiceLocator.Instance.Hive; }
31    }
32
33    #region Job Methods
34    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
35    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Client)]
36    public Guid AddJob(Job job, JobData jobData) {
37      using (trans.OpenTransaction()) {
38        jobData.JobId = dao.AddJob(job);
39        dao.AddJobData(jobData);
40        return jobData.JobId;
41      }
42    }
43
44    public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
45      using (trans.OpenTransaction()) {
46        job.ParentJobId = parentJobId;
47        return AddJob(job, jobData);
48      }
49    }
50
51    public Job GetJob(Guid jobId) {
52      return dao.GetJob(jobId);
53    }
54
55    public IEnumerable<Job> GetJobs() {
56      return dao.GetJobs(x => true);
57    }
58
59    public IEnumerable<LightweightJob> GetLightweightJobs(IEnumerable<Guid> jobIds) {
60      return dao.GetJobs(x => jobIds.Contains(x.JobId)).Select(x => new LightweightJob(x)).ToArray();
61    }
62
63    public IEnumerable<LightweightJob> GetLightweightChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
64      return GetChildJobs(parentJobId, recursive, includeParent).Select(x => new LightweightJob(x)).ToArray();
65    }
66
67    // formerly GetLastSerializedResult
68    public JobData GetJobData(Guid jobId) {
69      return dao.GetJobData(jobId);
70    }
71
72    public Stream GetJobDataStreamed(Guid jobId) {
73      throw new NotImplementedException();
74    }
75
76    public void UpdateJob(Job jobDto, JobData jobDataDto) {
77      using (trans.OpenTransaction()) {
78        dao.UpdateJob(jobDto);
79        dao.UpdateJobData(jobDataDto);
80      }
81    }
82
83    public void UpdateJobDataStreamed(Stream stream) {
84      using (trans.OpenTransaction()) {
85        throw new NotImplementedException();
86      }
87    }
88
89    public void DeleteChildJobs(Guid jobId) {
90      using (trans.OpenTransaction()) {
91        var jobs = GetChildJobs(jobId, true, false);
92        foreach (var job in jobs) {
93          dao.DeleteJob(job.Id);
94          dao.DeleteJobData(job.Id);
95        };
96      }
97    }
98
99    public Job AquireJob(Guid slaveId) {
100      using (trans.OpenTransaction()) {
101        var slave = dao.GetSlave(slaveId);
102        var availableJobs = dao.GetWaitingJobs(slave);
103        var job = availableJobs.FirstOrDefault();
104
105        if (job != null) {
106          job.SlaveId = slaveId;
107          job.JobState = JobState.Calculating;
108        }
109        return job;
110      }
111    }
112    #endregion
113
114    #region Job Control Methods
115    public void StopJob(Guid jobId) {
116      using (trans.OpenTransaction()) {
117        throw new NotImplementedException();
118      }
119    }
120    public void PauseJob(Guid jobId) {
121      using (trans.OpenTransaction()) {
122        throw new NotImplementedException();
123      }
124    }
125    #endregion
126
127    #region HiveExperiment Methods
128
129    public HiveExperiment GetHiveExperiment(Guid id) {
130      return dao.GetHiveExperiments(x => x.UserId == auth.UserId && x.HiveExperimentId == id).FirstOrDefault();
131    }
132
133    public IEnumerable<HiveExperiment> GetHiveExperiments() {
134      return dao.GetHiveExperiments(x => x.UserId == auth.UserId);
135    }
136
137    public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
138      using (trans.OpenTransaction()) {
139        hiveExperimentDto.UserId = auth.UserId;
140        return dao.AddHiveExperiment(hiveExperimentDto);
141      }
142    }
143
144    public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
145      using (trans.OpenTransaction()) {
146        dao.UpdateHiveExperiment(hiveExperimentDto);
147      }
148    }
149
150    public void DeleteHiveExperiment(Guid hiveExperimentId) {
151      using (trans.OpenTransaction()) {
152        HiveExperiment he = dao.GetHiveExperiment(hiveExperimentId);
153        if (he.RootJobId.HasValue) {
154          var jobs = GetChildJobs(he.RootJobId.Value, true, true);
155          foreach (var j in jobs) {
156            dao.DeleteJobData(j.Id);
157            dao.DeleteJob(j.Id);
158          }
159        }
160        dao.DeleteHiveExperiment(hiveExperimentId);
161      }
162    }
163    #endregion
164   
165    #region Login Methods
166    public void Hello(Guid slaveId, string name, int cores, int memory) {
167      throw new NotImplementedException();
168    }
169
170    public void GoodBye() {
171      throw new NotImplementedException();
172    }
173    #endregion
174
175    #region Heartbeat Methods
176    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
177      using (trans.OpenTransaction()) {
178        return hive.ProcessHeartbeat(heartbeat);
179      }
180    }
181    #endregion
182
183    #region Plugin Methods
184    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
185      using (trans.OpenTransaction()) {
186        Guid pluginId = dao.AddPlugin(plugin);
187        foreach (PluginData pluginData in pluginDatas) {
188          pluginData.PluginId = pluginId;
189          dao.AddPluginData(pluginData);
190        }
191        return pluginId;
192      }
193    }
194    public IEnumerable<Plugin> GetPlugins() {
195      return dao.GetPlugins(x => true);
196    }
197    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
198      throw new NotImplementedException();
199    }
200    public Stream GetStreamedPluginDatas(List<Guid> pluginIds) {
201      throw new NotImplementedException();
202    }
203    #endregion
204   
205    #region Slave Methods
206    public Guid AddSlave(Slave slave) {
207      using (trans.OpenTransaction()) {
208        return dao.AddSlave(slave);
209      }
210    }
211
212    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
213      using (trans.OpenTransaction()) {
214        return dao.AddSlaveGroup(slaveGroup);
215      }
216    }
217
218    public IEnumerable<Slave> GetSlaves() {
219      return dao.GetSlaves(x => true);
220    }
221
222    public IEnumerable<SlaveGroup> GetSlaveGroups() {
223      return dao.GetSlaveGroups(x => true);
224    }
225
226    public void DeleteSlaveGroup(Guid slaveGroupId) {
227      using (trans.OpenTransaction()) {
228        dao.DeleteSlaveGroup(slaveGroupId);
229      }
230    }
231
232    public void AddResourceToGroup(Guid slaveGroupId, Resource resource) {
233      using (trans.OpenTransaction()) {
234        throw new NotImplementedException();
235      }
236    }
237
238    public void RemoveResourceFromGroup(Guid clientGroupId, Guid resourceId) {
239      using (trans.OpenTransaction()) {
240        throw new NotImplementedException();
241      }
242    }
243
244    public void UpdateSlave(Slave slave) {
245      using (trans.OpenTransaction()) {
246        dao.UpdateSlave(slave);
247      }
248    }
249    #endregion
250
251    #region Helper Methods
252    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
253      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
254
255      if (includeParent) {
256        jobs.Add(GetJob(parentJobId.Value));
257      }
258
259      if (recursive) {
260        var childs = new List<Job>();
261        foreach (var job in jobs) {
262          childs.AddRange(GetChildJobs(job.Id, recursive, false));
263        }
264        jobs.AddRange(childs);
265      }
266      return jobs;
267    }
268
269    #endregion
270
271  }
272}
Note: See TracBrowser for help on using the repository browser.