#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.Clients.Hive.SlaveCore.Properties; 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 = Settings.Default.SlaveUser; ServiceLocator.Instance.Password = Settings.Default.SlavePwd; } return instance; } } private WcfService() { ConnState = NetworkEnum.WcfConnState.Disconnected; } #region Task Methods public Task GetJob(Guid jobId) { return CallHiveService(s => s.GetJob(jobId)); } public void UpdateJob(Task job) { CallHiveService(s => s.UpdateJob(job)); } #endregion #region TaskData Methods public TaskData GetJobData(Guid jobId) { return CallHiveService(s => s.GetJobData(jobId)); } /// /// Uploads the taskData and sets a new jobState (while correctly setting Transferring state) /// public void UpdateJobData(Task job, TaskData jobData, Guid slaveId, TaskState state, string exception = "") { CallHiveService(service => { service.UpdateJob(job); job = service.UpdateJobState(job.Id, TaskState.Transferring, slaveId, null, null); HiveClient.TryAndRepeat(() => { service.UpdateJobData(job, jobData); }, Settings.Default.PluginDeletionRetries, "Could not upload jobdata."); service.UpdateJobState(job.Id, state, slaveId, null, exception); }); } public Task UpdateJobState(Guid jobId, TaskState 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 } }