#region License Information /* HeuristicLab * Copyright (C) 2002-2011 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.Common; namespace HeuristicLab.Clients.Hive.SlaveCore { /// /// WcfService class is implemented as a singleton and works as a communication layer with the Hive server /// public class WcfService : MarshalByRefObject, IPluginProvider { private static WcfService instance; public DateTime ConnectedSince { get; private set; } public NetworkEnum.WcfConnState ConnState { get; private set; } /// /// Getter for the Instance of the WcfService /// /// the Instance of the WcfService class public static WcfService Instance { get { if (instance == null) { instance = new WcfService(); ServiceLocator.Instance.Username = "hiveslave"; ServiceLocator.Instance.Password = "hiveslave"; } return instance; } } private WcfService() { ConnState = NetworkEnum.WcfConnState.Disconnected; } #region Job Methods public Job GetJob(Guid jobId) { return CallHiveService(s => s.GetJob(jobId)); } public void UpdateJob(Job job) { CallHiveService(s => s.UpdateJob(job)); } #endregion #region JobData Methods public JobData GetJobData(Guid jobId) { return CallHiveService(s => s.GetJobData(jobId)); } /// /// Uploads the jobData and sets a new jobState (while correctly setting Transferring state) /// public void UpdateJobData(Job job, JobData jobData, Guid slaveId, JobState state, string exception = "") { CallHiveService(service => { service.UpdateJob(job); job = service.UpdateJobState(job.Id, JobState.Transferring, slaveId, null, null); HiveClient.TryAndRepeat(() => { service.UpdateJobData(job, jobData); }, 3, "Could not upload jobdata."); service.UpdateJobState(job.Id, state, slaveId, null, exception); }); } public Job UpdateJobState(Guid jobId, JobState jobState, string exception) { return CallHiveService(s => s.UpdateJobState(jobId, jobState, ConfigManager.Instance.GetClientInfo().Id, null, exception)); } #endregion #region Heartbeat Methods public List SendHeartbeat(Heartbeat heartbeat) { return CallHiveService(s => s.Heartbeat(heartbeat)); } #endregion #region Plugin Methods public Plugin GetPlugin(Guid id) { return CallHiveService(s => s.GetPlugin(id)); } public IEnumerable GetPlugins() { return CallHiveService(s => s.GetPlugins()); } public IEnumerable GetPluginDatas(List pluginIds) { return CallHiveService(s => s.GetPluginDatas(pluginIds)); } #endregion #region Events 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)); } #endregion #region Helpers /// /// Connects with the server, registers the events and fires the Connected event. /// public void Connect(Slave slaveInfo) { CallHiveService(service => { ConnState = NetworkEnum.WcfConnState.Connected; ConnectedSince = DateTime.Now; service.Hello(slaveInfo); OnConnected(); }); } /// /// Disconnects the slave from the server /// public void Disconnect() { CallHiveService(service => { service.GoodBye(ConfigManager.Instance.GetClientInfo().Id); ConnState = NetworkEnum.WcfConnState.Disconnected; }); } /// /// Network communication error handler. /// Every network error gets logged and the connection switches to faulted state /// private void HandleNetworkError(Exception e) { ConnState = NetworkEnum.WcfConnState.Failed; OnExceptionOccured(e); } public void CallHiveService(Action call) { try { ServiceLocator.Instance.CallHiveService(call); } catch (Exception ex) { HandleNetworkError(ex); } } private T CallHiveService(Func call) { try { return ServiceLocator.Instance.CallHiveService(call); } catch (Exception ex) { HandleNetworkError(ex); return default(T); } } #endregion } }