source: trunk/sources/HeuristicLab.Hive.Client.Communication/WcfService.cs @ 924

Last change on this file since 924 was 924, checked in by kgrading, 13 years ago

added a connectionRestored Event (#418)

File size: 4.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.ServiceModel;
6using HeuristicLab.Hive.Contracts.Interfaces;
7using HeuristicLab.Hive.Contracts;
8using HeuristicLab.Hive.Contracts.BusinessObjects;
9using HeuristicLab.Hive.Client.Common;
10
11
12namespace 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 enum ConnectionState { connected, disconnected, failed } 
25    public ConnectionState ConnState { get; set; }
26
27    public event EventHandler ConnectionRestored;   
28
29    private ClientCommunicatorClient proxy = null;
30    private string serverIP;
31    private string serverPort;
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 == ConnectionState.failed)
50          ConnectionRestored(this, new EventArgs());
51        ConnState = ConnectionState.connected;
52
53      }
54      catch (Exception ex) {
55        NetworkErrorHandling(ex);
56      }
57    }
58
59    public void Connect(String serverIP, String serverPort) {
60      this.serverIP = serverIP;
61      this.serverPort = serverPort;
62      Connect();
63    }
64
65    private void NetworkErrorHandling(Exception e) {
66      ConnState = ConnectionState.failed;
67      Logging.GetInstance().Error(this.ToString(), "exception: ", e);
68    }
69
70    #region Login
71    public event System.EventHandler<LoginCompletedEventArgs> LoginCompleted;
72    public void LoginAsync(ClientInfo clientInfo) {
73      if (ConnState == ConnectionState.connected)
74        proxy.LoginAsync(clientInfo);
75    }
76    private void proxy_LoginCompleted(object sender, LoginCompletedEventArgs e) {
77      if (e.Error == null)
78        LoginCompleted(sender, e);
79      else
80        NetworkErrorHandling(e.Error.InnerException);
81    }
82    #endregion
83
84    #region PullJob
85    public event System.EventHandler<PullJobCompletedEventArgs> PullJobCompleted;
86    public void PullJobAsync(Guid guid) {
87      if (ConnState == ConnectionState.connected)
88        proxy.PullJobAsync(guid);
89    }
90    void proxy_PullJobCompleted(object sender, PullJobCompletedEventArgs e) {
91      if (e.Error == null)
92        PullJobCompleted(sender, e);
93      else
94        NetworkErrorHandling(e.Error);
95    }
96    #endregion
97
98    #region SendJobResults
99    public event System.EventHandler<SendJobResultCompletedEventArgs> SendJobResultCompleted;
100    public void SendJobResultAsync(JobResult result, bool finished) {
101      if (ConnState == ConnectionState.connected)
102        proxy.SendJobResult(result, finished);
103    }
104    private void proxy_SendJobResultCompleted(object sender, SendJobResultCompletedEventArgs e) {
105      if (e.Error == null)
106        SendJobResultCompleted(sender, e);
107      else
108        NetworkErrorHandling(e.Error);
109    }
110
111    #endregion
112
113    #region Heartbeat
114
115    public event System.EventHandler<SendHeartBeatCompletedEventArgs> SendHeartBeatCompleted;
116    public void SendHeartBeatAsync(HeartBeatData hbd) {
117      if (ConnState == ConnectionState.connected)
118        proxy.SendHeartBeatAsync(hbd);
119    }
120
121    private void proxy_SendHeartBeatCompleted(object sender, SendHeartBeatCompletedEventArgs e) {
122      if (e.Error == null)
123        SendHeartBeatCompleted(sender, e);
124      else
125        NetworkErrorHandling(e.Error);
126    }
127
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.