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 | ServerChanged(this, new EventArgs());
|
---|
70 | }
|
---|
71 |
|
---|
72 | public void Disconnect() {
|
---|
73 | ConnState = NetworkEnum.WcfConnState.Disconnected;
|
---|
74 | }
|
---|
75 |
|
---|
76 | private void NetworkErrorHandling(Exception e) {
|
---|
77 | ConnState = NetworkEnum.WcfConnState.Failed;
|
---|
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) {
|
---|
84 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
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) {
|
---|
98 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
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) {
|
---|
112 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
113 | proxy.SendJobResultAsync(result, finished);
|
---|
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) {
|
---|
128 | if (ConnState == NetworkEnum.WcfConnState.Connected)
|
---|
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
|
---|
140 |
|
---|
141 | }
|
---|
142 | }
|
---|