Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/06/16 16:47:47 (8 years ago)
Author:
thasling
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs

    r13982 r14009  
    2222    private IContactService client;
    2323
    24     private IContactService heartbeatClient;
     24    private List<PeerInfo> cachedPeerList;
    2525
     26    private double communicationRate;
    2627
     28    private Random rand;
    2729
    28     public void Init(PeerInfo source, string contactServerUrl) {
     30    public void Init(PeerInfo source, string contactServerUrl, double communicationRate) {
    2931      serverString = contactServerUrl;
     32      this.communicationRate = communicationRate;
    3033      myself = source;
    31 
    32       //Init ChannelFactory and Clients
     34      cachedPeerList = new List<PeerInfo>();
     35      rand = new Random();
     36      //Init ChannelFactory and Client
    3337      var binding = new NetTcpBinding();
    3438      var endpoint = new EndpointAddress(serverString);
    3539      myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);
    3640      client = myChannelFactory.CreateChannel();
    37       heartbeatClient = myChannelFactory.CreateChannel();
    3841      //Register Peer
    39       client.RegisterPeer(source);
     42      client.RegisterPeer(myself);
    4043      //Start heartbeat timer
    4144      timer = new Timer(1000 * 20); //each 20 seconds
    42       timer.Elapsed += SendHeartbeatToServer;
     45      timer.Elapsed += RefreshPeerList;
    4346      timer.Start();
    4447    }
    4548
    4649    public List<PeerInfo> GetPeerList() {
    47       try {
    48         var allPeers = client.GetPeerList(myself); //maybe timout exception...
    49         return allPeers;
    50       }
    51       catch { } //if maybe sending failed (because of connection lost, etc.): just ignore
    52       return new List<PeerInfo>();
     50      return cachedPeerList;
    5351    }
    5452
     
    6159      timer.Dispose();
    6260      ((IClientChannel)client).Close();
    63       ((IClientChannel)heartbeatClient).Close();
    6461      myChannelFactory.Close();
    65       client = null;
    6662      myChannelFactory = null;
    6763    }
    6864
    69     private void SendHeartbeatToServer(object sender, ElapsedEventArgs e) {
     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) {
    7086      lock (timerLock) {
    7187        try {
    72           heartbeatClient.UpdateHeartbeat(myself);
     88          var allPeers = client.GetPeerList(myself);
     89          cachedPeerList = ChoosePeersForMessaging(ref allPeers);
    7390        }
    7491        catch { } //nothing to do
Note: See TracChangeset for help on using the changeset viewer.