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 Object timerLock = new Object();
|
---|
19 |
|
---|
20 | private ChannelFactory<IContactService> myChannelFactory;
|
---|
21 |
|
---|
22 | private IContactService client;
|
---|
23 |
|
---|
24 | private IContactService heartbeatClient;
|
---|
25 |
|
---|
26 | private Random rand;
|
---|
27 |
|
---|
28 | private double communicationRate; //how many peers are contacted by this peer in percent
|
---|
29 |
|
---|
30 | public void Init(PeerInfo source, string contactServerUrl, double communicationRate) {
|
---|
31 | serverString = contactServerUrl;
|
---|
32 | this.communicationRate = communicationRate;
|
---|
33 | myself = source;
|
---|
34 | rand = new Random();
|
---|
35 |
|
---|
36 | //Init ChannelFactory and Clients
|
---|
37 | var binding = new NetTcpBinding();
|
---|
38 | var endpoint = new EndpointAddress(serverString);
|
---|
39 | myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);
|
---|
40 | client = myChannelFactory.CreateChannel();
|
---|
41 | heartbeatClient = myChannelFactory.CreateChannel();
|
---|
42 | //Register Peer
|
---|
43 | client.RegisterPeer(source);
|
---|
44 | //Start heartbeat timer
|
---|
45 | timer = new Timer(1000); //each 5 minutes
|
---|
46 | timer.Elapsed += SendHeartbeatToServer;
|
---|
47 | timer.Start();
|
---|
48 | }
|
---|
49 |
|
---|
50 | public List<PeerInfo> GetPeerList() {
|
---|
51 | try {
|
---|
52 | var allPeers = client.GetPeerList(myself); //maybe timout exception...
|
---|
53 | var peersForMessaging = ChoosePeersForMessaging(ref allPeers);
|
---|
54 | return peersForMessaging;
|
---|
55 | }
|
---|
56 | catch { } //if maybe sending failed (because of connection lost, etc.): just ignore
|
---|
57 | return new List<PeerInfo>();
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void SendLogToServer(string msg) {
|
---|
61 | client.MakeLog(myself, msg);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void Dispose() {
|
---|
65 | timer.Stop();
|
---|
66 | timer.Dispose();
|
---|
67 | ((IClientChannel)client).Close();
|
---|
68 | ((IClientChannel)heartbeatClient).Close();
|
---|
69 | myChannelFactory.Close();
|
---|
70 | client = null;
|
---|
71 | myChannelFactory = null;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | private List<PeerInfo> ChoosePeersForMessaging(ref List<PeerInfo> allPeers) {
|
---|
76 | Shuffle<PeerInfo>(allPeers);
|
---|
77 | int toTake = Convert.ToInt32(allPeers.Count * communicationRate) + 1;
|
---|
78 | if (allPeers.Count > 0 && toTake == 0) {
|
---|
79 | toTake = 1;
|
---|
80 | }
|
---|
81 | return allPeers.Take(toTake).ToList(); ;
|
---|
82 | }
|
---|
83 | private void Shuffle<T>(IList<T> list) {
|
---|
84 | int n = list.Count;
|
---|
85 | while (n > 1) {
|
---|
86 | n--;
|
---|
87 | int k = rand.Next(n + 1);
|
---|
88 | T value = list[k];
|
---|
89 | list[k] = list[n];
|
---|
90 | list[n] = value;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | private void SendHeartbeatToServer(object sender, ElapsedEventArgs e) {
|
---|
95 | lock (timerLock) {
|
---|
96 | try {
|
---|
97 | heartbeatClient.UpdateHeartbeat(myself);
|
---|
98 | }
|
---|
99 | catch { } //nothing to do
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | }
|
---|
104 | }
|
---|