Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/02/16 15:23:57 (8 years ago)
Author:
thasling
Message:

#2615:
made minor changes

File:
1 edited

Legend:

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

    r13972 r13982  
    2828    private IMessageService host = null;
    2929
     30    private double communicationRate;
     31    private Random rand;
     32
    3033    public event EventHandler<Exception> ExceptionOccurend;
    3134
     
    3740          ProblemInstance = problemInstance
    3841        }; // TODO: get own peerinfo
     42
     43        this.communicationRate = communicationRate;
     44        rand = new Random();
    3945
    4046        writeQueue = new SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>>();
     
    4854
    4955        peerListManager = new WcfPeerListManager();
    50         peerListManager.Init(ownInstance, contactServerUrl, communicationRate);
     56        peerListManager.Init(ownInstance, contactServerUrl);
    5157
    5258        sender = new WcfMessageSender();
     
    7278    public void PublishDataToNetwork(byte[] data) {
    7379      try {
     80        var peers = peerListManager.GetPeerList();
    7481        foreach (PeerInfo peer in peerListManager.GetPeerList()) {
     82          var peersForMessaging = ChoosePeersForMessaging(ref peers);
     83
    7584          //maybe something will go wrong during network communication...
    7685          try {
     
    129138    }
    130139
     140    private List<PeerInfo> ChoosePeersForMessaging(ref List<PeerInfo> allPeers) {
     141      Shuffle<PeerInfo>(allPeers);
     142      int toTake = Convert.ToInt32(allPeers.Count * communicationRate) + 1;
     143      if (allPeers.Count > 0 && toTake == 0) {
     144        toTake = 1;
     145      }
     146      return allPeers.Take(toTake).ToList(); ;
     147    }
     148
    131149    private string GetInternalIpAddress(string ipPrefix) {
    132150      try {
     
    141159      }
    142160      catch { return null; }
     161    }
     162
     163    private void Shuffle<T>(IList<T> list) {
     164      int n = list.Count;
     165      while (n > 1) {
     166        n--;
     167        int k = rand.Next(n + 1);
     168        T value = list[k];
     169        list[k] = list[n];
     170        list[n] = value;
     171      }
    143172    }
    144173
Note: See TracChangeset for help on using the changeset viewer.