1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.ServiceModel;
|
---|
5 | using DistributedGA.Core.Domain;
|
---|
6 | using DistributedGA.Core.Interface;
|
---|
7 | using System.Timers;
|
---|
8 |
|
---|
9 | namespace DistributedGA.Core.Implementation
|
---|
10 | {
|
---|
11 | public class WcfPeerListManager : IPeerListManager
|
---|
12 | {
|
---|
13 | private string serverString = null;
|
---|
14 |
|
---|
15 | private IContactService client = null;
|
---|
16 |
|
---|
17 | private IContactService heartbeatClient = null;
|
---|
18 |
|
---|
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 |
|
---|
99 | }
|
---|
100 | }
|
---|