#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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 HeuristicLab.Clients.Common; using HeuristicLab.Clients.Hive.Slave.Properties; using HeuristicLab.Common; using HeuristicLab.Services.Hive.Common; using HeuristicLab.Services.Hive.Common.DataTransfer; using HeuristicLab.Services.Hive.Common.ServiceContracts; namespace HeuristicLab.Clients.Hive.Salve { /// /// WcfService class is implemented as a Singleton and works as a communication Layer with the Server /// public class WcfService : MarshalByRefObject { private static WcfService instance; /// /// Getter for the Instance of the WcfService /// /// the Instance of the WcfService class public static WcfService Instance { get { if (instance == null) { instance = new WcfService(); } return instance; } } public DateTime ConnectedSince { get; private set; } public NetworkEnum.WcfConnState ConnState { get; private set; } public event EventHandler Connected; private void OnConnected() { var handler = Connected; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler> ExceptionOccured; private void OnExceptionOccured(Exception e) { var handler = ExceptionOccured; if (handler != null) handler(this, new EventArgs(e)); } /// /// Constructor /// private WcfService() { ConnState = NetworkEnum.WcfConnState.Disconnected; } /// /// Connects with the Server, registers the events and fires the Connected (and quiet possibly the ConnectionRestored) Event. /// public void Connect(HeuristicLab.Services.Hive.Common.DataTransfer.Slave slaveInfo) { using (Disposable service = ServiceLocator.Instance.GetService()) { try { ConnState = NetworkEnum.WcfConnState.Connected; ConnectedSince = DateTime.Now; service.Obj.Hello(Settings.Default.Guid, slaveInfo.Name, slaveInfo.Cores.Value, slaveInfo.Memory.Value); OnConnected(); } catch (Exception ex) { HandleNetworkError(ex); } } } /// /// Disconnects the Slave from the Server /// public void Disconnect() { using (Disposable service = ServiceLocator.Instance.GetService()) { try { service.Obj.GoodBye(); ConnState = NetworkEnum.WcfConnState.Disconnected; } catch (Exception ex) { HandleNetworkError(ex); } } } /// /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state /// /// The Exception private void HandleNetworkError(Exception e) { ConnState = NetworkEnum.WcfConnState.Failed; OnExceptionOccured(e); } /// /// Aquire a Job from the Server, return the Job /// public Job AquireJob() { using (Disposable service = ServiceLocator.Instance.GetService()) { try { Job job = service.Obj.AquireJob(Settings.Default.Guid); return job; } catch (Exception ex) { HandleNetworkError(ex); return null; } } } public JobData GetJobData(Guid jobId) { using (Disposable service = ServiceLocator.Instance.GetService()) { try { JobData jobData = service.Obj.GetJobData(jobId); return jobData; } catch (Exception ex) { HandleNetworkError(ex); return null; } } } /// /// used on pause or if job finished to upload the job results /// /// /// public void UpdateJob(Job job, JobData jobData) { using (Disposable service = ServiceLocator.Instance.GetService()) { try { service.Obj.UpdateJob(job, jobData); } catch (Exception ex) { HandleNetworkError(ex); } } } public List SendHeartbeat(Heartbeat heartbeat) { using (Disposable service = ServiceLocator.Instance.GetService()) { try { List msg = service.Obj.Heartbeat(heartbeat); return msg; } catch (Exception ex) { HandleNetworkError(ex); return null; } } } public IEnumerable GetPluginDatas(List pluginIds) { using (Disposable service = ServiceLocator.Instance.GetService()) { try { IEnumerable msg = service.Obj.GetPluginDatas(pluginIds); return msg; } catch (Exception ex) { HandleNetworkError(ex); return null; } } } public IEnumerable GetPlugins() { using (Disposable service = ServiceLocator.Instance.GetService()) { try { IEnumerable msg = service.Obj.GetPlugins(); return msg; } catch (Exception ex) { HandleNetworkError(ex); return null; } } } public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) { using (Disposable service = ServiceLocator.Instance.GetService()) { try { Guid msg = service.Obj.AddChildJob(parentJobId, job, jobData); return msg; } catch (Exception ex) { HandleNetworkError(ex); return new Guid(); } } } public IEnumerable GetChildJobs(Guid? parentJobId) { using (Disposable service = ServiceLocator.Instance.GetService()) { try { IEnumerable msg = service.Obj.GetLightweightChildJobs(parentJobId, false, false); List jobs = new List(); foreach (LightweightJob ljob in msg) jobs.Add(service.Obj.GetJobData(ljob.Id)); return jobs; } catch (Exception ex) { HandleNetworkError(ex); return null; } } } public void DeleteChildJobs(Guid jobId) { using (Disposable service = ServiceLocator.Instance.GetService()) { try { service.Obj.DeleteChildJobs(jobId); } catch (Exception ex) { HandleNetworkError(ex); } } } public PluginData GetConfigurationFile() { using (Disposable service = ServiceLocator.Instance.GetService()) { try { PluginData msg = service.Obj.GetConfigurationFile(); return msg; } catch (Exception ex) { HandleNetworkError(ex); return null; } } } } }