[5105] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Clients.Common;
|
---|
| 26 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
| 27 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
| 28 | using HeuristicLab.Services.Hive.Common;
|
---|
| 29 | using HeuristicLab.Clients.Hive.Slave.Properties;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Clients.Hive.Salve {
|
---|
| 32 |
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// WcfService class is implemented as a Singleton and works as a communication Layer with the Server
|
---|
| 35 | /// </summary>
|
---|
| 36 | public class WcfService : MarshalByRefObject {
|
---|
| 37 | private static WcfService instance;
|
---|
| 38 | /// <summary>
|
---|
| 39 | /// Getter for the Instance of the WcfService
|
---|
| 40 | /// </summary>
|
---|
| 41 | /// <returns>the Instance of the WcfService class</returns>
|
---|
| 42 | public static WcfService Instance {
|
---|
| 43 | get {
|
---|
| 44 | if (instance == null) {
|
---|
| 45 | Logger.Debug("New WcfService Instance created");
|
---|
| 46 | instance = new WcfService();
|
---|
| 47 | }
|
---|
| 48 | return instance;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public DateTime ConnectedSince { get; private set; }
|
---|
| 53 | public NetworkEnum.WcfConnState ConnState { get; private set; }
|
---|
| 54 |
|
---|
| 55 | public event EventHandler Connected;
|
---|
| 56 | public void OnConnected() {
|
---|
| 57 | var handler = Connected;
|
---|
| 58 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /// <summary>
|
---|
| 62 | /// Constructor
|
---|
| 63 | /// </summary>
|
---|
| 64 | private WcfService() {
|
---|
| 65 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /// <summary>
|
---|
| 69 | /// Connects with the Server, registers the events and fires the Connected (and quiet possibly the ConnectionRestored) Event.
|
---|
| 70 | /// </summary>
|
---|
| 71 | public void Connect(HeuristicLab.Services.Hive.Common.DataTransfer.Slave slaveInfo) {
|
---|
| 72 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 73 | try {
|
---|
| 74 | Logger.Debug("Starting the Connection Process");
|
---|
| 75 | ConnState = NetworkEnum.WcfConnState.Connected;
|
---|
| 76 | ConnectedSince = DateTime.Now;
|
---|
| 77 | service.Obj.Hello(Settings.Default.Guid, slaveInfo.Name, slaveInfo.Cores.Value, slaveInfo.Memory.Value);
|
---|
| 78 | OnConnected();
|
---|
| 79 | }
|
---|
| 80 | catch (Exception ex) {
|
---|
| 81 | HandleNetworkError(ex);
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /// <summary>
|
---|
| 87 | /// Disconnects the Slave from the Server
|
---|
| 88 | /// </summary>
|
---|
| 89 | public void Disconnect() {
|
---|
| 90 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 91 | try {
|
---|
| 92 | service.Obj.GoodBye();
|
---|
| 93 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
| 94 | }
|
---|
| 95 | catch (Exception ex) {
|
---|
| 96 | HandleNetworkError(ex);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /// <summary>
|
---|
| 102 | /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
|
---|
| 103 | /// </summary>
|
---|
| 104 | /// <param name="e">The Exception</param>
|
---|
| 105 | private void HandleNetworkError(Exception e) {
|
---|
| 106 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
| 107 | Logger.Error("Network exception occurred: " + e);
|
---|
| 108 | OnExceptionOccured(e);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /// <summary>
|
---|
| 112 | /// Aquire a Job from the Server, return the Job
|
---|
| 113 | /// </summary>
|
---|
| 114 | public Job AquireJob() {
|
---|
| 115 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 116 | try {
|
---|
| 117 | Job job = service.Obj.AquireJob(Settings.Default.Guid);
|
---|
| 118 | return job;
|
---|
| 119 | }
|
---|
| 120 | catch (Exception ex) {
|
---|
| 121 | HandleNetworkError(ex);
|
---|
| 122 | return null;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | public JobData GetJobData(Guid jobId) {
|
---|
| 128 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 129 | try {
|
---|
| 130 | JobData jobData = service.Obj.GetJobData(jobId);
|
---|
| 131 | return jobData;
|
---|
| 132 | }
|
---|
| 133 | catch (Exception ex) {
|
---|
| 134 | HandleNetworkError(ex);
|
---|
| 135 | return null;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /// <summary>
|
---|
| 141 | /// used on pause or if job finished to upload the job results
|
---|
| 142 | /// </summary>
|
---|
| 143 | /// <param name="job"></param>
|
---|
| 144 | /// <param name="jobData"></param>
|
---|
| 145 | public void UpdateJob(Job job, JobData jobData) {
|
---|
| 146 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 147 | try {
|
---|
| 148 | service.Obj.UpdateJob(job, jobData);
|
---|
| 149 | }
|
---|
| 150 | catch (Exception ex) {
|
---|
| 151 | HandleNetworkError(ex);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
|
---|
| 157 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 158 | try {
|
---|
| 159 | List<MessageContainer> msg = service.Obj.Heartbeat(heartbeat);
|
---|
| 160 | return msg;
|
---|
| 161 | }
|
---|
| 162 | catch (Exception ex) {
|
---|
| 163 | HandleNetworkError(ex);
|
---|
| 164 | return null;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
| 170 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 171 | try {
|
---|
| 172 | IEnumerable<PluginData> msg = service.Obj.GetPluginDatas(pluginIds);
|
---|
| 173 | return msg;
|
---|
| 174 | }
|
---|
| 175 | catch (Exception ex) {
|
---|
| 176 | HandleNetworkError(ex);
|
---|
| 177 | return null;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | public IEnumerable<Plugin> GetPlugins() {
|
---|
| 183 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 184 | try {
|
---|
| 185 | IEnumerable<Plugin> msg = service.Obj.GetPlugins();
|
---|
| 186 | return msg;
|
---|
| 187 | }
|
---|
| 188 | catch (Exception ex) {
|
---|
| 189 | HandleNetworkError(ex);
|
---|
| 190 | return null;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
|
---|
| 196 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 197 | try {
|
---|
| 198 | Guid msg = service.Obj.AddChildJob(parentJobId, job, jobData);
|
---|
| 199 | return msg;
|
---|
| 200 | }
|
---|
| 201 | catch (Exception ex) {
|
---|
| 202 | HandleNetworkError(ex);
|
---|
| 203 | return new Guid();
|
---|
| 204 | }
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | public IEnumerable<JobData> GetChildJobs(Guid? parentJobId) {
|
---|
| 209 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 210 | try {
|
---|
| 211 | IEnumerable<LightweightJob> msg = service.Obj.GetLightweightChildJobs(parentJobId, false, false);
|
---|
| 212 |
|
---|
| 213 | List<JobData> jobs = new List<JobData>();
|
---|
| 214 | foreach (LightweightJob ljob in msg)
|
---|
| 215 | jobs.Add(service.Obj.GetJobData(ljob.Id));
|
---|
| 216 |
|
---|
| 217 | return jobs;
|
---|
| 218 | }
|
---|
| 219 | catch (Exception ex) {
|
---|
| 220 | HandleNetworkError(ex);
|
---|
| 221 | return null;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | public void DeleteChildJobs(Guid jobId) {
|
---|
| 227 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 228 | try {
|
---|
| 229 | service.Obj.DeleteChildJobs(jobId);
|
---|
| 230 | }
|
---|
| 231 | catch (Exception ex) {
|
---|
| 232 | HandleNetworkError(ex);
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | public PluginData GetConfigurationFile() {
|
---|
| 238 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 239 | try {
|
---|
| 240 | PluginData msg = service.Obj.GetConfigurationFile();
|
---|
| 241 | return msg;
|
---|
| 242 | }
|
---|
| 243 | catch (Exception ex) {
|
---|
| 244 | HandleNetworkError(ex);
|
---|
| 245 | return null;
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
| 251 | private void OnExceptionOccured(Exception e) {
|
---|
| 252 | Logger.Error("Error: " + e.ToString());
|
---|
| 253 | var handler = ExceptionOccured;
|
---|
| 254 | if (handler != null) handler(this, new EventArgs<Exception>(e));
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | } |
---|