1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.ServiceModel;
5: using System.ServiceModel.Discovery;
6: using System.Text;
7: using System.Threading.Tasks;
8:
9: namespace WpfApplication1
10: {
11: public class OperatorServicesAccessor
12: {
13:
14: private AnnouncementService mAnnouncementService;
15: private ServiceHost mAnnouncementServiceHost;
16: UdpAnnouncementEndpoint mUdpAnnouncementEndpoint;
17:
18: private static OperatorServicesAccessor mOperatorServicesAccessor = new OperatorServicesAccessor();
19:
20: public static OperatorServicesAccessor Instance()
21: {
22: return mOperatorServicesAccessor;
23: }
24:
25: private OperatorServicesAccessor()
26: {
27:
28: }
29:
30: public void Init()
31: {
32:
33: StartScanForeOperatorEntities();
34:
35: OpenAnnouncementService();
36: }
37:
38: /// <summary>
39: /// When the client recevied non aplication exception it close the channel
40: /// and wait for the service to start itself
41: /// </summary>
42: /// <param name="pFaultedChannel"></param>
43: /// <param name="pEx"></param>
44: public void ReportAboutFaultedClient(object pFaultedChannel, Exception pEx)
45: {
46:
47: if (pFaultedChannel == null)
48: {
49: return;
50: }
51:
52: if (pEx is ProtocolException)
53: {
54:
55: }
56:
57: if (pEx is CommunicationException)
58: {
59:
60: }
61:
62: }
63:
64: void mDiscoveryClient_FindCompleted(object sender, FindCompletedEventArgs e)
65: {
66: try
67: {
68:
69: switch (e.UserState.ToString())
70: {
71: case "IOperator":
72:
73: foreach (EndpointDiscoveryMetadata nextEndpointDiscoveryMetadata in e.Result.Endpoints)
74: {
75: EndpointAddress theAddress = nextEndpointDiscoveryMetadata.Address;
76:
77: return;
78: }
79: break;
80: }
81: }
82: catch (Exception ee)
83: {
84:
85: }
86: finally
87: {
88: ((DiscoveryClient)sender).Close();
89: }
90:
91: }
92:
93: private void StartScanForeOperatorEntities()
94: {
95:
96: UdpDiscoveryEndpoint mUdpDiscoveryEndpoint = new UdpDiscoveryEndpoint();
97:
98: DiscoveryClient mDiscoveryClient = new DiscoveryClient(mUdpDiscoveryEndpoint);
99:
100: mDiscoveryClient.FindCompleted += new EventHandler<FindCompletedEventArgs>(mDiscoveryClient_FindCompleted);
101:
102: FindCriteria criteria = new FindCriteria(typeof(IOperator));
103:
104: criteria.Duration = TimeSpan.FromSeconds(5);
105:
106: mDiscoveryClient.FindAsync(criteria, "IOperator");
107:
108: }
109:
110: private void OpenAnnouncementService()
111: {
112: mAnnouncementService = new AnnouncementService();
113: // Add event handlers
114:
115: mAnnouncementService.OnlineAnnouncementReceived +=
116: new EventHandler<AnnouncementEventArgs>(this.OnOnlineAnnouncement);
117: mAnnouncementService.OfflineAnnouncementReceived +=
118: new EventHandler<AnnouncementEventArgs>(this.OnOfflineAnnouncement);
119:
120:
121: // Create the service host with a singleton
122: mAnnouncementServiceHost = new ServiceHost(mAnnouncementService);
123: // Add the announcement endpoint
124: mUdpAnnouncementEndpoint = new UdpAnnouncementEndpoint();
125:
126: mAnnouncementServiceHost.AddServiceEndpoint(mUdpAnnouncementEndpoint);
127: // Open the host async
128: mAnnouncementServiceHost.Open();
129: }
130: /// <summary>
131: ///
132: /// Get a notification from a cell that its power up
133: /// </summary>
134: /// <param name="sender"></param>
135: /// <param name="e"></param>
136: private void OnOnlineAnnouncement(object sender, AnnouncementEventArgs e)
137: {
138:
139: }
140:
141: private void OnOfflineAnnouncement(object sender, AnnouncementEventArgs e)
142: {
143:
144: }
145: }
146: }
147: