[5105] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[6371] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5105] | 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;
|
---|
[6357] | 25 | using HeuristicLab.Hive;
|
---|
| 26 | using HeuristicLab.PluginInfrastructure;
|
---|
[5105] | 27 |
|
---|
[5599] | 28 | namespace HeuristicLab.Clients.Hive.SlaveCore {
|
---|
[5105] | 29 |
|
---|
| 30 | /// <summary>
|
---|
[6371] | 31 | /// WcfService class is implemented as a singleton and works as a communication layer with the Hive server
|
---|
[5105] | 32 | /// </summary>
|
---|
[6357] | 33 | public class WcfService : MarshalByRefObject, IPluginProvider {
|
---|
[5105] | 34 | private static WcfService instance;
|
---|
[6371] | 35 | public DateTime ConnectedSince { get; private set; }
|
---|
| 36 | public NetworkEnum.WcfConnState ConnState { get; private set; }
|
---|
| 37 |
|
---|
[5105] | 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 | instance = new WcfService();
|
---|
[5526] | 46 | ServiceLocator.Instance.Username = "hiveslave";
|
---|
| 47 | ServiceLocator.Instance.Password = "hiveslave";
|
---|
[5105] | 48 | }
|
---|
| 49 | return instance;
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[5636] | 53 | private WcfService() {
|
---|
| 54 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | #region Job Methods
|
---|
| 58 | public Job GetJob(Guid jobId) {
|
---|
| 59 | return CallHiveService(s => s.GetJob(jobId));
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public void UpdateJob(Job job) {
|
---|
| 63 | CallHiveService(s => s.UpdateJob(job));
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[6357] | 66 | public Guid AddChildJob(Guid parentJobId, Job job, IJob jobDataObject) {
|
---|
| 67 | return CallHiveService(s => {
|
---|
| 68 | JobData jobData = new JobData();
|
---|
| 69 | IEnumerable<Type> types;
|
---|
| 70 | jobData.Data = PersistenceUtil.Serialize(jobDataObject, out types);
|
---|
| 71 | var plugins = new List<IPluginDescription>();
|
---|
| 72 | PluginUtil.CollectDeclaringPlugins(plugins, types);
|
---|
| 73 | job.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, new List<Plugin>(), new List<Plugin>(), plugins, false);
|
---|
| 74 | return s.AddChildJob(parentJobId, job, jobData);
|
---|
| 75 | });
|
---|
[5636] | 76 | }
|
---|
| 77 |
|
---|
| 78 | public IEnumerable<JobData> GetChildJobs(Guid? parentJobId) {
|
---|
| 79 | return CallHiveService(service => {
|
---|
| 80 | IEnumerable<LightweightJob> msg = service.GetLightweightChildJobs(parentJobId, false, false);
|
---|
| 81 |
|
---|
| 82 | List<JobData> jobs = new List<JobData>();
|
---|
| 83 | foreach (LightweightJob ljob in msg)
|
---|
| 84 | jobs.Add(service.GetJobData(ljob.Id));
|
---|
| 85 |
|
---|
| 86 | return jobs;
|
---|
| 87 | });
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public void DeleteChildJobs(Guid jobId) {
|
---|
| 91 | CallHiveService(s => s.DeleteChildJobs(jobId));
|
---|
| 92 | }
|
---|
| 93 | #endregion
|
---|
| 94 |
|
---|
| 95 | #region JobData Methods
|
---|
| 96 | public JobData GetJobData(Guid jobId) {
|
---|
| 97 | return CallHiveService(s => s.GetJobData(jobId));
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /// <summary>
|
---|
[6371] | 101 | /// Uploads the jobData and sets a new jobState (while correctly setting Transferring state)
|
---|
[5636] | 102 | /// </summary>
|
---|
| 103 | public void UpdateJobData(Job job, JobData jobData, Guid slaveId, JobState state) {
|
---|
| 104 | CallHiveService(service => {
|
---|
| 105 | service.UpdateJob(job);
|
---|
| 106 | job = service.UpdateJobState(job.Id, JobState.Transferring, slaveId, null, null);
|
---|
| 107 | service.UpdateJobData(job, jobData);
|
---|
| 108 | service.UpdateJobState(job.Id, state, slaveId, null, null);
|
---|
| 109 | });
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | public Job UpdateJobState(Guid jobId, JobState jobState, string exception) {
|
---|
[5790] | 113 | return CallHiveService(s => s.UpdateJobState(jobId, jobState, ConfigManager.Instance.GetClientInfo().Id, null, exception));
|
---|
[5636] | 114 | }
|
---|
| 115 | #endregion
|
---|
| 116 |
|
---|
| 117 | #region Heartbeat Methods
|
---|
| 118 | public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
|
---|
| 119 | return CallHiveService(s => s.Heartbeat(heartbeat));
|
---|
| 120 | }
|
---|
| 121 | #endregion
|
---|
| 122 |
|
---|
| 123 | #region Plugin Methods
|
---|
[6004] | 124 | public Plugin GetPlugin(Guid id) {
|
---|
| 125 | return CallHiveService(s => s.GetPlugin(id));
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[5636] | 128 | public IEnumerable<Plugin> GetPlugins() {
|
---|
| 129 | return CallHiveService(s => s.GetPlugins());
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
| 133 | return CallHiveService(s => s.GetPluginDatas(pluginIds));
|
---|
| 134 | }
|
---|
| 135 | #endregion
|
---|
| 136 |
|
---|
| 137 | #region Events
|
---|
[5105] | 138 | public event EventHandler Connected;
|
---|
[5156] | 139 | private void OnConnected() {
|
---|
[5105] | 140 | var handler = Connected;
|
---|
| 141 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[5156] | 144 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
| 145 | private void OnExceptionOccured(Exception e) {
|
---|
| 146 | var handler = ExceptionOccured;
|
---|
| 147 | if (handler != null) handler(this, new EventArgs<Exception>(e));
|
---|
| 148 | }
|
---|
[5636] | 149 | #endregion
|
---|
[5156] | 150 |
|
---|
[5636] | 151 | #region Helpers
|
---|
[5105] | 152 | /// <summary>
|
---|
[6371] | 153 | /// Connects with the server, registers the events and fires the Connected event.
|
---|
[5105] | 154 | /// </summary>
|
---|
[5599] | 155 | public void Connect(Slave slaveInfo) {
|
---|
[5526] | 156 | CallHiveService(service => {
|
---|
| 157 | ConnState = NetworkEnum.WcfConnState.Connected;
|
---|
| 158 | ConnectedSince = DateTime.Now;
|
---|
| 159 | service.Hello(slaveInfo);
|
---|
| 160 | OnConnected();
|
---|
| 161 | });
|
---|
[5105] | 162 | }
|
---|
| 163 |
|
---|
| 164 | /// <summary>
|
---|
[6371] | 165 | /// Disconnects the slave from the server
|
---|
[5105] | 166 | /// </summary>
|
---|
| 167 | public void Disconnect() {
|
---|
[5526] | 168 | CallHiveService(service => {
|
---|
| 169 | service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
|
---|
| 170 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
| 171 | });
|
---|
[5105] | 172 | }
|
---|
| 173 |
|
---|
| 174 | /// <summary>
|
---|
[6371] | 175 | /// Network communication error handler.
|
---|
| 176 | /// Every network error gets logged and the connection switches to faulted state
|
---|
[5105] | 177 | /// </summary>
|
---|
| 178 | private void HandleNetworkError(Exception e) {
|
---|
| 179 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
| 180 | OnExceptionOccured(e);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[5526] | 183 | public void CallHiveService(Action<IHiveService> call) {
|
---|
| 184 | try {
|
---|
| 185 | ServiceLocator.Instance.CallHiveService(call);
|
---|
[5105] | 186 | }
|
---|
[5526] | 187 | catch (Exception ex) {
|
---|
| 188 | HandleNetworkError(ex);
|
---|
| 189 | }
|
---|
[5105] | 190 | }
|
---|
| 191 |
|
---|
[5526] | 192 | private T CallHiveService<T>(Func<IHiveService, T> call) {
|
---|
| 193 | try {
|
---|
| 194 | return ServiceLocator.Instance.CallHiveService(call);
|
---|
| 195 | }
|
---|
| 196 | catch (Exception ex) {
|
---|
| 197 | HandleNetworkError(ex);
|
---|
| 198 | return default(T);
|
---|
| 199 | }
|
---|
[5405] | 200 | }
|
---|
[5636] | 201 | #endregion
|
---|
[5105] | 202 | }
|
---|
| 203 | } |
---|