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

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

Rename "Client" to "Slave" #1157

File size: 5.6 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;
37
38namespace HeuristicLab.Hive.Server.Core {
39  [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
40  public class SlaveFacade : ISlaveFacade {
41
42    public SlaveFacade() {
43    }
44
45    private ISlaveCommunicator slaveCommunicator = ServiceLocator.GetSlaveCommunicator();
46
47    private IContextFactory contextFactory = ServiceLocator.GetContextFactory();
48
49    #region ISlaveCommunicator Members
50
51    public Response Login(ClientDto clientInfo) {
52      using (contextFactory.GetContext()) {
53        return slaveCommunicator.Login(clientInfo);
54      }
55    }
56
57    public ResponseHB ProcessHeartBeat(HeartBeatData hbData) {
58      using (contextFactory.GetContext()) {
59        return slaveCommunicator.ProcessHeartBeat(hbData);
60      }
61    }
62
63    public ResponseJob SendJob(Guid clientId) {
64      using (contextFactory.GetContext()) {
65        return slaveCommunicator.SendJob(clientId);
66      }
67    }
68
69    public ResponseResultReceived StoreFinishedJobResult(Guid clientId, Guid jobId, byte[] result, double percentage, string exception) {
70      using (contextFactory.GetContext()) {
71        return slaveCommunicator.StoreFinishedJobResult(clientId, jobId, result, percentage, exception);
72      }
73    }
74
75    public Response Logout(Guid clientId) {
76      using (contextFactory.GetContext()) {
77        return slaveCommunicator.Logout(clientId);
78      }
79    }
80
81    public Response IsJobStillNeeded(Guid jobId) {
82      using (contextFactory.GetContext()) {
83        return slaveCommunicator.IsJobStillNeeded(jobId);
84      }
85    }
86
87    public ResponsePlugin SendPlugins(List<HivePluginInfoDto> pluginList) {
88      return slaveCommunicator.SendPlugins(pluginList);     
89    }
90
91    public ResponseResultReceived ProcessSnapshot(Guid clientId, Guid jobId, byte[] result, double percentage, string exception) {
92      using (contextFactory.GetContext()) {
93        return slaveCommunicator.ProcessSnapshot(clientId, jobId, result, percentage, exception);
94      }
95    }
96
97
98    public ResponseCalendar GetCalendar(Guid clientId) {
99      using (contextFactory.GetContext()) {
100        return slaveCommunicator.GetCalendar(clientId);
101      }
102    }
103
104    public Response SetCalendarStatus(Guid clientId, CalendarState state) {
105      using (contextFactory.GetContext()) {
106        return slaveCommunicator.SetCalendarStatus(clientId, state);
107      }
108    }
109    #endregion
110
111    #region IClientFacade Members
112
113    /// <summary>
114    /// Do not use automatic transactions here
115    /// </summary>
116    public Stream SendStreamedJob(Guid clientId) {
117      using (contextFactory.GetContext(false)) {
118        MultiStream stream = new MultiStream();
119
120        ResponseJob job = null;
121
122        job = ServiceLocator.GetSlaveCommunicator().SendJob(clientId);
123
124        //first send response
125        stream.AddStream(new StreamedObject<ResponseJob>(job));
126
127        IJobManager jobManager = ServiceLocator.GetJobManager();
128        IInternalJobManager internalJobManager = (IInternalJobManager)jobManager;
129
130        //second stream the job binary data
131        MemoryStream memoryStream = new MemoryStream();
132        if (job.Job != null)
133          stream.AddStream(new MemoryStream(internalJobManager.GetSerializedJobDataById(job.Job.Id)));
134
135        OperationContext clientContext = OperationContext.Current;
136        clientContext.OperationCompleted += new EventHandler(delegate(object sender, EventArgs args) {
137          if (stream != null) {
138            stream.Dispose();
139          }
140        });
141
142        return stream;
143      }
144    }
145
146    public Stream SendStreamedPlugins(List<HivePluginInfoDto> pluginList) {
147      return new StreamedObject<ResponsePlugin>(this.SendPlugins(pluginList));
148    }
149
150    public ResponseResultReceived StoreFinishedJobResultStreamed(Stream stream) {
151      using (contextFactory.GetContext()) {
152        return ((IInternalSlaveCommunicator)slaveCommunicator).ProcessJobResult(stream, true);
153      }
154    }
155
156    public ResponseResultReceived ProcessSnapshotStreamed(Stream stream) {
157      using (contextFactory.GetContext()) {
158        return ((IInternalSlaveCommunicator)slaveCommunicator).ProcessJobResult(stream, false); 
159      }
160    }
161    #endregion
162  }
163}
Note: See TracBrowser for help on using the repository browser.