[923] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.ServiceModel;
|
---|
| 6 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
| 7 | using HeuristicLab.Hive.Contracts;
|
---|
| 8 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
| 9 | using HeuristicLab.Hive.Client.Common;
|
---|
[993] | 10 | using HeuristicLab.Hive.Client.Communication.ServerService;
|
---|
[923] | 11 |
|
---|
| 12 | namespace HeuristicLab.Hive.Client.Communication {
|
---|
| 13 | public class WcfService {
|
---|
| 14 | private static WcfService instance;
|
---|
| 15 | public static WcfService Instance {
|
---|
| 16 | get {
|
---|
| 17 | if (instance == null) {
|
---|
| 18 | instance = new WcfService();
|
---|
| 19 | }
|
---|
| 20 | return instance;
|
---|
| 21 | }
|
---|
| 22 | }
|
---|
| 23 |
|
---|
[932] | 24 | public DateTime ConnectedSince { get; private set; }
|
---|
| 25 | public NetworkEnum.WcfConnState ConnState { get; private set; }
|
---|
| 26 | public string ServerIP { get; private set; }
|
---|
| 27 | public int ServerPort { get; private set; }
|
---|
[923] | 28 |
|
---|
[924] | 29 | public event EventHandler ConnectionRestored;
|
---|
[1081] | 30 | public event EventHandler ServerChanged;
|
---|
| 31 | public event EventHandler Connected;
|
---|
[924] | 32 |
|
---|
[993] | 33 | public ClientCommunicatorClient proxy = null;
|
---|
[923] | 34 |
|
---|
| 35 | private WcfService() {
|
---|
[949] | 36 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
[923] | 37 | }
|
---|
| 38 | public void Connect() {
|
---|
| 39 | try {
|
---|
[1081] | 40 | proxy = new ClientCommunicatorClient(
|
---|
| 41 | new NetTcpBinding(),
|
---|
| 42 | new EndpointAddress("net.tcp://" + ServerIP + ":" + ServerPort + "/HiveServer/ClientCommunicator")
|
---|
| 43 | );
|
---|
[923] | 44 |
|
---|
| 45 | proxy.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(proxy_LoginCompleted);
|
---|
| 46 | proxy.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(proxy_PullJobCompleted);
|
---|
| 47 | proxy.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(proxy_SendJobResultCompleted);
|
---|
| 48 | proxy.SendHeartBeatCompleted += new EventHandler<SendHeartBeatCompletedEventArgs>(proxy_SendHeartBeatCompleted);
|
---|
[1082] | 49 | proxy.Open();
|
---|
| 50 |
|
---|
[932] | 51 | ConnState = NetworkEnum.WcfConnState.Connected;
|
---|
[1097] | 52 | ConnectedSince = DateTime.Now;
|
---|
| 53 |
|
---|
[1082] | 54 | if (Connected != null)
|
---|
| 55 | Connected(this, new EventArgs());
|
---|
[1097] | 56 |
|
---|
| 57 | if (ConnState == NetworkEnum.WcfConnState.Failed)
|
---|
| 58 | ConnectionRestored(this, new EventArgs());
|
---|
[923] | 59 | }
|
---|
| 60 | catch (Exception ex) {
|
---|
| 61 | NetworkErrorHandling(ex);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[932] | 65 | public void Connect(String serverIP, int serverPort) {
|
---|
[993] | 66 | String oldIp = this.ServerIP;
|
---|
| 67 | int oldPort = this.ServerPort;
|
---|
[932] | 68 | this.ServerIP = serverIP;
|
---|
[944] | 69 | this.ServerPort = serverPort;
|
---|
[923] | 70 | Connect();
|
---|
[993] | 71 | if (oldIp != serverIP || oldPort != ServerPort)
|
---|
[1036] | 72 | if(ServerChanged != null)
|
---|
| 73 | ServerChanged(this, new EventArgs());
|
---|
[923] | 74 | }
|
---|
| 75 |
|
---|
[932] | 76 | public void Disconnect() {
|
---|
| 77 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[923] | 80 | private void NetworkErrorHandling(Exception e) {
|
---|
[932] | 81 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
[923] | 82 | Logging.GetInstance().Error(this.ToString(), "exception: ", e);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | #region Login
|
---|
| 86 | public event System.EventHandler<LoginCompletedEventArgs> LoginCompleted;
|
---|
| 87 | public void LoginAsync(ClientInfo clientInfo) {
|
---|
[932] | 88 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
[923] | 89 | proxy.LoginAsync(clientInfo);
|
---|
| 90 | }
|
---|
| 91 | private void proxy_LoginCompleted(object sender, LoginCompletedEventArgs e) {
|
---|
| 92 | if (e.Error == null)
|
---|
| 93 | LoginCompleted(sender, e);
|
---|
| 94 | else
|
---|
| 95 | NetworkErrorHandling(e.Error.InnerException);
|
---|
| 96 | }
|
---|
[1097] | 97 |
|
---|
| 98 | public void LoginSync(ClientInfo clientInfo) {
|
---|
| 99 | try {
|
---|
| 100 | if (ConnState == NetworkEnum.WcfConnState.Connected) {
|
---|
| 101 | Response res = proxy.Login(clientInfo);
|
---|
| 102 | Logging.GetInstance().Info(this.ToString(), res.StatusMessage);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | catch (Exception e) {
|
---|
| 106 | NetworkErrorHandling(e);
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[923] | 110 | #endregion
|
---|
| 111 |
|
---|
| 112 | #region PullJob
|
---|
| 113 | public event System.EventHandler<PullJobCompletedEventArgs> PullJobCompleted;
|
---|
| 114 | public void PullJobAsync(Guid guid) {
|
---|
[1007] | 115 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
[923] | 116 | proxy.PullJobAsync(guid);
|
---|
| 117 | }
|
---|
| 118 | void proxy_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
|
---|
| 119 | if (e.Error == null)
|
---|
| 120 | PullJobCompleted(sender, e);
|
---|
| 121 | else
|
---|
| 122 | NetworkErrorHandling(e.Error);
|
---|
| 123 | }
|
---|
| 124 | #endregion
|
---|
| 125 |
|
---|
| 126 | #region SendJobResults
|
---|
| 127 | public event System.EventHandler<SendJobResultCompletedEventArgs> SendJobResultCompleted;
|
---|
[1103] | 128 | public void SendJobResultAsync(Guid clientId,
|
---|
| 129 | long jobId,
|
---|
| 130 | byte[] result,
|
---|
| 131 | Exception exception,
|
---|
| 132 | bool finished) {
|
---|
[932] | 133 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
[1103] | 134 | proxy.SendJobResultAsync(clientId,
|
---|
| 135 | jobId,
|
---|
| 136 | result,
|
---|
| 137 | exception,
|
---|
| 138 | finished);
|
---|
[923] | 139 | }
|
---|
| 140 | private void proxy_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
|
---|
| 141 | if (e.Error == null)
|
---|
| 142 | SendJobResultCompleted(sender, e);
|
---|
| 143 | else
|
---|
| 144 | NetworkErrorHandling(e.Error);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | #endregion
|
---|
| 148 |
|
---|
| 149 | #region Heartbeat
|
---|
| 150 |
|
---|
| 151 | public event System.EventHandler<SendHeartBeatCompletedEventArgs> SendHeartBeatCompleted;
|
---|
| 152 | public void SendHeartBeatAsync(HeartBeatData hbd) {
|
---|
[932] | 153 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
[923] | 154 | proxy.SendHeartBeatAsync(hbd);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | private void proxy_SendHeartBeatCompleted(object sender, SendHeartBeatCompletedEventArgs e) {
|
---|
| 158 | if (e.Error == null)
|
---|
| 159 | SendHeartBeatCompleted(sender, e);
|
---|
| 160 | else
|
---|
| 161 | NetworkErrorHandling(e.Error);
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[1097] | 164 | #endregion
|
---|
[923] | 165 | }
|
---|
| 166 | }
|
---|