Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2615:
PeerList is now recieved each 10 seconds instead of 20 seconds
PeerList is also recieved at Init-Call for the first time
changed data type of some parameters of P2PMigrationAnalyzer

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