using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.ServiceModel; using DistributedGA.Core.Domain; using DistributedGA.Core.Interface; namespace DistributedGA.Core.Implementation { public class WcfPeerListManager : IPeerListManager { private string serverString = null; private IContactService client = null; private PeerInfo myself = null; public void Init(PeerInfo source) { serverString = ConfigurationManager.AppSettings["ContactServerURL"]; client = CreateClient(); myself = source; client.RegisterPeer(source); } public List GetPeerList() { var allPeers = client.GetPeerList(myself); var peersForMessaging = ChoosePeersForMessaging(allPeers); //return peersForMessaging; return allPeers; //TODO: Enable 10% list communication } public void SendLogToServer(string msg) { client.MakeLog(myself, msg); } private IContactService CreateClient() { var binding = new BasicHttpBinding(); var endpoint = new EndpointAddress(serverString); var myChannelFactory = new ChannelFactory(binding, endpoint); IContactService client = null; client = myChannelFactory.CreateChannel(); return client; } private List ChoosePeersForMessaging(List allPeers) { //communicate with 10% of the network int noOfPeers = allPeers.Count / 10; List indexList = GetRandomItemIndexes(noOfPeers, 0, allPeers.Count - 1); List res = new List(); foreach (int index in indexList) { res.Add(allPeers.ElementAt(index)); } return allPeers; } private List GetRandomItemIndexes(int noOfItems, int minValue, int maxValue) { List res = new List(); Random rnd = new Random(); int tmp = -1; while (res.Count < noOfItems) { tmp = rnd.Next(minValue, maxValue); if (!res.Contains(tmp)) { res.Add(tmp); } } return res; } } }