Free cookie consent management tool by TermsFeed Policy Generator

source: branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs @ 13923

Last change on this file since 13923 was 13923, checked in by thasling, 8 years ago

#2615:
re-implemented WcfPeerListManager
also sending messages is now done in the background async

File size: 3.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.ServiceModel;
5using DistributedGA.Core.Domain;
6using DistributedGA.Core.Interface;
7using System.Timers;
8
9namespace DistributedGA.Core.Implementation {
10  public class WcfPeerListManager : IPeerListManager {
11
12    private string serverString = null;
13
14    private PeerInfo myself = null;
15
16    private Timer timer = null; //sends heartbeat to contact-server
17
18    private ChannelFactory<IContactService> myChannelFactory;
19
20    private IContactService client;
21
22    private IContactService heartbeatClient;
23
24    public void Init(PeerInfo source, string contactServerUrl) {
25      serverString = contactServerUrl;
26      myself = source;
27      //Init ChannelFactory and Clients
28      var binding = new NetTcpBinding();
29      var endpoint = new EndpointAddress(serverString);
30      myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);
31      client = myChannelFactory.CreateChannel();
32      heartbeatClient = myChannelFactory.CreateChannel();
33      //Register Peer
34      client.RegisterPeer(source);
35      //Start heartbeat timer
36      timer = new Timer(1000); //each 5 minutes
37      timer.Elapsed += SendHeartbeatToServer;
38      timer.Start();
39    }
40
41    public List<PeerInfo> GetPeerList() {
42      try {
43        var allPeers = client.GetPeerList(myself); //maybe timout exception...
44        var peersForMessaging = ChoosePeersForMessaging(allPeers);
45        //return peersForMessaging;
46        return allPeers; //TODO: Enable 10% list communication
47      }
48      catch { } //if maybe sending failed (because of connection lost, etc.): just ignore
49      return new List<PeerInfo>();
50    }
51
52    public void SendLogToServer(string msg) {
53      client.MakeLog(myself, msg);
54    }
55
56    public void Dispose() {
57      timer.Stop();
58      timer.Dispose();
59      ((IClientChannel)client).Close();
60      ((IClientChannel)heartbeatClient).Close();
61      myChannelFactory.Close();
62    }
63
64
65    private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers) {
66      //communicate with 10% of the network
67      int noOfPeers = allPeers.Count / 10;
68      List<int> indexList = GetRandomItemIndexes(noOfPeers, 0, allPeers.Count - 1);
69      List<PeerInfo> res = new List<PeerInfo>();
70      foreach (int index in indexList) {
71        res.Add(allPeers.ElementAt(index));
72      }
73      return res;
74    }
75
76    private List<int> GetRandomItemIndexes(int noOfItems, int minValue, int maxValue) {
77      List<int> res = new List<int>();
78      Random rnd = new Random();
79      int tmp = -1;
80      while (res.Count < noOfItems) {
81        tmp = rnd.Next(minValue, maxValue);
82        if (!res.Contains(tmp)) {
83          res.Add(tmp);
84        }
85      }
86      return res;
87    }
88
89    private void SendHeartbeatToServer(object sender, ElapsedEventArgs e) {
90      try {
91        heartbeatClient.UpdateHeartbeat(myself);
92      }
93      catch { } //nothing to do
94    }
95
96  }
97}
Note: See TracBrowser for help on using the repository browser.