[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;
|
---|
[932] | 30 | public event EventHandler ServerChanged;
|
---|
[924] | 31 |
|
---|
[993] | 32 | public ClientCommunicatorClient proxy = null;
|
---|
[923] | 33 |
|
---|
| 34 | private WcfService() {
|
---|
[949] | 35 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
[923] | 36 | }
|
---|
| 37 | public void Connect() {
|
---|
| 38 | try {
|
---|
[1001] | 39 | proxy = null;
|
---|
[923] | 40 | if (proxy == null) {
|
---|
| 41 | proxy = new ClientCommunicatorClient(
|
---|
| 42 | new NetTcpBinding(),
|
---|
[932] | 43 | new EndpointAddress("net.tcp://" + ServerIP + ":" + ServerPort + "/HiveServer/ClientCommunicator")
|
---|
[923] | 44 | );
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | proxy.LoginCompleted += new EventHandler<LoginCompletedEventArgs>(proxy_LoginCompleted);
|
---|
| 48 | proxy.PullJobCompleted += new EventHandler<PullJobCompletedEventArgs>(proxy_PullJobCompleted);
|
---|
| 49 | proxy.SendJobResultCompleted += new EventHandler<SendJobResultCompletedEventArgs>(proxy_SendJobResultCompleted);
|
---|
| 50 | proxy.SendHeartBeatCompleted += new EventHandler<SendHeartBeatCompletedEventArgs>(proxy_SendHeartBeatCompleted);
|
---|
[924] | 51 |
|
---|
[932] | 52 | if (ConnState == NetworkEnum.WcfConnState.Failed)
|
---|
[924] | 53 | ConnectionRestored(this, new EventArgs());
|
---|
[932] | 54 | ConnState = NetworkEnum.WcfConnState.Connected;
|
---|
[993] | 55 | ConnectedSince = DateTime.Now;
|
---|
[923] | 56 | }
|
---|
| 57 | catch (Exception ex) {
|
---|
| 58 | NetworkErrorHandling(ex);
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[932] | 62 | public void Connect(String serverIP, int serverPort) {
|
---|
[993] | 63 | String oldIp = this.ServerIP;
|
---|
| 64 | int oldPort = this.ServerPort;
|
---|
[932] | 65 | this.ServerIP = serverIP;
|
---|
[944] | 66 | this.ServerPort = serverPort;
|
---|
[923] | 67 | Connect();
|
---|
[993] | 68 | if (oldIp != serverIP || oldPort != ServerPort)
|
---|
| 69 | ServerChanged(this, new EventArgs());
|
---|
[923] | 70 | }
|
---|
| 71 |
|
---|
[932] | 72 | public void Disconnect() {
|
---|
| 73 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[923] | 76 | private void NetworkErrorHandling(Exception e) {
|
---|
[932] | 77 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
[923] | 78 | Logging.GetInstance().Error(this.ToString(), "exception: ", e);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | #region Login
|
---|
| 82 | public event System.EventHandler<LoginCompletedEventArgs> LoginCompleted;
|
---|
| 83 | public void LoginAsync(ClientInfo clientInfo) {
|
---|
[932] | 84 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
[923] | 85 | proxy.LoginAsync(clientInfo);
|
---|
| 86 | }
|
---|
| 87 | private void proxy_LoginCompleted(object sender, LoginCompletedEventArgs e) {
|
---|
| 88 | if (e.Error == null)
|
---|
| 89 | LoginCompleted(sender, e);
|
---|
| 90 | else
|
---|
| 91 | NetworkErrorHandling(e.Error.InnerException);
|
---|
| 92 | }
|
---|
| 93 | #endregion
|
---|
| 94 |
|
---|
| 95 | #region PullJob
|
---|
| 96 | public event System.EventHandler<PullJobCompletedEventArgs> PullJobCompleted;
|
---|
| 97 | public void PullJobAsync(Guid guid) {
|
---|
[932] | 98 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
[923] | 99 | proxy.PullJobAsync(guid);
|
---|
| 100 | }
|
---|
| 101 | void proxy_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
|
---|
| 102 | if (e.Error == null)
|
---|
| 103 | PullJobCompleted(sender, e);
|
---|
| 104 | else
|
---|
| 105 | NetworkErrorHandling(e.Error);
|
---|
| 106 | }
|
---|
| 107 | #endregion
|
---|
| 108 |
|
---|
| 109 | #region SendJobResults
|
---|
| 110 | public event System.EventHandler<SendJobResultCompletedEventArgs> SendJobResultCompleted;
|
---|
| 111 | public void SendJobResultAsync(JobResult result, bool finished) {
|
---|
[932] | 112 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
[993] | 113 | proxy.SendJobResultAsync(result, finished);
|
---|
[923] | 114 | }
|
---|
| 115 | private void proxy_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
|
---|
| 116 | if (e.Error == null)
|
---|
| 117 | SendJobResultCompleted(sender, e);
|
---|
| 118 | else
|
---|
| 119 | NetworkErrorHandling(e.Error);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | #endregion
|
---|
| 123 |
|
---|
| 124 | #region Heartbeat
|
---|
| 125 |
|
---|
| 126 | public event System.EventHandler<SendHeartBeatCompletedEventArgs> SendHeartBeatCompleted;
|
---|
| 127 | public void SendHeartBeatAsync(HeartBeatData hbd) {
|
---|
[932] | 128 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
[923] | 129 | proxy.SendHeartBeatAsync(hbd);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | private void proxy_SendHeartBeatCompleted(object sender, SendHeartBeatCompletedEventArgs e) {
|
---|
| 133 | if (e.Error == null)
|
---|
| 134 | SendHeartBeatCompleted(sender, e);
|
---|
| 135 | else
|
---|
| 136 | NetworkErrorHandling(e.Error);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | #endregion
|
---|
[932] | 140 |
|
---|
[923] | 141 | }
|
---|
| 142 | }
|
---|