Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5046 was 5038, checked in by cneumuel, 14 years ago

#1233

  • changed version to 3.4
File size: 8.8 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;
12
13namespace HeuristicLab.Services.Hive {
14
15  /// <summary>
16  /// Implementation of the Hive service (interface <see cref="IHiveService"/>).
17  /// </summary>
18  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
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 AbortJob(Guid jobId) {
116      using (trans.OpenTransaction()) {
117        throw new NotImplementedException();
118      }
119    }
120    public Job 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 Login() {
167    //  throw new NotImplementedException();
168    //}
169
170    //public void Login(Slave slave) {
171    //  throw new NotImplementedException();
172    //}
173
174    //public void Logout(Guid clientId) {
175    //  throw new NotImplementedException();
176    //}
177    #endregion
178
179    #region Heartbeat Methods
180    public List<MessageContainer> ProcessHeartbeat(Heartbeat heartbeat) {
181      using (trans.OpenTransaction()) {
182        return hive.ProcessHeartbeat(heartbeat);
183      }
184    }
185    #endregion
186
187    #region Plugin Methods
188    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
189      throw new NotImplementedException();
190    }
191    public Stream GetStreamedPluginDatas(List<Guid> pluginIds) {
192      throw new NotImplementedException();
193    }
194    public IEnumerable<Plugin> GetAvailablePlugins() {
195      return dao.GetPlugins(x => true);
196    }
197    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
198      using (trans.OpenTransaction()) {
199        Guid pluginId = dao.AddPlugin(plugin);
200        foreach (PluginData pluginData in pluginDatas) {
201          pluginData.PluginId = pluginId;
202          dao.AddPluginData(pluginData);
203        }
204        return pluginId;
205      }
206    }
207    #endregion
208
209    #region Calendar Methods
210    public IEnumerable<Appointment> GetCalendar(Guid clientId) {
211      throw new NotImplementedException();
212    }
213
214    public void SetCalendarStatus(Guid clientId, CalendarState state) {
215      using (trans.OpenTransaction()) {
216        throw new NotImplementedException();
217      }
218    }
219
220    public IEnumerable<Appointment> GetUptimeCalendarForResource(Guid guid) {
221      throw new NotImplementedException();
222    }
223
224    public void SetUptimeCalendarForResource(Guid guid, IEnumerable<Appointment> appointments, bool isForced) {
225      throw new NotImplementedException();
226    }
227    #endregion
228
229    #region Slave Methods
230    public Guid AddSlave(Slave slave) {
231      using (trans.OpenTransaction()) {
232        return dao.AddSlave(slave);
233      }
234    }
235
236    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
237      using (trans.OpenTransaction()) {
238        return dao.AddSlaveGroup(slaveGroup);
239      }
240    }
241
242    public IEnumerable<Slave> GetSlaves() {
243      return dao.GetSlaves(x => true);
244    }
245
246    public IEnumerable<SlaveGroup> GetSlaveGroups() {
247      return dao.GetSlaveGroups(x => true);
248    }
249
250    public void DeleteSlaveGroup(Guid slaveGroupId) {
251      using (trans.OpenTransaction()) {
252        dao.DeleteSlaveGroup(slaveGroupId);
253      }
254    }
255
256    public void AddResourceToGroup(Guid slaveGroupId, Resource resource) {
257      using (trans.OpenTransaction()) {
258        throw new NotImplementedException();
259      }
260    }
261
262    public void RemoveResourceFromGroup(Guid clientGroupId, Guid resourceId) {
263      using (trans.OpenTransaction()) {
264        throw new NotImplementedException();
265      }
266    }
267
268    public void UpdateSlave(Slave slave) {
269      using (trans.OpenTransaction()) {
270        dao.UpdateSlave(slave);
271      }
272    }
273    #endregion
274
275    #region Helper Methods
276    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
277      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
278
279      if (includeParent) {
280        jobs.Add(GetJob(parentJobId.Value));
281      }
282
283      if (recursive) {
284        var childs = new List<Job>();
285        foreach (var job in jobs) {
286          childs.AddRange(GetChildJobs(job.Id, recursive, false));
287        }
288        jobs.AddRange(childs);
289      }
290      return jobs;
291    }
292
293    #endregion
294  }
295}
Note: See TracBrowser for help on using the repository browser.