[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 |
|
---|
[13943] | 18 | private Object timerLock = new Object();
|
---|
| 19 |
|
---|
[13923] | 20 | private ChannelFactory<IContactService> myChannelFactory;
|
---|
[13524] | 21 |
|
---|
[13923] | 22 | private IContactService client;
|
---|
[13524] | 23 |
|
---|
[13923] | 24 | private IContactService heartbeatClient;
|
---|
[13524] | 25 |
|
---|
[13923] | 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 | }
|
---|
[13524] | 42 |
|
---|
[13923] | 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 | }
|
---|
[13524] | 53 |
|
---|
[13923] | 54 | public void SendLogToServer(string msg) {
|
---|
| 55 | client.MakeLog(myself, msg);
|
---|
| 56 | }
|
---|
[13887] | 57 |
|
---|
[13923] | 58 | public void Dispose() {
|
---|
| 59 | timer.Stop();
|
---|
| 60 | timer.Dispose();
|
---|
| 61 | ((IClientChannel)client).Close();
|
---|
| 62 | ((IClientChannel)heartbeatClient).Close();
|
---|
| 63 | myChannelFactory.Close();
|
---|
[13937] | 64 | client = null;
|
---|
| 65 | myChannelFactory = null;
|
---|
[13923] | 66 | }
|
---|
[13887] | 67 |
|
---|
| 68 |
|
---|
[13923] | 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 | }
|
---|
[13943] | 77 | return allPeers;
|
---|
[13923] | 78 | }
|
---|
[13887] | 79 |
|
---|
[13923] | 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);
|
---|
[13887] | 88 | }
|
---|
[13923] | 89 | }
|
---|
| 90 | return res;
|
---|
| 91 | }
|
---|
[13887] | 92 |
|
---|
[13923] | 93 | private void SendHeartbeatToServer(object sender, ElapsedEventArgs e) {
|
---|
[13943] | 94 | lock (timerLock) {
|
---|
| 95 | try {
|
---|
| 96 | heartbeatClient.UpdateHeartbeat(myself);
|
---|
| 97 | }
|
---|
| 98 | catch { } //nothing to do
|
---|
[13923] | 99 | }
|
---|
| 100 | }
|
---|
[13887] | 101 |
|
---|
[13923] | 102 | }
|
---|
[13524] | 103 | }
|
---|