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