#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.Hive.Contracts.Interfaces;
using HeuristicLab.Hive.Contracts.BusinessObjects;
using HeuristicLab.Hive.Contracts;
using HeuristicLab.PluginInfrastructure;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.ServiceModel;
using HeuristicLab.Hive.Server.Core.InternalInterfaces;
using System.Transactions;
using HeuristicLab.Hive.Server.LINQDataAccess;
using HeuristicLab.Hive.Server.DataAccess;
using HeuristicLab.Hive.Contracts.ResponseObjects;
using System.Security.Permissions;
namespace HeuristicLab.Hive.Server.Core {
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class SlaveFacade : ISlaveFacade {
private ISlaveCommunicator slaveCommunicator = ServiceLocator.GetSlaveCommunicator();
private IContextFactory contextFactory = ServiceLocator.GetContextFactory();
public SlaveFacade() {
}
#region ISlaveCommunicator Members
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public Response Login(SlaveDto slave) {
using (contextFactory.GetContext()) {
return slaveCommunicator.Login(slave);
}
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public ResponseHeartBeat ProcessHeartBeat(HeartBeatData hbData) {
using (contextFactory.GetContext()) {
return slaveCommunicator.ProcessHeartBeat(hbData);
}
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public ResponseObject GetJob(Guid slaveId) {
using (contextFactory.GetContext()) {
return slaveCommunicator.GetJob(slaveId);
}
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public ResponseResultReceived StoreFinishedJobResult(Guid slaveId, Guid jobId, byte[] result, double percentage, string exception) {
using (contextFactory.GetContext()) {
ServiceLocator.GetAuthorizationManager().AuthorizeForJobs(jobId);
return slaveCommunicator.StoreFinishedJobResult(slaveId, jobId, result, percentage, exception);
}
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public Response Logout(Guid slaveId) {
using (contextFactory.GetContext()) {
return slaveCommunicator.Logout(slaveId);
}
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public Response IsJobStillNeeded(Guid jobId) {
using (contextFactory.GetContext()) {
ServiceLocator.GetAuthorizationManager().AuthorizeForJobs(jobId);
return slaveCommunicator.IsJobStillNeeded(jobId);
}
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public ResponseList GetPlugins(List pluginList) {
return slaveCommunicator.GetPlugins(pluginList);
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public ResponseResultReceived ProcessSnapshot(Guid slaveId, Guid jobId, byte[] result, double percentage, string exception) {
using (contextFactory.GetContext()) {
ServiceLocator.GetAuthorizationManager().AuthorizeForJobs(jobId);
return slaveCommunicator.ProcessSnapshot(slaveId, jobId, result, percentage, exception);
}
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public ResponseCalendar GetCalendar(Guid slaveId) {
using (contextFactory.GetContext()) {
return slaveCommunicator.GetCalendar(slaveId);
}
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public Response SetCalendarStatus(Guid slaveId, CalendarState state) {
using (contextFactory.GetContext()) {
return slaveCommunicator.SetCalendarStatus(slaveId, state);
}
}
#endregion
#region ISlaveFacade Members
///
/// Do not use automatic transactions here
///
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public Stream GetStreamedJob(Guid slaveId) {
using (contextFactory.GetContext(false)) {
ResponseObject job = null;
MultiStream stream = new MultiStream();
job = ServiceLocator.GetSlaveCommunicator().GetJob(slaveId);
//first send response
stream.AddStream(new StreamedObject>(job));
IInternalJobManager internalJobManager = (IInternalJobManager)ServiceLocator.GetJobManager();
//second stream the job binary data
MemoryStream memoryStream = new MemoryStream();
if (job.Obj != null) {
stream.AddStream(new MemoryStream(internalJobManager.GetSerializedJobDataById(job.Obj.Id)));
}
OperationContext slaveContext = OperationContext.Current;
slaveContext.OperationCompleted += new EventHandler(delegate(object sender, EventArgs args) {
if (stream != null) {
stream.Dispose();
}
});
return stream;
}
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public Stream GetStreamedPlugins(List pluginList) {
return new StreamedObject>(this.GetPlugins(pluginList));
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public ResponseResultReceived StoreFinishedJobResultStreamed(Stream stream) {
using (contextFactory.GetContext()) {
return ((IInternalSlaveCommunicator)slaveCommunicator).ProcessJobResult(stream, true);
}
}
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Administrator)]
[PrincipalPermission(SecurityAction.Demand, Role = HiveRoles.Slave)]
public ResponseResultReceived ProcessSnapshotStreamed(Stream stream) {
using (contextFactory.GetContext()) {
return ((IInternalSlaveCommunicator)slaveCommunicator).ProcessJobResult(stream, false);
}
}
#endregion
}
}