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