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