Changeset 13982
- Timestamp:
- 07/02/16 15:23:57 (8 years ago)
- Location:
- branches/thasling/DistributedGA
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/thasling/DistributedGA/DistributedGA.ContactServer/ContactServiceImpl.cs
r13943 r13982 24 24 allPeers = new ConcurrentDictionary<PeerInfo, DateTime>(); 25 25 26 timer = new Timer(1000); //each hour26 timer = new Timer(1000); //each minute 27 27 timer.Elapsed += CleanUpContactTable; 28 28 timer.Start(); … … 87 87 DateTime tmp; 88 88 if (allPeers.TryGetValue(pi, out tmp)) { 89 //if (tmp.AddHours(1f) < deadline) 90 if (tmp.AddMinutes(1) < deadline) //TODO 91 { 92 //if (tmp < deadline.AddHours(1f)) { 89 if (tmp.AddMinutes(1) < deadline) { 93 90 itemsToDelete.Add(pi); 94 91 } -
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 -
branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageSender.cs
r13972 r13982 102 102 private void SendDataFromQueue(PeerInfo destination, byte[][] data) { 103 103 try { 104 //int arrayLength = 3;105 //if (data.GetUpperBound(0) > arrayLength) {106 // //HACK: SEND MAX 10 items107 // byte[][] fake = new byte[arrayLength][];108 // for (int i = 0; i < arrayLength; i++) {109 // fake[i] = data[i];110 // }111 // data = fake;112 //}113 114 115 104 Console.WriteLine(string.Format("Sending data to {0}:{1} in the background...", destination.IpAddress, destination.Port)); 116 105 var serviceUrl = "DistributedGA.svc"; … … 130 119 using (IClientChannel client = (IClientChannel)myChannelFactory.CreateChannel()) { 131 120 for (int i = 0; i < data.GetUpperBound(0) + 1; i++) { 132 ((IMessageContract)client).SendData(myself, data[i]); //maybe timoutexception...121 ((IMessageContract)client).SendData(myself, data[i]); //maybe exception... 133 122 } 134 123 } -
branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs
r13965 r13982 24 24 private IContactService heartbeatClient; 25 25 26 private Random rand;27 26 28 private double communicationRate; //how many peers are contacted by this peer in percent29 27 30 public void Init(PeerInfo source, string contactServerUrl , double communicationRate) {28 public void Init(PeerInfo source, string contactServerUrl) { 31 29 serverString = contactServerUrl; 32 this.communicationRate = communicationRate;33 30 myself = source; 34 rand = new Random(); 35 31 36 32 //Init ChannelFactory and Clients 37 33 var binding = new NetTcpBinding(); … … 43 39 client.RegisterPeer(source); 44 40 //Start heartbeat timer 45 timer = new Timer(1000 ); //each 5 minutes41 timer = new Timer(1000 * 20); //each 20 seconds 46 42 timer.Elapsed += SendHeartbeatToServer; 47 43 timer.Start(); … … 51 47 try { 52 48 var allPeers = client.GetPeerList(myself); //maybe timout exception... 53 var peersForMessaging = ChoosePeersForMessaging(ref allPeers); 54 return peersForMessaging; 49 return allPeers; 55 50 } 56 51 catch { } //if maybe sending failed (because of connection lost, etc.): just ignore … … 72 67 } 73 68 74 75 private List<PeerInfo> ChoosePeersForMessaging(ref List<PeerInfo> allPeers) {76 Shuffle<PeerInfo>(allPeers);77 int toTake = Convert.ToInt32(allPeers.Count * communicationRate) + 1;78 if (allPeers.Count > 0 && toTake == 0) {79 toTake = 1;80 }81 return allPeers.Take(toTake).ToList(); ;82 }83 private void Shuffle<T>(IList<T> list) {84 int n = list.Count;85 while (n > 1) {86 n--;87 int k = rand.Next(n + 1);88 T value = list[k];89 list[k] = list[n];90 list[n] = value;91 }92 }93 94 69 private void SendHeartbeatToServer(object sender, ElapsedEventArgs e) { 95 70 lock (timerLock) { -
branches/thasling/DistributedGA/DistributedGA.Core/Interface/IPeerListManager.cs
r13965 r13982 6 6 public interface IPeerListManager { 7 7 8 void Init(PeerInfo source, string contactServerUrl , double communicationRate); //Registers own instance at the contact-server8 void Init(PeerInfo source, string contactServerUrl); //Registers own instance at the contact-server 9 9 10 10 List<PeerInfo> GetPeerList(); //Recieves all peers in the network from contact-server -
branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs
r13972 r13982 136 136 base.InitializeState(); 137 137 // init P2P 138 Init(); 138 if (h == null) { 139 //otherwhise old object is not disposed correctly 140 Init(); 141 } 139 142 } 140 143
Note: See TracChangeset
for help on using the changeset viewer.