Changeset 13982


Ignore:
Timestamp:
07/02/16 15:23:57 (3 years ago)
Author:
thasling
Message:

#2615:
made minor changes

Location:
branches/thasling/DistributedGA
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/thasling/DistributedGA/DistributedGA.ContactServer/ContactServiceImpl.cs

    r13943 r13982  
    2424      allPeers = new ConcurrentDictionary<PeerInfo, DateTime>();
    2525
    26       timer = new Timer(1000); //each hour
     26      timer = new Timer(1000); //each minute
    2727      timer.Elapsed += CleanUpContactTable;
    2828      timer.Start();
     
    8787          DateTime tmp;
    8888          if (allPeers.TryGetValue(pi, out tmp)) {
    89             //if (tmp.AddHours(1f) < deadline)
    90             if (tmp.AddMinutes(1) < deadline)  //TODO
    91                     {
    92               //if (tmp < deadline.AddHours(1f)) {
     89            if (tmp.AddMinutes(1) < deadline) {
    9390              itemsToDelete.Add(pi);
    9491            }
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs

    r13972 r13982  
    2828    private IMessageService host = null;
    2929
     30    private double communicationRate;
     31    private Random rand;
     32
    3033    public event EventHandler<Exception> ExceptionOccurend;
    3134
     
    3740          ProblemInstance = problemInstance
    3841        }; // TODO: get own peerinfo
     42
     43        this.communicationRate = communicationRate;
     44        rand = new Random();
    3945
    4046        writeQueue = new SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>>();
     
    4854
    4955        peerListManager = new WcfPeerListManager();
    50         peerListManager.Init(ownInstance, contactServerUrl, communicationRate);
     56        peerListManager.Init(ownInstance, contactServerUrl);
    5157
    5258        sender = new WcfMessageSender();
     
    7278    public void PublishDataToNetwork(byte[] data) {
    7379      try {
     80        var peers = peerListManager.GetPeerList();
    7481        foreach (PeerInfo peer in peerListManager.GetPeerList()) {
     82          var peersForMessaging = ChoosePeersForMessaging(ref peers);
     83
    7584          //maybe something will go wrong during network communication...
    7685          try {
     
    129138    }
    130139
     140    private List<PeerInfo> ChoosePeersForMessaging(ref List<PeerInfo> allPeers) {
     141      Shuffle<PeerInfo>(allPeers);
     142      int toTake = Convert.ToInt32(allPeers.Count * communicationRate) + 1;
     143      if (allPeers.Count > 0 && toTake == 0) {
     144        toTake = 1;
     145      }
     146      return allPeers.Take(toTake).ToList(); ;
     147    }
     148
    131149    private string GetInternalIpAddress(string ipPrefix) {
    132150      try {
     
    141159      }
    142160      catch { return null; }
     161    }
     162
     163    private void Shuffle<T>(IList<T> list) {
     164      int n = list.Count;
     165      while (n > 1) {
     166        n--;
     167        int k = rand.Next(n + 1);
     168        T value = list[k];
     169        list[k] = list[n];
     170        list[n] = value;
     171      }
    143172    }
    144173
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageSender.cs

    r13972 r13982  
    102102    private void SendDataFromQueue(PeerInfo destination, byte[][] data) {
    103103      try {
    104         //int arrayLength = 3;
    105         //if (data.GetUpperBound(0) > arrayLength) {
    106         //  //HACK: SEND MAX 10 items
    107         //  byte[][] fake = new byte[arrayLength][];
    108         //  for (int i = 0; i < arrayLength; i++) {
    109         //    fake[i] = data[i];
    110         //  }
    111         //  data = fake;
    112         //}
    113 
    114 
    115104        Console.WriteLine(string.Format("Sending data to {0}:{1} in the background...", destination.IpAddress, destination.Port));
    116105        var serviceUrl = "DistributedGA.svc";
     
    130119          using (IClientChannel client = (IClientChannel)myChannelFactory.CreateChannel()) {
    131120            for (int i = 0; i < data.GetUpperBound(0) + 1; i++) {
    132               ((IMessageContract)client).SendData(myself, data[i]); //maybe timout exception...
     121              ((IMessageContract)client).SendData(myself, data[i]); //maybe exception...
    133122            }
    134123          }
  • branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs

    r13965 r13982  
    2424    private IContactService heartbeatClient;
    2525
    26     private Random rand;
    2726
    28     private double communicationRate; //how many peers are contacted by this peer in percent
    2927
    30     public void Init(PeerInfo source, string contactServerUrl, double communicationRate) {
     28    public void Init(PeerInfo source, string contactServerUrl) {
    3129      serverString = contactServerUrl;
    32       this.communicationRate = communicationRate;
    3330      myself = source;
    34       rand = new Random();
    35      
     31
    3632      //Init ChannelFactory and Clients
    3733      var binding = new NetTcpBinding();
     
    4339      client.RegisterPeer(source);
    4440      //Start heartbeat timer
    45       timer = new Timer(1000); //each 5 minutes
     41      timer = new Timer(1000 * 20); //each 20 seconds
    4642      timer.Elapsed += SendHeartbeatToServer;
    4743      timer.Start();
     
    5147      try {
    5248        var allPeers = client.GetPeerList(myself); //maybe timout exception...
    53         var peersForMessaging = ChoosePeersForMessaging(ref allPeers);
    54         return peersForMessaging;
     49        return allPeers;
    5550      }
    5651      catch { } //if maybe sending failed (because of connection lost, etc.): just ignore
     
    7267    }
    7368
    74 
    75     private List<PeerInfo> ChoosePeersForMessaging(ref List<PeerInfo> allPeers) {
    76       Shuffle<PeerInfo>(allPeers);
    77       int toTake = Convert.ToInt32(allPeers.Count * communicationRate) + 1;
    78       if (allPeers.Count > 0 && toTake == 0) {
    79         toTake = 1;
    80       }
    81       return allPeers.Take(toTake).ToList(); ;
    82     }
    83     private  void Shuffle<T>(IList<T> list) {
    84       int n = list.Count;
    85       while (n > 1) {
    86         n--;
    87         int k = rand.Next(n + 1);
    88         T value = list[k];
    89         list[k] = list[n];
    90         list[n] = value;
    91       }
    92     }
    93 
    9469    private void SendHeartbeatToServer(object sender, ElapsedEventArgs e) {
    9570      lock (timerLock) {
  • branches/thasling/DistributedGA/DistributedGA.Core/Interface/IPeerListManager.cs

    r13965 r13982  
    66  public interface IPeerListManager {
    77
    8     void Init(PeerInfo source, string contactServerUrl, double communicationRate); //Registers own instance at the contact-server
     8    void Init(PeerInfo source, string contactServerUrl); //Registers own instance at the contact-server
    99
    1010    List<PeerInfo> GetPeerList(); //Recieves all peers in the network from contact-server
  • branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs

    r13972 r13982  
    136136      base.InitializeState();
    137137      // init P2P
    138       Init();
     138      if (h == null) {
     139        //otherwhise old object is not disposed correctly
     140        Init();
     141      }
    139142    }
    140143
Note: See TracChangeset for help on using the changeset viewer.