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