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 | public class WcfPeerListManager : IPeerListManager {
|
---|
11 |
|
---|
12 | private string serverString = null;
|
---|
13 |
|
---|
14 | private PeerInfo myself = null;
|
---|
15 |
|
---|
16 | private Timer timer = null; //sends heartbeat to contact-server
|
---|
17 |
|
---|
18 | private ChannelFactory<IContactService> myChannelFactory;
|
---|
19 |
|
---|
20 | private IContactService client;
|
---|
21 |
|
---|
22 | private IContactService heartbeatClient;
|
---|
23 |
|
---|
24 | public void Init(PeerInfo source, string contactServerUrl) {
|
---|
25 | serverString = contactServerUrl;
|
---|
26 | myself = source;
|
---|
27 | //Init ChannelFactory and Clients
|
---|
28 | var binding = new NetTcpBinding();
|
---|
29 | var endpoint = new EndpointAddress(serverString);
|
---|
30 | myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);
|
---|
31 | client = myChannelFactory.CreateChannel();
|
---|
32 | heartbeatClient = myChannelFactory.CreateChannel();
|
---|
33 | //Register Peer
|
---|
34 | client.RegisterPeer(source);
|
---|
35 | //Start heartbeat timer
|
---|
36 | timer = new Timer(1000); //each 5 minutes
|
---|
37 | timer.Elapsed += SendHeartbeatToServer;
|
---|
38 | timer.Start();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public List<PeerInfo> GetPeerList() {
|
---|
42 | try {
|
---|
43 | var allPeers = client.GetPeerList(myself); //maybe timout exception...
|
---|
44 | var peersForMessaging = ChoosePeersForMessaging(allPeers);
|
---|
45 | //return peersForMessaging;
|
---|
46 | return allPeers; //TODO: Enable 10% list communication
|
---|
47 | }
|
---|
48 | catch { } //if maybe sending failed (because of connection lost, etc.): just ignore
|
---|
49 | return new List<PeerInfo>();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void SendLogToServer(string msg) {
|
---|
53 | client.MakeLog(myself, msg);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void Dispose() {
|
---|
57 | timer.Stop();
|
---|
58 | timer.Dispose();
|
---|
59 | ((IClientChannel)client).Close();
|
---|
60 | ((IClientChannel)heartbeatClient).Close();
|
---|
61 | myChannelFactory.Close();
|
---|
62 | client = null;
|
---|
63 | myChannelFactory = null;
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers) {
|
---|
68 | //communicate with 10% of the network
|
---|
69 | int noOfPeers = allPeers.Count / 10;
|
---|
70 | List<int> indexList = GetRandomItemIndexes(noOfPeers, 0, allPeers.Count - 1);
|
---|
71 | List<PeerInfo> res = new List<PeerInfo>();
|
---|
72 | foreach (int index in indexList) {
|
---|
73 | res.Add(allPeers.ElementAt(index));
|
---|
74 | }
|
---|
75 | return res;
|
---|
76 | }
|
---|
77 |
|
---|
78 | private List<int> GetRandomItemIndexes(int noOfItems, int minValue, int maxValue) {
|
---|
79 | List<int> res = new List<int>();
|
---|
80 | Random rnd = new Random();
|
---|
81 | int tmp = -1;
|
---|
82 | while (res.Count < noOfItems) {
|
---|
83 | tmp = rnd.Next(minValue, maxValue);
|
---|
84 | if (!res.Contains(tmp)) {
|
---|
85 | res.Add(tmp);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | return res;
|
---|
89 | }
|
---|
90 |
|
---|
91 | private void SendHeartbeatToServer(object sender, ElapsedEventArgs e) {
|
---|
92 | try {
|
---|
93 | heartbeatClient.UpdateHeartbeat(myself);
|
---|
94 | }
|
---|
95 | catch { } //nothing to do
|
---|
96 | }
|
---|
97 |
|
---|
98 | }
|
---|
99 | }
|
---|