Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server.Core/3.3/Facades/SlaveFacade.cs @ 4424

Last change on this file since 4424 was 4424, checked in by cneumuel, 14 years ago
  • Added and updated License Information in every file
  • Sort and remove usings in every file
  • Deleted obsolete DataAccess.ADOHelper
  • Deleted some obsolete files
File size: 9.9 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.Security.Permissions;
26using System.ServiceModel;
27using HeuristicLab.Hive.Contracts;
28using HeuristicLab.Hive.Contracts.BusinessObjects;
29using HeuristicLab.Hive.Contracts.Interfaces;
30using HeuristicLab.Hive.Contracts.ResponseObjects;
31using HeuristicLab.Hive.Server.Core.InternalInterfaces;
32using HeuristicLab.Hive.Server.DataAccess;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Hive.Server.Core {
36  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
37  public class SlaveFacade : ISlaveFacade {
38    private ISlaveCommunicator slaveCommunicator = ServiceLocator.GetSlaveCommunicator();
39    private IJobManager jobManager = ServiceLocator.GetJobManager();
40    private IContextFactory contextFactory = ServiceLocator.GetContextFactory();
41
42    public SlaveFacade() {
43    }
44
45    #region ISlaveCommunicator Members
46
47    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
48    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
49    public Response Login(SlaveDto slave) {
50      using (contextFactory.GetContext()) {
51        return slaveCommunicator.Login(slave);
52      }
53    }
54
55    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
56    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
57    public ResponseHeartBeat ProcessHeartBeat(HeartBeatData hbData) {
58      using (contextFactory.GetContext(false)) { // due to concurrency issues with lifecycle-heartbeat do the transaction inside
59        return slaveCommunicator.ProcessHeartBeat(hbData);
60      }
61    }
62
63    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
64    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
65    public ResponseObject<JobDto> GetJob(Guid slaveId) {
66      using (contextFactory.GetContext()) {
67        return slaveCommunicator.GetJob(slaveId);
68      }
69    }
70
71    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
72    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
73    public ResponseResultReceived StoreFinishedJobResult(Guid slaveId, Guid jobId, byte[] result, TimeSpan executionTime, string exception) {
74      using (contextFactory.GetContext()) {
75        ServiceLocator.GetAuthorizationManager().AuthorizeJobs(jobId);
76        return slaveCommunicator.StoreFinishedJobResult(slaveId, jobId, result, executionTime, exception);
77      }
78    }
79
80    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
81    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
82    public Response Logout(Guid slaveId) {
83      using (contextFactory.GetContext()) {
84        return slaveCommunicator.Logout(slaveId);
85      }
86    }
87
88    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
89    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
90    public Response IsJobStillNeeded(Guid jobId) {
91      using (contextFactory.GetContext()) {
92        return slaveCommunicator.IsJobStillNeeded(jobId);
93      }
94    }
95
96    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
97    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
98    public ResponseList<CachedHivePluginInfoDto> GetPlugins(List<HivePluginInfoDto> pluginList) {
99      return slaveCommunicator.GetPlugins(pluginList);
100    }
101
102    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
103    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
104    public ResponseResultReceived ProcessSnapshot(Guid slaveId, Guid jobId, byte[] result, TimeSpan executionTime, string exception) {
105      using (contextFactory.GetContext()) {
106        ServiceLocator.GetAuthorizationManager().AuthorizeJobs(jobId);
107        return slaveCommunicator.ProcessSnapshot(slaveId, jobId, result, executionTime, exception);
108      }
109    }
110
111    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
112    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
113    public ResponseCalendar GetCalendar(Guid slaveId) {
114      using (contextFactory.GetContext()) {
115        return slaveCommunicator.GetCalendar(slaveId);
116      }
117    }
118
119    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
120    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
121    public Response SetCalendarStatus(Guid slaveId, CalendarState state) {
122      using (contextFactory.GetContext()) {
123        return slaveCommunicator.SetCalendarStatus(slaveId, state);
124      }
125    }
126
127    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
128    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
129    public ResponseObject<JobDto> AddChildJob(Guid parentJobId, SerializedJob serializedJob) {
130      using (contextFactory.GetContext()) {
131        return jobManager.AddChildJob(parentJobId, serializedJob);
132      }
133    }
134    #endregion
135
136    #region ISlaveFacade Members
137
138    /// <summary>
139    /// Do not use automatic transactions here
140    /// </summary>
141    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
142    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
143    public Stream GetStreamedJob(Guid slaveId) {
144      ServiceLocator.GetLifecycleManager().JobsCurrentlyTransferring++;
145      using (contextFactory.GetContext(false)) {
146        ResponseObject<JobDto> job = null;
147        MultiStream stream = new MultiStream();
148        job = ServiceLocator.GetSlaveCommunicator().GetJob(slaveId);
149
150        //first send response
151        stream.AddStream(new StreamedObject<ResponseObject<JobDto>>(job));
152
153        IInternalJobManager internalJobManager = (IInternalJobManager)ServiceLocator.GetJobManager();
154
155        //second stream the job binary data
156        MemoryStream memoryStream = new MemoryStream();
157        if (job.Obj != null) {
158          stream.AddStream(new MemoryStream(internalJobManager.GetSerializedJobDataById(job.Obj.Id)));
159        }
160
161        OperationContext slaveContext = OperationContext.Current;
162        slaveContext.OperationCompleted += new EventHandler(delegate(object sender, EventArgs args) {
163          if (stream != null) {
164            stream.Dispose();
165          }
166          ServiceLocator.GetLifecycleManager().JobsCurrentlyTransferring--;
167        });
168
169        return stream;
170      }
171    }
172
173    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
174    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
175    public Stream GetStreamedPlugins(List<HivePluginInfoDto> pluginList) {
176      return new StreamedObject<ResponseList<CachedHivePluginInfoDto>>(this.GetPlugins(pluginList));
177    }
178
179    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
180    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
181    public ResponseResultReceived StoreFinishedJobResultStreamed(Stream stream) {
182      using (contextFactory.GetContext()) {
183        return ((IInternalSlaveCommunicator)slaveCommunicator).ProcessJobResult(stream, true);
184      }
185    }
186
187    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
188    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
189    public ResponseResultReceived ProcessSnapshotStreamed(Stream stream) {
190      using (contextFactory.GetContext()) {
191        return ((IInternalSlaveCommunicator)slaveCommunicator).ProcessJobResult(stream, false);
192      }
193    }
194
195    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
196    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
197    public ResponseObject<JobDto> PauseJob(SerializedJob serializedJob) {
198      using (contextFactory.GetContext()) {
199        return jobManager.PauseJob(serializedJob);
200      }
201    }
202
203    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
204    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
205    public JobResultList GetChildJobResults(Guid? parentJobId, bool recursive, bool includeParent) {
206      using (contextFactory.GetContext(false)) {
207        return jobManager.GetChildJobResults(parentJobId, recursive, includeParent).Obj;
208      }
209    }
210
211    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
212    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
213    public SerializedJob GetLastSerializedResult(Guid jobId) {
214      using (contextFactory.GetContext(false)) {
215        return jobManager.GetLastSerializedResult(jobId).Obj;
216      }
217    }
218
219    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
220    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
221    public Response DeleteChildJobs(Guid jobId) {
222      using (contextFactory.GetContext()) {
223        return jobManager.DeleteChildJobs(jobId);
224      }
225    }
226
227    #endregion
228  }
229}
Note: See TracBrowser for help on using the repository browser.