Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4333 was 4333, checked in by cneumuel, 12 years ago

added authorizationManager which checks for permission to specific jobs (#1168)

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Linq;
25using System.Text;
26using HeuristicLab.Hive.Contracts.Interfaces;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.Hive.Contracts;
29using HeuristicLab.PluginInfrastructure;
30using System.IO;
31using System.Runtime.Serialization.Formatters.Binary;
32using System.ServiceModel;
33using HeuristicLab.Hive.Server.Core.InternalInterfaces;
34using System.Transactions;
35using HeuristicLab.Hive.Server.LINQDataAccess;
36using HeuristicLab.Hive.Server.DataAccess;
37using HeuristicLab.Hive.Contracts.ResponseObjects;
38using System.Security.Permissions;
39
40namespace HeuristicLab.Hive.Server.Core {
41  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
42  public class SlaveFacade : ISlaveFacade {
43    private ISlaveCommunicator slaveCommunicator = ServiceLocator.GetSlaveCommunicator();
44    private IContextFactory contextFactory = ServiceLocator.GetContextFactory();
45
46    public SlaveFacade() {
47    }
48
49    #region ISlaveCommunicator Members
50
51    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
52    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
53    public Response Login(SlaveDto slave) {
54      using (contextFactory.GetContext()) {
55        return slaveCommunicator.Login(slave);
56      }
57    }
58
59    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
60    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
61    public ResponseHeartBeat ProcessHeartBeat(HeartBeatData hbData) {
62      using (contextFactory.GetContext()) {
63        return slaveCommunicator.ProcessHeartBeat(hbData);
64      }
65    }
66
67    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
68    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
69    public ResponseObject<JobDto> GetJob(Guid slaveId) {
70      using (contextFactory.GetContext()) {
71        return slaveCommunicator.GetJob(slaveId);
72      }
73    }
74
75    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
76    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
77    public ResponseResultReceived StoreFinishedJobResult(Guid slaveId, Guid jobId, byte[] result, double percentage, string exception) {
78      using (contextFactory.GetContext()) {
79        ServiceLocator.GetAuthorizationManager().AuthorizeForJobs(jobId);
80        return slaveCommunicator.StoreFinishedJobResult(slaveId, jobId, result, percentage, exception);
81      }
82    }
83
84    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
85    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
86    public Response Logout(Guid slaveId) {
87      using (contextFactory.GetContext()) {
88        return slaveCommunicator.Logout(slaveId);
89      }
90    }
91
92    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
93    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
94    public Response IsJobStillNeeded(Guid jobId) {
95      using (contextFactory.GetContext()) {
96        ServiceLocator.GetAuthorizationManager().AuthorizeForJobs(jobId);
97        return slaveCommunicator.IsJobStillNeeded(jobId);
98      }
99    }
100
101    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
102    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
103    public ResponseList<CachedHivePluginInfoDto> GetPlugins(List<HivePluginInfoDto> pluginList) {
104      return slaveCommunicator.GetPlugins(pluginList);
105    }
106
107    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
108    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
109    public ResponseResultReceived ProcessSnapshot(Guid slaveId, Guid jobId, byte[] result, double percentage, string exception) {
110      using (contextFactory.GetContext()) {
111        ServiceLocator.GetAuthorizationManager().AuthorizeForJobs(jobId);
112        return slaveCommunicator.ProcessSnapshot(slaveId, jobId, result, percentage, exception);
113      }
114    }
115
116    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
117    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
118    public ResponseCalendar GetCalendar(Guid slaveId) {
119      using (contextFactory.GetContext()) {
120        return slaveCommunicator.GetCalendar(slaveId);
121      }
122    }
123
124    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
125    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
126    public Response SetCalendarStatus(Guid slaveId, CalendarState state) {
127      using (contextFactory.GetContext()) {
128        return slaveCommunicator.SetCalendarStatus(slaveId, state);
129      }
130    }
131    #endregion
132
133    #region ISlaveFacade Members
134
135    /// <summary>
136    /// Do not use automatic transactions here
137    /// </summary>
138    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
139    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
140    public Stream GetStreamedJob(Guid slaveId) {
141      using (contextFactory.GetContext(false)) {
142        ResponseObject<JobDto> job = null;
143        MultiStream stream = new MultiStream();
144        job = ServiceLocator.GetSlaveCommunicator().GetJob(slaveId);
145
146        //first send response
147        stream.AddStream(new StreamedObject<ResponseObject<JobDto>>(job));
148
149        IInternalJobManager internalJobManager = (IInternalJobManager)ServiceLocator.GetJobManager();
150
151        //second stream the job binary data
152        MemoryStream memoryStream = new MemoryStream();
153        if (job.Obj != null) {
154          stream.AddStream(new MemoryStream(internalJobManager.GetSerializedJobDataById(job.Obj.Id)));
155        }
156
157        OperationContext slaveContext = OperationContext.Current;
158        slaveContext.OperationCompleted += new EventHandler(delegate(object sender, EventArgs args) {
159          if (stream != null) {
160            stream.Dispose();
161          }
162        });
163
164        return stream;
165      }
166    }
167
168    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
169    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
170    public Stream GetStreamedPlugins(List<HivePluginInfoDto> pluginList) {
171      return new StreamedObject<ResponseList<CachedHivePluginInfoDto>>(this.GetPlugins(pluginList));
172    }
173
174    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
175    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
176    public ResponseResultReceived StoreFinishedJobResultStreamed(Stream stream) {
177      using (contextFactory.GetContext()) {
178        return ((IInternalSlaveCommunicator)slaveCommunicator).ProcessJobResult(stream, true);
179      }
180    }
181
182    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
183    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
184    public ResponseResultReceived ProcessSnapshotStreamed(Stream stream) {
185      using (contextFactory.GetContext()) {
186        return ((IInternalSlaveCommunicator)slaveCommunicator).ProcessJobResult(stream, false);
187      }
188    }
189    #endregion
190  }
191}
Note: See TracBrowser for help on using the repository browser.