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

Last change on this file since 14009 was 14009, checked in by thasling, 5 years ago

#2615:
made minor changes,
wrong commit happende last time, so still tbd:
-sort List
-dispose of anaylzer is never called

File size: 2.7 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 Object timerLock = new Object();
19
20    private ChannelFactory<IContactService> myChannelFactory;
21
22    private IContactService client;
23
24    private List<PeerInfo> cachedPeerList;
25
26    private double communicationRate;
27
28    private Random rand;
29
30    public void Init(PeerInfo source, string contactServerUrl, double communicationRate) {
31      serverString = contactServerUrl;
32      this.communicationRate = communicationRate;
33      myself = source;
34      cachedPeerList = new List<PeerInfo>();
35      rand = new Random();
36      //Init ChannelFactory and Client
37      var binding = new NetTcpBinding();
38      var endpoint = new EndpointAddress(serverString);
39      myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);
40      client = myChannelFactory.CreateChannel();
41      //Register Peer
42      client.RegisterPeer(myself);
43      //Start heartbeat timer
44      timer = new Timer(1000 * 20); //each 20 seconds
45      timer.Elapsed += RefreshPeerList;
46      timer.Start();
47    }
48
49    public List<PeerInfo> GetPeerList() {
50      return cachedPeerList;
51    }
52
53    public void SendLogToServer(string msg) {
54      client.MakeLog(myself, msg);
55    }
56
57    public void Dispose() {
58      timer.Stop();
59      timer.Dispose();
60      ((IClientChannel)client).Close();
61      myChannelFactory.Close();
62      myChannelFactory = null;
63    }
64
65    private List<PeerInfo> ChoosePeersForMessaging(ref List<PeerInfo> allPeers) {
66      Shuffle<PeerInfo>(allPeers);
67      int toTake = Convert.ToInt32(allPeers.Count * communicationRate) + 1;
68      if (allPeers.Count > 0 && toTake == 0) {
69        toTake = 1;
70      }
71      return allPeers.Take(toTake).ToList(); ;
72    }
73
74    private void Shuffle<T>(IList<T> list) {
75      int n = list.Count;
76      while (n > 1) {
77        n--;
78        int k = rand.Next(n + 1);
79        T value = list[k];
80        list[k] = list[n];
81        list[n] = value;
82      }
83    }
84
85    private void RefreshPeerList(object sender, ElapsedEventArgs e) {
86      lock (timerLock) {
87        try {
88          var allPeers = client.GetPeerList(myself);
89          cachedPeerList = ChoosePeersForMessaging(ref allPeers);
90        }
91        catch { } //nothing to do
92      }
93    }
94
95  }
96}
Note: See TracBrowser for help on using the repository browser.