Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14060 was 14060, checked in by thasling, 8 years ago

#2615:
changed parameter in P2PMigrationAnalyzer, so that there is no invalid cast ExceptionThrown
other parameters still TBD
made WcfPeerListManager stateless by removing a stored ChannelFactory and creating a new one by each method call

File size: 2.9 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 List<PeerInfo> cachedPeerList;
21
22    private double communicationRate;
23
24    private Random rand;
25
26    public void Init(PeerInfo source, string contactServerUrl, double communicationRate) {
27      serverString = contactServerUrl;
28      this.communicationRate = communicationRate;
29      myself = source;
30      cachedPeerList = new List<PeerInfo>();
31      rand = new Random();
32      //Start heartbeat timer
33      timer = new Timer(1000 * 20); //each 20 seconds
34      timer.Elapsed += RefreshPeerList;
35      timer.Start();
36    }
37
38    public List<PeerInfo> GetPeerList() {
39      return cachedPeerList;
40    }
41
42    public void SendLogToServer(string msg) {
43      var binding = new NetTcpBinding();
44      var endpoint = new EndpointAddress(serverString);
45      using (var myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint)) {
46        using (IClientChannel client = (IClientChannel)myChannelFactory.CreateChannel()) {
47          ((IContactService)client).MakeLog(myself, msg);     
48        }
49      }
50    }
51
52    public void Dispose() {
53      timer.Stop();
54      timer.Dispose();
55    }
56
57    private List<PeerInfo> ChoosePeersForMessaging(ref List<PeerInfo> allPeers) {
58      Shuffle<PeerInfo>(allPeers);
59      int toTake = Convert.ToInt32(allPeers.Count * communicationRate) + 1;
60      if (allPeers.Count > 0 && toTake == 0) {
61        toTake = 1;
62      }
63      return allPeers.Take(toTake).ToList(); ;
64    }
65
66    private void Shuffle<T>(IList<T> list) {
67      int n = list.Count;
68      while (n > 1) {
69        n--;
70        int k = rand.Next(n + 1);
71        T value = list[k];
72        list[k] = list[n];
73        list[n] = value;
74      }
75    }
76
77    private void RefreshPeerList(object sender, ElapsedEventArgs e) {
78      lock (timerLock) {
79        try {
80          List<PeerInfo> allPeers = new List<PeerInfo>();
81          var binding = new NetTcpBinding();
82          var endpoint = new EndpointAddress(serverString);
83          using (var myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint)) {
84            using (IClientChannel client = (IClientChannel)myChannelFactory.CreateChannel()) {
85              allPeers = ((IContactService)client).GetPeerList(myself);           
86            }
87          }
88          cachedPeerList = ChoosePeersForMessaging(ref allPeers);
89        }
90        catch { } //nothing to do
91      }
92    }
93
94  }
95}
Note: See TracBrowser for help on using the repository browser.