[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);
|
---|
[6426] | 73 | job.PluginsNeededIds = PluginUtil.GetPluginDependencies(s, new List<Plugin>(), new List<Plugin>(), plugins);
|
---|
[6357] | 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>
|
---|
[6381] | 103 | public void UpdateJobData(Job job, JobData jobData, Guid slaveId, JobState state, string exception = "") {
|
---|
[5636] | 104 | CallHiveService(service => {
|
---|
| 105 | service.UpdateJob(job);
|
---|
| 106 | job = service.UpdateJobState(job.Id, JobState.Transferring, slaveId, null, null);
|
---|
[6419] | 107 | HiveClient.TryAndRepeat(() => {
|
---|
| 108 | service.UpdateJobData(job, jobData);
|
---|
| 109 | }, 3, "Could not upload jobdata.");
|
---|
[6381] | 110 | service.UpdateJobState(job.Id, state, slaveId, null, exception);
|
---|
[5636] | 111 | });
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | public Job UpdateJobState(Guid jobId, JobState jobState, string exception) {
|
---|
[5790] | 115 | return CallHiveService(s => s.UpdateJobState(jobId, jobState, ConfigManager.Instance.GetClientInfo().Id, null, exception));
|
---|
[5636] | 116 | }
|
---|
| 117 | #endregion
|
---|
| 118 |
|
---|
| 119 | #region Heartbeat Methods
|
---|
| 120 | public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
|
---|
| 121 | return CallHiveService(s => s.Heartbeat(heartbeat));
|
---|
| 122 | }
|
---|
| 123 | #endregion
|
---|
| 124 |
|
---|
| 125 | #region Plugin Methods
|
---|
[6004] | 126 | public Plugin GetPlugin(Guid id) {
|
---|
| 127 | return CallHiveService(s => s.GetPlugin(id));
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[5636] | 130 | public IEnumerable<Plugin> GetPlugins() {
|
---|
| 131 | return CallHiveService(s => s.GetPlugins());
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
| 135 | return CallHiveService(s => s.GetPluginDatas(pluginIds));
|
---|
| 136 | }
|
---|
| 137 | #endregion
|
---|
| 138 |
|
---|
| 139 | #region Events
|
---|
[5105] | 140 | public event EventHandler Connected;
|
---|
[5156] | 141 | private void OnConnected() {
|
---|
[5105] | 142 | var handler = Connected;
|
---|
| 143 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[5156] | 146 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
| 147 | private void OnExceptionOccured(Exception e) {
|
---|
| 148 | var handler = ExceptionOccured;
|
---|
| 149 | if (handler != null) handler(this, new EventArgs<Exception>(e));
|
---|
| 150 | }
|
---|
[5636] | 151 | #endregion
|
---|
[5156] | 152 |
|
---|
[5636] | 153 | #region Helpers
|
---|
[5105] | 154 | /// <summary>
|
---|
[6371] | 155 | /// Connects with the server, registers the events and fires the Connected event.
|
---|
[5105] | 156 | /// </summary>
|
---|
[5599] | 157 | public void Connect(Slave slaveInfo) {
|
---|
[5526] | 158 | CallHiveService(service => {
|
---|
| 159 | ConnState = NetworkEnum.WcfConnState.Connected;
|
---|
| 160 | ConnectedSince = DateTime.Now;
|
---|
| 161 | service.Hello(slaveInfo);
|
---|
| 162 | OnConnected();
|
---|
| 163 | });
|
---|
[5105] | 164 | }
|
---|
| 165 |
|
---|
| 166 | /// <summary>
|
---|
[6371] | 167 | /// Disconnects the slave from the server
|
---|
[5105] | 168 | /// </summary>
|
---|
| 169 | public void Disconnect() {
|
---|
[5526] | 170 | CallHiveService(service => {
|
---|
| 171 | service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
|
---|
| 172 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
| 173 | });
|
---|
[5105] | 174 | }
|
---|
| 175 |
|
---|
| 176 | /// <summary>
|
---|
[6371] | 177 | /// Network communication error handler.
|
---|
| 178 | /// Every network error gets logged and the connection switches to faulted state
|
---|
[5105] | 179 | /// </summary>
|
---|
| 180 | private void HandleNetworkError(Exception e) {
|
---|
| 181 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
| 182 | OnExceptionOccured(e);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[5526] | 185 | public void CallHiveService(Action<IHiveService> call) {
|
---|
| 186 | try {
|
---|
| 187 | ServiceLocator.Instance.CallHiveService(call);
|
---|
[5105] | 188 | }
|
---|
[5526] | 189 | catch (Exception ex) {
|
---|
| 190 | HandleNetworkError(ex);
|
---|
| 191 | }
|
---|
[5105] | 192 | }
|
---|
| 193 |
|
---|
[5526] | 194 | private T CallHiveService<T>(Func<IHiveService, T> call) {
|
---|
| 195 | try {
|
---|
| 196 | return ServiceLocator.Instance.CallHiveService(call);
|
---|
| 197 | }
|
---|
| 198 | catch (Exception ex) {
|
---|
| 199 | HandleNetworkError(ex);
|
---|
| 200 | return default(T);
|
---|
| 201 | }
|
---|
[5405] | 202 | }
|
---|
[5636] | 203 | #endregion
|
---|
[5105] | 204 | }
|
---|
| 205 | } |
---|