Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/19/16 21:37:24 (8 years ago)
Author:
thasling
Message:

#2615:
re-implemented WcfPeerListManager
also sending messages is now done in the background async

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs

    r13918 r13923  
    77using System.Timers;
    88
    9 namespace DistributedGA.Core.Implementation
    10 {
    11     public class WcfPeerListManager : IPeerListManager
    12     {
    13         private string serverString = null;
     9namespace DistributedGA.Core.Implementation {
     10  public class WcfPeerListManager : IPeerListManager {
    1411
    15         private IContactService client = null;
     12    private string serverString = null;
    1613
    17         private IContactService heartbeatClient = null;
     14    private PeerInfo myself = null;
    1815
    19         private PeerInfo myself = null;
     16    private Timer timer = null; //sends heartbeat to contact-server
    2017
    21         private Timer timer = null; //sends heartbeat to contact-server
     18    private ChannelFactory<IContactService> myChannelFactory;
    2219
    23         public void Init(PeerInfo source, string contactServerUrl)
    24         {
    25             serverString = contactServerUrl;
    26             client = CreateClient();
    27             heartbeatClient = CreateClient();
    28             myself = source;
    29             client.RegisterPeer(source);
    30             timer = new Timer(1000 ); //each 5 minutes
    31             timer.Elapsed += SendHeartbeatToServer;
    32             timer.Start();
    33         }
     20    private IContactService client;
     21
     22    private IContactService heartbeatClient;
     23
     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    }
     40
     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    }
     51
     52    public void SendLogToServer(string msg) {
     53      client.MakeLog(myself, msg);
     54    }
     55
     56    public void Dispose() {
     57      timer.Stop();
     58      timer.Dispose();
     59      ((IClientChannel)client).Close();
     60      ((IClientChannel)heartbeatClient).Close();
     61      myChannelFactory.Close();
     62    }
    3463
    3564
     65    private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers) {
     66      //communicate with 10% of the network
     67      int noOfPeers = allPeers.Count / 10;
     68      List<int> indexList = GetRandomItemIndexes(noOfPeers, 0, allPeers.Count - 1);
     69      List<PeerInfo> res = new List<PeerInfo>();
     70      foreach (int index in indexList) {
     71        res.Add(allPeers.ElementAt(index));
     72      }
     73      return res;
     74    }
    3675
    37         public List<PeerInfo> GetPeerList()
    38         {
    39             var allPeers = client.GetPeerList(myself);
    40             var peersForMessaging = ChoosePeersForMessaging(allPeers);
    41             //return peersForMessaging;
    42             return allPeers; //TODO: Enable 10% list communication
     76    private List<int> GetRandomItemIndexes(int noOfItems, int minValue, int maxValue) {
     77      List<int> res = new List<int>();
     78      Random rnd = new Random();
     79      int tmp = -1;
     80      while (res.Count < noOfItems) {
     81        tmp = rnd.Next(minValue, maxValue);
     82        if (!res.Contains(tmp)) {
     83          res.Add(tmp);
    4384        }
     85      }
     86      return res;
     87    }
    4488
    45         public void SendLogToServer(string msg)
    46         {
    47             client.MakeLog(myself, msg);
    48         }
     89    private void SendHeartbeatToServer(object sender, ElapsedEventArgs e) {
     90      try {
     91        heartbeatClient.UpdateHeartbeat(myself);
     92      }
     93      catch { } //nothing to do
     94    }
    4995
    50         private IContactService CreateClient()
    51         {
    52             var binding = new NetTcpBinding();
    53             var endpoint = new EndpointAddress(serverString);
    54             var myChannelFactory = new ChannelFactory<IContactService>(binding, endpoint);
    55 
    56             IContactService client = null;
    57             client = myChannelFactory.CreateChannel();
    58             return client;
    59         }
    60 
    61         private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers)
    62         {
    63             //communicate with 10% of the network
    64             int noOfPeers = allPeers.Count / 10;
    65             List<int> indexList = GetRandomItemIndexes(noOfPeers, 0, allPeers.Count - 1);
    66             List<PeerInfo> res = new List<PeerInfo>();
    67             foreach (int index in indexList)
    68             {
    69                 res.Add(allPeers.ElementAt(index));
    70             }
    71             return res;
    72         }
    73 
    74         private List<int> GetRandomItemIndexes(int noOfItems, int minValue, int maxValue)
    75         {
    76             List<int> res = new List<int>();
    77             Random rnd = new Random();
    78             int tmp = -1;
    79             while (res.Count < noOfItems)
    80             {
    81                 tmp = rnd.Next(minValue, maxValue);
    82                 if (!res.Contains(tmp))
    83                 {
    84                     res.Add(tmp);
    85                 }
    86             }
    87             return res;
    88         }
    89 
    90         private void SendHeartbeatToServer(object sender, ElapsedEventArgs e)
    91         {
    92             try
    93             {
    94                 heartbeatClient.UpdateHeartbeat(myself);
    95             }
    96             catch { } //nothing to do, exception is raised when getting peer list
    97         }
    98 
    99 
    100 
    101         public void Dispose()
    102         {
    103             timer.Stop();
    104             timer.Dispose();
    105             timer = null;
    106         }
    107     }
     96  }
    10897}
Note: See TracChangeset for help on using the changeset viewer.