[13538] | 1 | using System;
|
---|
[13524] | 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.ServiceModel;
|
---|
[13538] | 5 | using DistributedGA.Core.Domain;
|
---|
| 6 | using DistributedGA.Core.Interface;
|
---|
[13524] | 7 |
|
---|
[13538] | 8 | namespace DistributedGA.Core.Implementation {
|
---|
| 9 | public class WcfPeerListManager : IPeerListManager {
|
---|
[13541] | 10 | private string serverString = null;
|
---|
[13524] | 11 |
|
---|
[13538] | 12 | private IContactService client = null;
|
---|
[13524] | 13 |
|
---|
[13538] | 14 | private PeerInfo myself = null;
|
---|
[13524] | 15 |
|
---|
[13556] | 16 | public void Init(PeerInfo source, string contactServerUrl) {
|
---|
| 17 | serverString = contactServerUrl;
|
---|
[13538] | 18 | client = CreateClient();
|
---|
| 19 | myself = source;
|
---|
| 20 | client.RegisterPeer(source);
|
---|
| 21 | }
|
---|
[13524] | 22 |
|
---|
[13538] | 23 | public List<PeerInfo> GetPeerList() {
|
---|
[13554] | 24 | var allPeers = client.GetPeerList(myself);
|
---|
| 25 | var peersForMessaging = ChoosePeersForMessaging(allPeers);
|
---|
[13538] | 26 | //return peersForMessaging;
|
---|
| 27 | return allPeers; //TODO: Enable 10% list communication
|
---|
| 28 | }
|
---|
[13524] | 29 |
|
---|
[13538] | 30 | public void SendLogToServer(string msg) {
|
---|
| 31 | client.MakeLog(myself, msg);
|
---|
| 32 | }
|
---|
[13524] | 33 |
|
---|
[13538] | 34 | private IContactService CreateClient() {
|
---|
| 35 | var binding = new BasicHttpBinding();
|
---|
[13541] | 36 | var endpoint = new EndpointAddress(serverString);
|
---|
[13538] | 37 | var myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);
|
---|
[13524] | 38 |
|
---|
[13538] | 39 | IContactService client = null;
|
---|
| 40 | client = myChannelFactory.CreateChannel();
|
---|
| 41 | return client;
|
---|
| 42 | }
|
---|
[13524] | 43 |
|
---|
[13538] | 44 | private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers) {
|
---|
| 45 | //communicate with 10% of the network
|
---|
| 46 | int noOfPeers = allPeers.Count / 10;
|
---|
[13541] | 47 | List<int> indexList = GetRandomItemIndexes(noOfPeers, 0, allPeers.Count - 1);
|
---|
[13538] | 48 | List<PeerInfo> res = new List<PeerInfo>();
|
---|
[13541] | 49 | foreach (int index in indexList) {
|
---|
[13538] | 50 | res.Add(allPeers.ElementAt(index));
|
---|
| 51 | }
|
---|
| 52 | return allPeers;
|
---|
| 53 | }
|
---|
[13524] | 54 |
|
---|
[13541] | 55 | private List<int> GetRandomItemIndexes(int noOfItems, int minValue, int maxValue) {
|
---|
| 56 | List<int> res = new List<int>();
|
---|
[13538] | 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);
|
---|
[13528] | 63 | }
|
---|
[13538] | 64 | }
|
---|
| 65 | return res;
|
---|
| 66 | }
|
---|
[13528] | 67 |
|
---|
[13538] | 68 | }
|
---|
[13524] | 69 | }
|
---|