Changeset 13887 for branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs
- Timestamp:
- 06/08/16 17:02:06 (8 years ago)
- Location:
- branches/thasling/DistributedGA/DistributedGA.Core
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/thasling/DistributedGA/DistributedGA.Core
-
Property
svn:global-ignores
set to
obj
-
Property
svn:global-ignores
set to
-
branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs
r13557 r13887 5 5 using DistributedGA.Core.Domain; 6 6 using DistributedGA.Core.Interface; 7 using System.Timers; 7 8 8 namespace DistributedGA.Core.Implementation { 9 public class WcfPeerListManager : IPeerListManager { 10 private string serverString = null; 9 namespace DistributedGA.Core.Implementation 10 { 11 public class WcfPeerListManager : IPeerListManager 12 { 13 private string serverString = null; 11 14 12 private IContactService client = null;15 private IContactService client = null; 13 16 14 private PeerInfo myself= null;17 private IContactService heartbeatClient = null; 15 18 16 public void Init(PeerInfo source, string contactServerUrl) { 17 serverString = contactServerUrl; 18 client = CreateClient(); 19 myself = source; 20 client.RegisterPeer(source); 19 private PeerInfo myself = null; 20 21 private Timer timer = null; //sends heartbeat to contact-server 22 23 public void Init(PeerInfo source, string contactServerUrl) 24 { 25 serverString = contactServerUrl; 26 client = CreateClient(); 27 heartbeatClient = CreateClient(); 28 myself = source; 29 client.RegisterPeer(source); 30 timer = new Timer(1000 * 60 * 5); //each 5 minutes 31 timer.Elapsed += SendHeartbeatToServer; 32 timer.Start(); 33 } 34 35 36 37 public List<PeerInfo> GetPeerList() 38 { 39 var allPeers = client.GetPeerList(myself); 40 var peersForMessaging = ChoosePeersForMessaging(allPeers); 41 //return peersForMessaging; 42 return allPeers; //TODO: Enable 10% list communication 43 } 44 45 public void SendLogToServer(string msg) 46 { 47 client.MakeLog(myself, msg); 48 } 49 50 private IContactService CreateClient() 51 { 52 var binding = new NetTcpBinding(); 53 var endpoint = new EndpointAddress(serverString); 54 var myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint); 55 56 IContactService client = null; 57 client = myChannelFactory.CreateChannel(); 58 return client; 59 } 60 61 private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers) 62 { 63 //communicate with 10% of the network 64 int noOfPeers = allPeers.Count / 10; 65 List<int> indexList = GetRandomItemIndexes(noOfPeers, 0, allPeers.Count - 1); 66 List<PeerInfo> res = new List<PeerInfo>(); 67 foreach (int index in indexList) 68 { 69 res.Add(allPeers.ElementAt(index)); 70 } 71 return res; 72 } 73 74 private List<int> GetRandomItemIndexes(int noOfItems, int minValue, int maxValue) 75 { 76 List<int> res = new List<int>(); 77 Random rnd = new Random(); 78 int tmp = -1; 79 while (res.Count < noOfItems) 80 { 81 tmp = rnd.Next(minValue, maxValue); 82 if (!res.Contains(tmp)) 83 { 84 res.Add(tmp); 85 } 86 } 87 return res; 88 } 89 90 private void SendHeartbeatToServer(object sender, ElapsedEventArgs e) 91 { 92 try 93 { 94 heartbeatClient.UpdateHeartbeat(myself); 95 } 96 catch { } //nothing to do, exception is raised when getting peer list 97 } 98 21 99 } 22 23 public List<PeerInfo> GetPeerList() {24 var allPeers = client.GetPeerList(myself);25 var peersForMessaging = ChoosePeersForMessaging(allPeers);26 //return peersForMessaging;27 return allPeers; //TODO: Enable 10% list communication28 }29 30 public void SendLogToServer(string msg) {31 client.MakeLog(myself, msg);32 }33 34 private IContactService CreateClient() {35 var binding = new NetTcpBinding();36 var endpoint = new EndpointAddress(serverString);37 var myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);38 39 IContactService client = null;40 client = myChannelFactory.CreateChannel();41 return client;42 }43 44 private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers) {45 //communicate with 10% of the network46 int noOfPeers = allPeers.Count / 10;47 List<int> indexList = GetRandomItemIndexes(noOfPeers, 0, allPeers.Count - 1);48 List<PeerInfo> res = new List<PeerInfo>();49 foreach (int index in indexList) {50 res.Add(allPeers.ElementAt(index));51 }52 return allPeers;53 }54 55 private List<int> GetRandomItemIndexes(int noOfItems, int minValue, int maxValue) {56 List<int> res = new List<int>();57 Random rnd = new Random();58 int tmp = -1;59 while (res.Count < noOfItems) {60 tmp = rnd.Next(minValue, maxValue);61 if (!res.Contains(tmp)) {62 res.Add(tmp);63 }64 }65 return res;66 }67 68 }69 100 }
Note: See TracChangeset
for help on using the changeset viewer.