[1132] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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;
|
---|
[923] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.ServiceModel;
|
---|
| 27 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
| 28 | using HeuristicLab.Hive.Contracts;
|
---|
| 29 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
| 30 | using HeuristicLab.Hive.Client.Common;
|
---|
[993] | 31 | using HeuristicLab.Hive.Client.Communication.ServerService;
|
---|
[923] | 32 |
|
---|
| 33 | namespace HeuristicLab.Hive.Client.Communication {
|
---|
[1132] | 34 | /// <summary>
|
---|
| 35 | /// WcfService class is implemented as a Singleton and works as a communication Layer with the Server
|
---|
| 36 | /// </summary>
|
---|
[923] | 37 | public class WcfService {
|
---|
| 38 | private static WcfService instance;
|
---|
[1132] | 39 | /// <summary>
|
---|
| 40 | /// Getter for the Instance of the WcfService
|
---|
| 41 | /// </summary>
|
---|
| 42 | /// <returns>the Instance of the WcfService class</returns>
|
---|
[923] | 43 | public static WcfService Instance {
|
---|
| 44 | get {
|
---|
| 45 | if (instance == null) {
|
---|
| 46 | instance = new WcfService();
|
---|
| 47 | }
|
---|
| 48 | return instance;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[932] | 52 | public DateTime ConnectedSince { get; private set; }
|
---|
| 53 | public NetworkEnum.WcfConnState ConnState { get; private set; }
|
---|
| 54 | public string ServerIP { get; private set; }
|
---|
| 55 | public int ServerPort { get; private set; }
|
---|
[923] | 56 |
|
---|
[924] | 57 | public event EventHandler ConnectionRestored;
|
---|
[1081] | 58 | public event EventHandler ServerChanged;
|
---|
| 59 | public event EventHandler Connected;
|
---|
[924] | 60 |
|
---|
[993] | 61 | public ClientCommunicatorClient proxy = null;
|
---|
[923] | 62 |
|
---|
[1132] | 63 | /// <summary>
|
---|
| 64 | /// Constructor
|
---|
| 65 | /// </summary>
|
---|
[923] | 66 | private WcfService() {
|
---|
[949] | 67 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
[923] | 68 | }
|
---|
[1132] | 69 |
|
---|
| 70 | /// <summary>
|
---|
| 71 | /// Connects with the Server, registers the events and fires the Connected (and quiet possibly the ConnectionRestored) Event.
|
---|
| 72 | /// </summary>
|
---|
[923] | 73 | public void Connect() {
|
---|
| 74 | try {
|
---|
[1081] | 75 | proxy = new ClientCommunicatorClient(
|
---|
[1449] | 76 | new NetTcpBinding(SecurityMode.None, true),
|
---|
[1081] | 77 | new EndpointAddress("net.tcp://" + ServerIP + ":" + ServerPort + "/HiveServer/ClientCommunicator")
|
---|
| 78 | );
|
---|
[923] | 79 |
|
---|
| 80 | proxy.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(proxy_LoginCompleted);
|
---|
[1366] | 81 | proxy.SendJobCompleted += new EventHandler<SendJobCompletedEventArgs>(proxy_SendJobCompleted);
|
---|
[1379] | 82 | proxy.StoreFinishedJobResultCompleted += new EventHandler<StoreFinishedJobResultCompletedEventArgs>(proxy_StoreFinishedJobResultCompleted);
|
---|
| 83 | proxy.ProcessSnapshotCompleted += new EventHandler<ProcessSnapshotCompletedEventArgs>(proxy_ProcessSnapshotCompleted);
|
---|
[1366] | 84 | proxy.ProcessHeartBeatCompleted += new EventHandler<ProcessHeartBeatCompletedEventArgs>(proxy_ProcessHeartBeatCompleted);
|
---|
[1082] | 85 | proxy.Open();
|
---|
| 86 |
|
---|
[932] | 87 | ConnState = NetworkEnum.WcfConnState.Connected;
|
---|
[1097] | 88 | ConnectedSince = DateTime.Now;
|
---|
| 89 |
|
---|
[1082] | 90 | if (Connected != null)
|
---|
| 91 | Connected(this, new EventArgs());
|
---|
[1340] | 92 | //Todo: This won't be hit. EVER
|
---|
[1097] | 93 | if (ConnState == NetworkEnum.WcfConnState.Failed)
|
---|
| 94 | ConnectionRestored(this, new EventArgs());
|
---|
[923] | 95 | }
|
---|
[1371] | 96 | catch (Exception ex) {
|
---|
| 97 | HandleNetworkError(ex);
|
---|
[923] | 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[1379] | 101 |
|
---|
| 102 |
|
---|
[1132] | 103 | /// <summary>
|
---|
| 104 | /// Changes the Connectionsettings (serverIP & serverPort) and reconnects
|
---|
| 105 | /// </summary>
|
---|
| 106 | /// <param name="serverIP">current Server IP</param>
|
---|
| 107 | /// <param name="serverPort">current Server Port</param>
|
---|
[932] | 108 | public void Connect(String serverIP, int serverPort) {
|
---|
[993] | 109 | String oldIp = this.ServerIP;
|
---|
| 110 | int oldPort = this.ServerPort;
|
---|
[932] | 111 | this.ServerIP = serverIP;
|
---|
[944] | 112 | this.ServerPort = serverPort;
|
---|
[923] | 113 | Connect();
|
---|
[993] | 114 | if (oldIp != serverIP || oldPort != ServerPort)
|
---|
[1036] | 115 | if(ServerChanged != null)
|
---|
| 116 | ServerChanged(this, new EventArgs());
|
---|
[923] | 117 | }
|
---|
[1132] | 118 |
|
---|
| 119 | /// <summary>
|
---|
| 120 | /// Disconnects the Client from the Server
|
---|
| 121 | /// </summary>
|
---|
[932] | 122 | public void Disconnect() {
|
---|
| 123 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[1132] | 126 | /// <summary>
|
---|
| 127 | /// Network communication Error Handler - Every network error gets logged and the connection switches to faulted state
|
---|
| 128 | /// </summary>
|
---|
| 129 | /// <param name="e">The Exception</param>
|
---|
[1371] | 130 | private void HandleNetworkError(Exception e) {
|
---|
[932] | 131 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
[1371] | 132 | Logging.Instance.Error(this.ToString(), "exception: ", e);
|
---|
[923] | 133 | }
|
---|
| 134 |
|
---|
[1132] | 135 |
|
---|
| 136 |
|
---|
| 137 | /// <summary>
|
---|
| 138 | /// Methods for the Server Login
|
---|
| 139 | /// </summary>
|
---|
[923] | 140 | #region Login
|
---|
| 141 | public event System.EventHandler<LoginCompletedEventArgs> LoginCompleted;
|
---|
| 142 | public void LoginAsync(ClientInfo clientInfo) {
|
---|
[932] | 143 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
[923] | 144 | proxy.LoginAsync(clientInfo);
|
---|
| 145 | }
|
---|
| 146 | private void proxy_LoginCompleted(object sender, LoginCompletedEventArgs e) {
|
---|
| 147 | if (e.Error == null)
|
---|
| 148 | LoginCompleted(sender, e);
|
---|
| 149 | else
|
---|
[1371] | 150 | HandleNetworkError(e.Error.InnerException);
|
---|
[923] | 151 | }
|
---|
[1097] | 152 |
|
---|
| 153 | public void LoginSync(ClientInfo clientInfo) {
|
---|
| 154 | try {
|
---|
| 155 | if (ConnState == NetworkEnum.WcfConnState.Connected) {
|
---|
| 156 | Response res = proxy.Login(clientInfo);
|
---|
[1255] | 157 | ConnState = NetworkEnum.WcfConnState.Loggedin;
|
---|
[1371] | 158 | Logging.Instance.Info(this.ToString(), res.StatusMessage);
|
---|
[1097] | 159 | }
|
---|
| 160 | }
|
---|
| 161 | catch (Exception e) {
|
---|
[1371] | 162 | HandleNetworkError(e);
|
---|
[1097] | 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[923] | 166 | #endregion
|
---|
| 167 |
|
---|
[1132] | 168 | /// <summary>
|
---|
| 169 | /// Pull a Job from the Server
|
---|
| 170 | /// </summary>
|
---|
[923] | 171 | #region PullJob
|
---|
[1367] | 172 | public event System.EventHandler<SendJobCompletedEventArgs> SendJobCompleted;
|
---|
| 173 | public void SendJobAsync(Guid guid) {
|
---|
[1255] | 174 | if (ConnState == NetworkEnum.WcfConnState.Loggedin)
|
---|
[1366] | 175 | proxy.SendJobAsync(guid);
|
---|
[923] | 176 | }
|
---|
[1366] | 177 | void proxy_SendJobCompleted(object sender, SendJobCompletedEventArgs e) {
|
---|
[923] | 178 | if (e.Error == null)
|
---|
[1367] | 179 | SendJobCompleted(sender, e);
|
---|
[923] | 180 | else
|
---|
[1371] | 181 | HandleNetworkError(e.Error);
|
---|
[923] | 182 | }
|
---|
| 183 | #endregion
|
---|
| 184 |
|
---|
[1132] | 185 | /// <summary>
|
---|
| 186 | /// Send back finished Job Results
|
---|
| 187 | /// </summary>
|
---|
[923] | 188 | #region SendJobResults
|
---|
[1379] | 189 | public event System.EventHandler<StoreFinishedJobResultCompletedEventArgs> StoreFinishedJobResultCompleted;
|
---|
[1449] | 190 | public void StoreFinishedJobResultAsync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
|
---|
[1255] | 191 | if (ConnState == NetworkEnum.WcfConnState.Loggedin)
|
---|
[1379] | 192 | proxy.StoreFinishedJobResultAsync(clientId, jobId, result, percentage, exception, finished);
|
---|
[923] | 193 | }
|
---|
[1379] | 194 | private void proxy_StoreFinishedJobResultCompleted(object sender, StoreFinishedJobResultCompletedEventArgs e) {
|
---|
[923] | 195 | if (e.Error == null)
|
---|
[1379] | 196 | StoreFinishedJobResultCompleted(sender, e);
|
---|
[923] | 197 | else
|
---|
[1371] | 198 | HandleNetworkError(e.Error);
|
---|
[923] | 199 | }
|
---|
| 200 |
|
---|
| 201 | #endregion
|
---|
| 202 |
|
---|
[1379] | 203 | #region Processsnapshots
|
---|
| 204 | public event System.EventHandler<ProcessSnapshotCompletedEventArgs> ProcessSnapshotCompleted;
|
---|
[1449] | 205 | public void ProcessSnapshotAsync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
|
---|
[1379] | 206 | if(ConnState == NetworkEnum.WcfConnState.Loggedin)
|
---|
| 207 | proxy.ProcessSnapshotAsync(clientId, jobId, result, percentage, exception);
|
---|
| 208 | }
|
---|
| 209 | void proxy_ProcessSnapshotCompleted(object sender, ProcessSnapshotCompletedEventArgs e) {
|
---|
| 210 | if (e.Error == null)
|
---|
| 211 | ProcessSnapshotCompleted(sender, e);
|
---|
| 212 | else
|
---|
| 213 | HandleNetworkError(e.Error);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | #endregion
|
---|
| 217 |
|
---|
[1132] | 218 | /// <summary>
|
---|
| 219 | /// Methods for sending the periodically Heartbeat
|
---|
| 220 | /// </summary>
|
---|
[923] | 221 | #region Heartbeat
|
---|
| 222 |
|
---|
[1366] | 223 | public event System.EventHandler<ProcessHeartBeatCompletedEventArgs> SendHeartBeatCompleted;
|
---|
[923] | 224 | public void SendHeartBeatAsync(HeartBeatData hbd) {
|
---|
[1255] | 225 | if (ConnState == NetworkEnum.WcfConnState.Loggedin)
|
---|
[1366] | 226 | proxy.ProcessHeartBeatAsync(hbd);
|
---|
[923] | 227 | }
|
---|
| 228 |
|
---|
[1366] | 229 | private void proxy_ProcessHeartBeatCompleted(object sender, ProcessHeartBeatCompletedEventArgs e) {
|
---|
[923] | 230 | if (e.Error == null)
|
---|
| 231 | SendHeartBeatCompleted(sender, e);
|
---|
| 232 | else
|
---|
[1371] | 233 | HandleNetworkError(e.Error);
|
---|
[923] | 234 | }
|
---|
| 235 |
|
---|
[1097] | 236 | #endregion
|
---|
[1271] | 237 |
|
---|
| 238 | /// <summary>
|
---|
| 239 | /// Send back finished and Stored Job Results
|
---|
| 240 | /// </summary>
|
---|
[1379] | 241 | /*#region SendJobResults
|
---|
| 242 | public event System.EventHandler<StoreFinishedJobResultCompletedEventArgs> ProcessStoredJobResultCompleted;
|
---|
[1367] | 243 | public void ProcessStoredJobResultAsync(Guid clientId, long jobId, byte[] result, double percentage, Exception exception, bool finished) {
|
---|
[1271] | 244 | if (ConnState == NetworkEnum.WcfConnState.Loggedin)
|
---|
| 245 | //TODO: some sort of algo for the stored jobs
|
---|
[1366] | 246 | proxy.ProcessJobResultAsync(clientId, jobId, result, percentage, exception, finished);
|
---|
[1271] | 247 | }
|
---|
[1379] | 248 | #endregion */
|
---|
[1271] | 249 |
|
---|
[1449] | 250 | public ResponseResultReceived SendStoredJobResultsSync(Guid clientId, Guid jobId, byte[] result, double percentage, Exception exception, bool finished) {
|
---|
[1379] | 251 | return proxy.StoreFinishedJobResult(clientId, jobId, result, percentage, exception);
|
---|
[1271] | 252 | }
|
---|
[923] | 253 | }
|
---|
| 254 | }
|
---|