Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive.New/HeuristicLab.Services.Hive/3.3/HiveService.cs @ 5028

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

#1233 cleanup

File size: 9.3 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 Project Methods
166    public Project GetAllProjects() {
167      throw new NotImplementedException();
168    }
169
170    public void CreateProject(Project project) {
171      throw new NotImplementedException();
172    }
173
174    public void ChangeProject(Project project) {
175      throw new NotImplementedException();
176    }
177
178    public void DeleteProject(Guid projectId) {
179      throw new NotImplementedException();
180    }
181
182    public IEnumerable<Job> GetJobsByProject(Guid projectId) {
183      throw new NotImplementedException();
184    }
185    #endregion
186
187    #region Login Methods
188    //public void Login() {
189    //  throw new NotImplementedException();
190    //}
191
192    //public void Login(Slave slave) {
193    //  throw new NotImplementedException();
194    //}
195
196    //public void Logout(Guid clientId) {
197    //  throw new NotImplementedException();
198    //}
199    #endregion
200
201    #region Heartbeat Methods
202    public List<MessageContainer> ProcessHeartbeat(Heartbeat heartbeat) {
203      using (trans.OpenTransaction()) {
204        return hive.ProcessHeartbeat(heartbeat);
205      }
206    }
207    #endregion
208
209    #region Plugin Methods
210    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
211      throw new NotImplementedException();
212    }
213    public Stream GetStreamedPluginDatas(List<Guid> pluginIds) {
214      throw new NotImplementedException();
215    }
216    public IEnumerable<Plugin> GetAvailablePlugins() {
217      return dao.GetPlugins(x => true);
218    }
219    public Guid AddPlugin(Plugin plugin, List<PluginData> pluginDatas) {
220      using (trans.OpenTransaction()) {
221        Guid pluginId = dao.AddPlugin(plugin);
222        foreach (PluginData pluginData in pluginDatas) {
223          pluginData.PluginId = pluginId;
224          dao.AddPluginData(pluginData);
225        }
226        return pluginId;
227      }
228    }
229    #endregion
230
231    #region Calendar Methods
232    public IEnumerable<Appointment> GetCalendar(Guid clientId) {
233      throw new NotImplementedException();
234    }
235
236    public void SetCalendarStatus(Guid clientId, CalendarState state) {
237      using (trans.OpenTransaction()) {
238        throw new NotImplementedException();
239      }
240    }
241
242    public IEnumerable<Appointment> GetUptimeCalendarForResource(Guid guid) {
243      throw new NotImplementedException();
244    }
245
246    public void SetUptimeCalendarForResource(Guid guid, IEnumerable<Appointment> appointments, bool isForced) {
247      throw new NotImplementedException();
248    }
249    #endregion
250
251    #region Slave Methods
252    public Guid AddSlave(Slave slave) {
253      using (trans.OpenTransaction()) {
254        return dao.AddSlave(slave);
255      }
256    }
257
258    public Guid AddSlaveGroup(SlaveGroup slaveGroup) {
259      using (trans.OpenTransaction()) {
260        return dao.AddSlaveGroup(slaveGroup);
261      }
262    }
263
264    public IEnumerable<Slave> GetSlaves() {
265      return dao.GetSlaves(x => true);
266    }
267
268    public IEnumerable<SlaveGroup> GetSlaveGroups() {
269      return dao.GetSlaveGroups(x => true);
270    }
271
272    public void DeleteSlaveGroup(Guid slaveGroupId) {
273      using (trans.OpenTransaction()) {
274        dao.DeleteSlaveGroup(slaveGroupId);
275      }
276    }
277
278    public void AddResourceToGroup(Guid slaveGroupId, Resource resource) {
279      using (trans.OpenTransaction()) {
280        throw new NotImplementedException();
281      }
282    }
283
284    public void RemoveResourceFromGroup(Guid clientGroupId, Guid resourceId) {
285      using (trans.OpenTransaction()) {
286        throw new NotImplementedException();
287      }
288    }
289
290    public void UpdateSlave(Slave slave) {
291      using (trans.OpenTransaction()) {
292        dao.UpdateSlave(slave);
293      }
294    }
295    #endregion
296
297    #region Helper Methods
298    private IEnumerable<Job> GetChildJobs(Guid? parentJobId, bool recursive, bool includeParent) {
299      var jobs = new List<Job>(dao.GetJobs(x => parentJobId == null ? !x.ParentJobId.HasValue : x.ParentJobId.Value == parentJobId));
300
301      if (includeParent) {
302        jobs.Add(GetJob(parentJobId.Value));
303      }
304
305      if (recursive) {
306        var childs = new List<Job>();
307        foreach (var job in jobs) {
308          childs.AddRange(GetChildJobs(job.Id, recursive, false));
309        }
310        jobs.AddRange(childs);
311      }
312      return jobs;
313    }
314
315    #endregion
316  }
317}
Note: See TracBrowser for help on using the repository browser.