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 @ 4337

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

changed Slave.Core WCF-Proxy to stateless object

File size: 7.9 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        return slaveCommunicator.IsJobStillNeeded(jobId);
97      }
98    }
99
100    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
101    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
102    public ResponseList<CachedHivePluginInfoDto> GetPlugins(List<HivePluginInfoDto> pluginList) {
103      return slaveCommunicator.GetPlugins(pluginList);
104    }
105
106    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
107    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
108    public ResponseResultReceived ProcessSnapshot(Guid slaveId, Guid jobId, byte[] result, double percentage, string exception) {
109      using (contextFactory.GetContext()) {
110        ServiceLocator.GetAuthorizationManager().AuthorizeForJobs(jobId);
111        return slaveCommunicator.ProcessSnapshot(slaveId, jobId, result, percentage, exception);
112      }
113    }
114
115    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
116    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
117    public ResponseCalendar GetCalendar(Guid slaveId) {
118      using (contextFactory.GetContext()) {
119        return slaveCommunicator.GetCalendar(slaveId);
120      }
121    }
122
123    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
124    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
125    public Response SetCalendarStatus(Guid slaveId, CalendarState state) {
126      using (contextFactory.GetContext()) {
127        return slaveCommunicator.SetCalendarStatus(slaveId, state);
128      }
129    }
130    #endregion
131
132    #region ISlaveFacade Members
133
134    /// <summary>
135    /// Do not use automatic transactions here
136    /// </summary>
137    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
138    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
139    public Stream GetStreamedJob(Guid slaveId) {
140      using (contextFactory.GetContext(false)) {
141        ResponseObject<JobDto> job = null;
142        MultiStream stream = new MultiStream();
143        job = ServiceLocator.GetSlaveCommunicator().GetJob(slaveId);
144
145        //first send response
146        stream.AddStream(new StreamedObject<ResponseObject<JobDto>>(job));
147
148        IInternalJobManager internalJobManager = (IInternalJobManager)ServiceLocator.GetJobManager();
149
150        //second stream the job binary data
151        MemoryStream memoryStream = new MemoryStream();
152        if (job.Obj != null) {
153          stream.AddStream(new MemoryStream(internalJobManager.GetSerializedJobDataById(job.Obj.Id)));
154        }
155
156        OperationContext slaveContext = OperationContext.Current;
157        slaveContext.OperationCompleted += new EventHandler(delegate(object sender, EventArgs args) {
158          if (stream != null) {
159            stream.Dispose();
160          }
161        });
162
163        return stream;
164      }
165    }
166
167    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
168    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
169    public Stream GetStreamedPlugins(List<HivePluginInfoDto> pluginList) {
170      return new StreamedObject<ResponseList<CachedHivePluginInfoDto>>(this.GetPlugins(pluginList));
171    }
172
173    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
174    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
175    public ResponseResultReceived StoreFinishedJobResultStreamed(Stream stream) {
176      using (contextFactory.GetContext()) {
177        return ((IInternalSlaveCommunicator)slaveCommunicator).ProcessJobResult(stream, true);
178      }
179    }
180
181    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
182    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
183    public ResponseResultReceived ProcessSnapshotStreamed(Stream stream) {
184      using (contextFactory.GetContext()) {
185        return ((IInternalSlaveCommunicator)slaveCommunicator).ProcessJobResult(stream, false);
186      }
187    }
188    #endregion
189  }
190}
Note: See TracBrowser for help on using the repository browser.