Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2615:
fixed bug, which not raised an exception

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