Changeset 13982 for branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs
- Timestamp:
- 07/02/16 15:23:57 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs
r13972 r13982 28 28 private IMessageService host = null; 29 29 30 private double communicationRate; 31 private Random rand; 32 30 33 public event EventHandler<Exception> ExceptionOccurend; 31 34 … … 37 40 ProblemInstance = problemInstance 38 41 }; // TODO: get own peerinfo 42 43 this.communicationRate = communicationRate; 44 rand = new Random(); 39 45 40 46 writeQueue = new SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>>(); … … 48 54 49 55 peerListManager = new WcfPeerListManager(); 50 peerListManager.Init(ownInstance, contactServerUrl , communicationRate);56 peerListManager.Init(ownInstance, contactServerUrl); 51 57 52 58 sender = new WcfMessageSender(); … … 72 78 public void PublishDataToNetwork(byte[] data) { 73 79 try { 80 var peers = peerListManager.GetPeerList(); 74 81 foreach (PeerInfo peer in peerListManager.GetPeerList()) { 82 var peersForMessaging = ChoosePeersForMessaging(ref peers); 83 75 84 //maybe something will go wrong during network communication... 76 85 try { … … 129 138 } 130 139 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 131 149 private string GetInternalIpAddress(string ipPrefix) { 132 150 try { … … 141 159 } 142 160 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 } 143 172 } 144 173
Note: See TracChangeset
for help on using the changeset viewer.