Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 978 was 949, checked in by kgrading, 16 years ago

implemented changes for #401

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