[13538] | 1 | using System;
|
---|
[13524] | 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.ServiceModel;
|
---|
[13538] | 5 | using DistributedGA.Core.Domain;
|
---|
| 6 | using DistributedGA.Core.Interface;
|
---|
[13887] | 7 | using System.Timers;
|
---|
[13524] | 8 |
|
---|
[13923] | 9 | namespace DistributedGA.Core.Implementation {
|
---|
| 10 | public class WcfPeerListManager : IPeerListManager {
|
---|
[13524] | 11 |
|
---|
[13923] | 12 | private string serverString = null;
|
---|
[13524] | 13 |
|
---|
[13923] | 14 | private PeerInfo myself = null;
|
---|
[13524] | 15 |
|
---|
[13923] | 16 | private Timer timer = null; //sends heartbeat to contact-server
|
---|
[13524] | 17 |
|
---|
[13947] | 18 | private Object timerLock = new Object();
|
---|
[13943] | 19 |
|
---|
[13923] | 20 | private ChannelFactory<IContactService> myChannelFactory;
|
---|
[13524] | 21 |
|
---|
[13923] | 22 | private IContactService client;
|
---|
[13524] | 23 |
|
---|
[13923] | 24 | private IContactService heartbeatClient;
|
---|
[13524] | 25 |
|
---|
[13956] | 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) {
|
---|
[13923] | 29 | serverString = contactServerUrl;
|
---|
[13956] | 30 | this.communicationRate = communicationRate;
|
---|
[13923] | 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 | }
|
---|
[13524] | 45 |
|
---|
[13923] | 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 | }
|
---|
[13524] | 56 |
|
---|
[13923] | 57 | public void SendLogToServer(string msg) {
|
---|
| 58 | client.MakeLog(myself, msg);
|
---|
| 59 | }
|
---|
[13887] | 60 |
|
---|
[13923] | 61 | public void Dispose() {
|
---|
| 62 | timer.Stop();
|
---|
| 63 | timer.Dispose();
|
---|
| 64 | ((IClientChannel)client).Close();
|
---|
| 65 | ((IClientChannel)heartbeatClient).Close();
|
---|
| 66 | myChannelFactory.Close();
|
---|
[13937] | 67 | client = null;
|
---|
| 68 | myChannelFactory = null;
|
---|
[13923] | 69 | }
|
---|
[13887] | 70 |
|
---|
| 71 |
|
---|
[13923] | 72 | private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers) {
|
---|
| 73 | //communicate with 10% of the network
|
---|
[13959] | 74 | int noOfPeers = allPeers.Count / (100 / communicationRate);
|
---|
[13923] | 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 | }
|
---|
[13943] | 80 | return allPeers;
|
---|
[13923] | 81 | }
|
---|
[13887] | 82 |
|
---|
[13923] | 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) {
|
---|
[13959] | 88 | tmp = rnd.Next(minValue, maxValue + 1);
|
---|
[13923] | 89 | if (!res.Contains(tmp)) {
|
---|
| 90 | res.Add(tmp);
|
---|
[13887] | 91 | }
|
---|
[13923] | 92 | }
|
---|
| 93 | return res;
|
---|
| 94 | }
|
---|
[13887] | 95 |
|
---|
[13923] | 96 | private void SendHeartbeatToServer(object sender, ElapsedEventArgs e) {
|
---|
[13943] | 97 | lock (timerLock) {
|
---|
| 98 | try {
|
---|
| 99 | heartbeatClient.UpdateHeartbeat(myself);
|
---|
| 100 | }
|
---|
| 101 | catch { } //nothing to do
|
---|
[13923] | 102 | }
|
---|
| 103 | }
|
---|
[13887] | 104 |
|
---|
[13923] | 105 | }
|
---|
[13524] | 106 | }
|
---|