source: branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs @ 13956

Last change on this file since 13956 was 13956, checked in by thasling, 6 years ago

#2615:
finally fixed bug concerning message send to the wrong peers
also made communicationRate and messageCacheCapacity as paramters
integration in P2PMigrationAnalyzer still TBD

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