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

Last change on this file since 4302 was 4302, checked in by cneumuel, 12 years ago
  • made ServerConsole work with wsHttpBinding
  • applied role-base restrictions to all WCF-Services
  • made wcf-services work with certificates
  • renamed ExecutionEngineFacade to ClientFacade

(#1168)

File size: 7.8 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(Guid slaveId) {
54      using (contextFactory.GetContext()) {
55        return slaveCommunicator.Login(slaveId);
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        return slaveCommunicator.StoreFinishedJobResult(slaveId, jobId, result, percentage, exception);
80      }
81    }
82
83    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
84    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
85    public Response Logout(Guid slaveId) {
86      using (contextFactory.GetContext()) {
87        return slaveCommunicator.Logout(slaveId);
88      }
89    }
90
91    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
92    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
93    public Response IsJobStillNeeded(Guid jobId) {
94      using (contextFactory.GetContext()) {
95        return slaveCommunicator.IsJobStillNeeded(jobId);
96      }
97    }
98
99    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
100    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
101    public ResponseList<CachedHivePluginInfoDto> GetPlugins(List<HivePluginInfoDto> pluginList) {
102      return slaveCommunicator.GetPlugins(pluginList);     
103    }
104
105    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
106    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
107    public ResponseResultReceived ProcessSnapshot(Guid slaveId, Guid jobId, byte[] result, double percentage, string exception) {
108      using (contextFactory.GetContext()) {
109        return slaveCommunicator.ProcessSnapshot(slaveId, jobId, result, percentage, exception);
110      }
111    }
112
113    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
114    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
115    public ResponseCalendar GetCalendar(Guid slaveId) {
116      using (contextFactory.GetContext()) {
117        return slaveCommunicator.GetCalendar(slaveId);
118      }
119    }
120
121    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
122    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
123    public Response SetCalendarStatus(Guid slaveId, CalendarState state) {
124      using (contextFactory.GetContext()) {
125        return slaveCommunicator.SetCalendarStatus(slaveId, state);
126      }
127    }
128    #endregion
129
130    #region ISlaveFacade Members
131
132    /// <summary>
133    /// Do not use automatic transactions here
134    /// </summary>
135    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
136    [PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
137    public Stream GetStreamedJob(Guid slaveId) {
138      using (contextFactory.GetContext(false)) {
139        MultiStream stream = new MultiStream();
140
141        ResponseObject<JobDto> job = null;
142
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.