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