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