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 | public event EventHandler Connected;
|
---|
32 |
|
---|
33 | public ClientCommunicatorClient proxy = null;
|
---|
34 |
|
---|
35 | private WcfService() {
|
---|
36 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
37 | }
|
---|
38 | public void Connect() {
|
---|
39 | try {
|
---|
40 | proxy = new ClientCommunicatorClient(
|
---|
41 | new NetTcpBinding(),
|
---|
42 | new EndpointAddress("net.tcp://" + ServerIP + ":" + ServerPort + "/HiveServer/ClientCommunicator")
|
---|
43 | );
|
---|
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);
|
---|
49 | proxy.Open();
|
---|
50 |
|
---|
51 | ConnState = NetworkEnum.WcfConnState.Connected;
|
---|
52 | ConnectedSince = DateTime.Now;
|
---|
53 |
|
---|
54 | if (Connected != null)
|
---|
55 | Connected(this, new EventArgs());
|
---|
56 |
|
---|
57 | if (ConnState == NetworkEnum.WcfConnState.Failed)
|
---|
58 | ConnectionRestored(this, new EventArgs());
|
---|
59 | }
|
---|
60 | catch (Exception ex) {
|
---|
61 | NetworkErrorHandling(ex);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void Connect(String serverIP, int serverPort) {
|
---|
66 | String oldIp = this.ServerIP;
|
---|
67 | int oldPort = this.ServerPort;
|
---|
68 | this.ServerIP = serverIP;
|
---|
69 | this.ServerPort = serverPort;
|
---|
70 | Connect();
|
---|
71 | if (oldIp != serverIP || oldPort != ServerPort)
|
---|
72 | if(ServerChanged != null)
|
---|
73 | ServerChanged(this, new EventArgs());
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void Disconnect() {
|
---|
77 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
78 | }
|
---|
79 |
|
---|
80 | private void NetworkErrorHandling(Exception e) {
|
---|
81 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
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) {
|
---|
88 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
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 | }
|
---|
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 |
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | #region PullJob
|
---|
113 | public event System.EventHandler<PullJobCompletedEventArgs> PullJobCompleted;
|
---|
114 | public void PullJobAsync(Guid guid) {
|
---|
115 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
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;
|
---|
128 | public void SendJobResultAsync(Guid clientId,
|
---|
129 | long jobId,
|
---|
130 | byte[] result,
|
---|
131 | Exception exception,
|
---|
132 | bool finished) {
|
---|
133 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
134 | proxy.SendJobResultAsync(clientId,
|
---|
135 | jobId,
|
---|
136 | result,
|
---|
137 | exception,
|
---|
138 | finished);
|
---|
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) {
|
---|
153 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
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 |
|
---|
164 | #endregion
|
---|
165 | }
|
---|
166 | }
|
---|