Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14009 was 14009, checked in by thasling, 8 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
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;
[13524]8
[13923]9namespace DistributedGA.Core.Implementation {
10  public class WcfPeerListManager : IPeerListManager {
[13524]11
[13923]12    private string serverString = null;
[13524]13
[13923]14    private PeerInfo myself = null;
[13524]15
[13923]16    private Timer timer = null; //sends heartbeat to contact-server
[13524]17
[13947]18    private Object timerLock = new Object();
[13943]19
[13923]20    private ChannelFactory<IContactService> myChannelFactory;
[13524]21
[13923]22    private IContactService client;
[13524]23
[14009]24    private List<PeerInfo> cachedPeerList;
[13524]25
[14009]26    private double communicationRate;
[13956]27
[14009]28    private Random rand;
[13965]29
[14009]30    public void Init(PeerInfo source, string contactServerUrl, double communicationRate) {
[13923]31      serverString = contactServerUrl;
[14009]32      this.communicationRate = communicationRate;
[13923]33      myself = source;
[14009]34      cachedPeerList = new List<PeerInfo>();
35      rand = new Random();
36      //Init ChannelFactory and Client
[13923]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
[14009]42      client.RegisterPeer(myself);
[13923]43      //Start heartbeat timer
[13982]44      timer = new Timer(1000 * 20); //each 20 seconds
[14009]45      timer.Elapsed += RefreshPeerList;
[13923]46      timer.Start();
47    }
[13524]48
[13923]49    public List<PeerInfo> GetPeerList() {
[14009]50      return cachedPeerList;
[13923]51    }
[13524]52
[13923]53    public void SendLogToServer(string msg) {
54      client.MakeLog(myself, msg);
55    }
[13887]56
[13923]57    public void Dispose() {
58      timer.Stop();
59      timer.Dispose();
60      ((IClientChannel)client).Close();
61      myChannelFactory.Close();
[13937]62      myChannelFactory = null;
[13923]63    }
[13887]64
[14009]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) {
[13943]86      lock (timerLock) {
87        try {
[14009]88          var allPeers = client.GetPeerList(myself);
89          cachedPeerList = ChoosePeersForMessaging(ref allPeers);
[13943]90        }
91        catch { } //nothing to do
[13923]92      }
93    }
[13887]94
[13923]95  }
[13524]96}
Note: See TracBrowser for help on using the repository browser.