Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14253 was 14253, checked in by gkronber, 8 years ago

bugfixing (memory leaks)

File size: 3.0 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 SendLogToServer(string msg) {
[14060]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()) {
[14061]50          ((IContactService)client).MakeLog(myself, msg);
[14060]51        }
52      }
[13923]53    }
[13887]54
[13923]55    public void Dispose() {
56      timer.Stop();
57      timer.Dispose();
58    }
[13887]59
[14253]60    private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers) {
[14009]61      Shuffle<PeerInfo>(allPeers);
62      int toTake = Convert.ToInt32(allPeers.Count * communicationRate) + 1;
63      if (allPeers.Count > 0 && toTake == 0) {
64        toTake = 1;
65      }
[14253]66      return allPeers.Take(toTake).ToList();
[14009]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) {
[13943]81      lock (timerLock) {
82        try {
[14060]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()) {
[14061]88              allPeers = ((IContactService)client).GetPeerList(myself);
[14060]89            }
90          }
[14253]91          cachedPeerList = ChoosePeersForMessaging(allPeers);
[13943]92        }
93        catch { } //nothing to do
[13923]94      }
95    }
[13887]96
[13923]97  }
[13524]98}
Note: See TracBrowser for help on using the repository browser.