[5105] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 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;
|
---|
[5156] | 25 | using HeuristicLab.Services.Hive.Common;
|
---|
| 26 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
[5105] | 27 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
| 28 |
|
---|
[5314] | 29 | namespace HeuristicLab.Clients.Hive.Slave {
|
---|
[5105] | 30 |
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// WcfService class is implemented as a Singleton and works as a communication Layer with the Server
|
---|
| 33 | /// </summary>
|
---|
| 34 | public class WcfService : MarshalByRefObject {
|
---|
| 35 | private static WcfService instance;
|
---|
| 36 | /// <summary>
|
---|
| 37 | /// Getter for the Instance of the WcfService
|
---|
| 38 | /// </summary>
|
---|
| 39 | /// <returns>the Instance of the WcfService class</returns>
|
---|
| 40 | public static WcfService Instance {
|
---|
| 41 | get {
|
---|
| 42 | if (instance == null) {
|
---|
| 43 | instance = new WcfService();
|
---|
[5526] | 44 | ServiceLocator.Instance.Username = "hiveslave";
|
---|
| 45 | ServiceLocator.Instance.Password = "hiveslave";
|
---|
[5105] | 46 | }
|
---|
| 47 | return instance;
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public DateTime ConnectedSince { get; private set; }
|
---|
| 52 | public NetworkEnum.WcfConnState ConnState { get; private set; }
|
---|
| 53 |
|
---|
| 54 | public event EventHandler Connected;
|
---|
[5156] | 55 | private void OnConnected() {
|
---|
[5105] | 56 | var handler = Connected;
|
---|
| 57 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[5156] | 60 | public event EventHandler<EventArgs<Exception>> ExceptionOccured;
|
---|
| 61 | private void OnExceptionOccured(Exception e) {
|
---|
| 62 | var handler = ExceptionOccured;
|
---|
| 63 | if (handler != null) handler(this, new EventArgs<Exception>(e));
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[5105] | 66 | /// <summary>
|
---|
| 67 | /// Constructor
|
---|
| 68 | /// </summary>
|
---|
| 69 | private WcfService() {
|
---|
| 70 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /// <summary>
|
---|
| 74 | /// Connects with the Server, registers the events and fires the Connected (and quiet possibly the ConnectionRestored) Event.
|
---|
| 75 | /// </summary>
|
---|
| 76 | public void Connect(HeuristicLab.Services.Hive.Common.DataTransfer.Slave slaveInfo) {
|
---|
[5526] | 77 | CallHiveService(service => {
|
---|
| 78 | ConnState = NetworkEnum.WcfConnState.Connected;
|
---|
| 79 | ConnectedSince = DateTime.Now;
|
---|
| 80 | service.Hello(slaveInfo);
|
---|
| 81 | OnConnected();
|
---|
| 82 | });
|
---|
[5105] | 83 | }
|
---|
| 84 |
|
---|
| 85 | /// <summary>
|
---|
| 86 | /// Disconnects the Slave from the Server
|
---|
| 87 | /// </summary>
|
---|
| 88 | public void Disconnect() {
|
---|
[5526] | 89 | CallHiveService(service => {
|
---|
| 90 | service.GoodBye(ConfigManager.Instance.GetClientInfo().Id);
|
---|
| 91 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
| 92 | });
|
---|
[5105] | 93 | }
|
---|
| 94 |
|
---|
| 95 | /// <summary>
|
---|
| 96 | /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
|
---|
| 97 | /// </summary>
|
---|
| 98 | /// <param name="e">The Exception</param>
|
---|
| 99 | private void HandleNetworkError(Exception e) {
|
---|
| 100 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
| 101 | OnExceptionOccured(e);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /// <summary>
|
---|
[5404] | 105 | /// Get a Job from the Server
|
---|
[5105] | 106 | /// </summary>
|
---|
[5404] | 107 | public Job GetJob(Guid jobId) {
|
---|
[5526] | 108 | return CallHiveService(s => s.GetJob(jobId));
|
---|
[5105] | 109 | }
|
---|
| 110 |
|
---|
| 111 | public JobData GetJobData(Guid jobId) {
|
---|
[5526] | 112 | return CallHiveService(s => s.GetJobData(jobId));
|
---|
[5105] | 113 | }
|
---|
| 114 |
|
---|
[5511] | 115 | public void UpdateJob(Job job) {
|
---|
[5526] | 116 | CallHiveService(s => s.UpdateJob(job));
|
---|
[5511] | 117 | }
|
---|
| 118 |
|
---|
[5105] | 119 | /// <summary>
|
---|
| 120 | /// used on pause or if job finished to upload the job results
|
---|
| 121 | /// </summary>
|
---|
| 122 | /// <param name="job"></param>
|
---|
| 123 | /// <param name="jobData"></param>
|
---|
[5511] | 124 | public void UpdateJobData(Job job, JobData jobData, Guid slaveId) {
|
---|
[5526] | 125 | CallHiveService(service => {
|
---|
| 126 | JobState before = job.State;
|
---|
| 127 | job.SetState(JobState.Transferring, slaveId, "");
|
---|
| 128 | service.UpdateJob(job);
|
---|
[5511] | 129 |
|
---|
[5526] | 130 | service.UpdateJobData(job, jobData);
|
---|
[5511] | 131 |
|
---|
[5526] | 132 | job.SetState(before, slaveId, "");
|
---|
| 133 | service.UpdateJob(job);
|
---|
| 134 | });
|
---|
[5105] | 135 | }
|
---|
[5526] | 136 |
|
---|
[5105] | 137 | public List<MessageContainer> SendHeartbeat(Heartbeat heartbeat) {
|
---|
[5526] | 138 | return CallHiveService(s => s.Heartbeat(heartbeat));
|
---|
[5105] | 139 | }
|
---|
| 140 |
|
---|
| 141 | public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
|
---|
[5526] | 142 | return CallHiveService(s => s.GetPluginDatas(pluginIds));
|
---|
[5105] | 143 | }
|
---|
| 144 |
|
---|
| 145 | public IEnumerable<Plugin> GetPlugins() {
|
---|
[5526] | 146 | return CallHiveService(s => s.GetPlugins());
|
---|
[5105] | 147 | }
|
---|
| 148 |
|
---|
| 149 | public Guid AddChildJob(Guid parentJobId, Job job, JobData jobData) {
|
---|
[5526] | 150 | return CallHiveService(s => s.AddChildJob(parentJobId, job, jobData));
|
---|
[5105] | 151 | }
|
---|
| 152 |
|
---|
| 153 | public IEnumerable<JobData> GetChildJobs(Guid? parentJobId) {
|
---|
[5526] | 154 | return CallHiveService(service => {
|
---|
| 155 | IEnumerable<LightweightJob> msg = service.GetLightweightChildJobs(parentJobId, false, false);
|
---|
[5105] | 156 |
|
---|
[5526] | 157 | List<JobData> jobs = new List<JobData>();
|
---|
| 158 | foreach (LightweightJob ljob in msg)
|
---|
| 159 | jobs.Add(service.GetJobData(ljob.Id));
|
---|
[5105] | 160 |
|
---|
[5526] | 161 | return jobs;
|
---|
| 162 | });
|
---|
[5105] | 163 | }
|
---|
| 164 |
|
---|
| 165 | public void DeleteChildJobs(Guid jobId) {
|
---|
[5526] | 166 | CallHiveService(s => s.DeleteChildJobs(jobId));
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | public void CallHiveService(Action<IHiveService> call) {
|
---|
| 170 | try {
|
---|
| 171 | ServiceLocator.Instance.CallHiveService(call);
|
---|
[5105] | 172 | }
|
---|
[5526] | 173 | catch (Exception ex) {
|
---|
| 174 | HandleNetworkError(ex);
|
---|
| 175 | }
|
---|
[5105] | 176 | }
|
---|
| 177 |
|
---|
[5526] | 178 | private T CallHiveService<T>(Func<IHiveService, T> call) {
|
---|
| 179 | try {
|
---|
| 180 | return ServiceLocator.Instance.CallHiveService(call);
|
---|
| 181 | }
|
---|
| 182 | catch (Exception ex) {
|
---|
| 183 | HandleNetworkError(ex);
|
---|
| 184 | return default(T);
|
---|
| 185 | }
|
---|
[5405] | 186 | }
|
---|
[5105] | 187 | }
|
---|
| 188 | } |
---|