1 | using DistributedGA.Core.Domain;
|
---|
2 | using DistributedGA.Core.Interface;
|
---|
3 | using System;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Linq;
|
---|
6 | using System.ServiceModel;
|
---|
7 | using System.Text;
|
---|
8 | using System.Threading.Tasks;
|
---|
9 |
|
---|
10 | namespace DistributedGA.Core.Implementation
|
---|
11 | {
|
---|
12 | public class WcfPeerListManager : IPeerListManager
|
---|
13 | {
|
---|
14 | private const string SERVER_STRING = "http://localhost:9090/DistributedGA.ContactServer/ContactService";
|
---|
15 |
|
---|
16 | private IContactService client = null;
|
---|
17 |
|
---|
18 | private PeerInfo myself = null;
|
---|
19 |
|
---|
20 | List<PeerInfo> allPeers = null;
|
---|
21 | List<PeerInfo> peersForMessaging = null;
|
---|
22 |
|
---|
23 | public void Init(PeerInfo source)
|
---|
24 | {
|
---|
25 | client = CreateClient();
|
---|
26 | myself = source;
|
---|
27 | client.RegisterPeer(source);
|
---|
28 | }
|
---|
29 |
|
---|
30 | public List<PeerInfo> GetPeerList()
|
---|
31 | {
|
---|
32 | //TODO: maybe always request a new list from the contact server so that he can refresh the heartbeat...
|
---|
33 | if (allPeers == null)
|
---|
34 | {
|
---|
35 | allPeers = client.GetPeerList(myself);
|
---|
36 | peersForMessaging = ChoosePeersForMessaging(allPeers);
|
---|
37 | }
|
---|
38 | //return peersForMessaging;
|
---|
39 | return allPeers; //TODO: Enable 10% list communication
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void SendLogToServer(string msg)
|
---|
43 | {
|
---|
44 | client.MakeLog(myself, msg);
|
---|
45 | }
|
---|
46 |
|
---|
47 | private IContactService CreateClient()
|
---|
48 | {
|
---|
49 | var binding = new BasicHttpBinding();
|
---|
50 | var endpoint = new EndpointAddress(SERVER_STRING);
|
---|
51 | var myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);
|
---|
52 |
|
---|
53 | IContactService client = null;
|
---|
54 | client = myChannelFactory.CreateChannel();
|
---|
55 | return client;
|
---|
56 | }
|
---|
57 |
|
---|
58 | private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers)
|
---|
59 | {
|
---|
60 | //communicate with 10% of the network
|
---|
61 | int noOfPeers = allPeers.Count / 10;
|
---|
62 | List<Int32> indexList = GetRandomItemIndexes(noOfPeers, 0, allPeers.Count - 1);
|
---|
63 | List<PeerInfo> res = new List<PeerInfo>();
|
---|
64 | foreach (Int32 index in indexList)
|
---|
65 | {
|
---|
66 | res.Add(allPeers.ElementAt(index));
|
---|
67 | }
|
---|
68 | return allPeers;
|
---|
69 | }
|
---|
70 |
|
---|
71 | private List<Int32> GetRandomItemIndexes(int noOfItems, int minValue, int maxValue)
|
---|
72 | {
|
---|
73 | List<Int32> res = new List<Int32>();
|
---|
74 | Random rnd = new Random();
|
---|
75 | int tmp = -1;
|
---|
76 | while (res.Count < noOfItems)
|
---|
77 | {
|
---|
78 | tmp = rnd.Next(minValue, maxValue);
|
---|
79 | if (!res.Contains(tmp))
|
---|
80 | {
|
---|
81 | res.Add(tmp);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | return res;
|
---|
85 | }
|
---|
86 |
|
---|
87 | }
|
---|
88 | }
|
---|