Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/18/16 13:24:28 (9 years ago)
Author:
thasling
Message:

Connectionstring to peerlistserver now in app.config

Location:
branches/thasling/DistributedGA/DistributedGA.ContactServer
Files:
9 edited

Legend:

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

    r13524 r13538  
    1 using DistributedGA.Core.Domain;
    2 using System;
     1using System;
    32using System.Collections.Concurrent;
    43using System.Collections.Generic;
     4using System.IO;
    55using System.Linq;
    66using System.ServiceModel;
    7 using System.Text;
    8 using System.Threading.Tasks;
    97using System.Timers;
     8using DistributedGA.Core.Domain;
    109
    11 namespace DistributedGA.ContactServer
    12 {
     10namespace DistributedGA.ContactServer {
    1311
    14     [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    15     public class ContactServiceImpl : IContactService
    16     {
     12  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
     13  public class ContactServiceImpl : IContactService {
    1714
    18         private ConcurrentDictionary<PeerInfo, DateTime> allPeers = null;
     15    private ConcurrentDictionary<PeerInfo, DateTime> allPeers = null;
    1916
    20         private Timer timer = null;
     17    private Timer timer = null;
    2118
    22         public ContactServiceImpl()
    23         {
    24             allPeers = new ConcurrentDictionary<PeerInfo, DateTime>();
     19    private Object logLock = new Object();
    2520
    26             timer = new Timer(60 * 1000); //each hour
    27             timer.Elapsed += CleanUpContactTable;
    28             timer.Start();
     21    public ContactServiceImpl() {
     22      allPeers = new ConcurrentDictionary<PeerInfo, DateTime>();
     23
     24      timer = new Timer(60 * 1000); //each hour
     25      timer.Elapsed += CleanUpContactTable;
     26      timer.Start();
     27    }
     28
     29    public void RegisterPeer(PeerInfo source) {
     30      try {
     31        UpdateHeartbeat(source);
     32      } catch (Exception ex) {
     33        AddError("ContactServiceImpl.RegisterPeer", ex);
     34      }
     35    }
     36
     37    public List<PeerInfo> GetPeerList(PeerInfo source) {
     38      try {
     39        UpdateHeartbeat(source);
     40        //only return peers of the same work group and not the sender itself
     41        return allPeers.Keys.Where(x => {
     42          if (source.ProblemInstance.Equals(x.ProblemInstance) &&
     43              (!(x.IpAddress.Equals(source.IpAddress) && (x.Port.Equals(source.Port)))))
     44            return true;
     45          else
     46            return false;
     47        }).ToList();
     48      } catch (Exception ex) {
     49        AddError("ContactServiceImpl.GetPeerList", ex);
     50        return null;
     51      }
     52    }
     53
     54    public void MakeLog(PeerInfo source, string msg) {
     55      try {
     56        // TODO
     57        lock (logLock) {
     58          File.AppendAllText("Log.txt", string.Concat(source.IpAddress, ":", source.Port, ",", source.ProblemInstance, ",", msg, Environment.NewLine));
    2959        }
     60      } catch (Exception ex) {
     61        //Nothing to do because maybe called from adderror
     62      }
     63    }
    3064
    31         public void RegisterPeer(PeerInfo source)
    32         {
    33             try
    34             {
    35                 UpdateHeartbeat(source);
    36             }
    37             catch (Exception ex)
    38             {
    39                 AddError("ContactServiceImpl.RegisterPeer", ex);
    40             }
     65    private void UpdateHeartbeat(PeerInfo source) {
     66      DateTime now = DateTime.Now;
     67      allPeers.AddOrUpdate(source, now, (k, v) => v = now);
     68    }
     69
     70    private void CleanUpContactTable(object sender, ElapsedEventArgs e) {
     71      DateTime deadline = DateTime.Now;
     72      //collect items to remove
     73      List<PeerInfo> itemsToDelete = new List<PeerInfo>();
     74      foreach (PeerInfo pi in allPeers.Keys) {
     75        DateTime tmp;
     76        if (allPeers.TryGetValue(pi, out tmp)) {
     77          //if (tmp.AddHours(1f) < deadline)
     78          if (tmp < deadline.AddHours(1f)) {
     79            itemsToDelete.Add(pi);
     80          }
    4181        }
     82      }
     83      //remove items
     84      foreach (PeerInfo pi in itemsToDelete) {
     85        DateTime tmp;
     86        allPeers.TryRemove(pi, out tmp);
     87      }
     88    }
    4289
    43         public List<PeerInfo> GetPeerList(PeerInfo source)
    44         {
    45             try
    46             {
    47                 UpdateHeartbeat(source);
    48                 //only return peers of the same work group and not the sender itself
    49                 return allPeers.Keys.Where(x =>
    50                 {
    51                     if (source.ProblemInstance.Equals(x.ProblemInstance) &&
    52                         (!(x.IpAddress.Equals(source.IpAddress) && (x.Port.Equals(source.Port)))))
    53                         return true;
    54                     else
    55                         return false;
    56                 }).ToList();
    57             }
    58             catch (Exception ex)
    59             {
    60                 AddError("ContactServiceImpl.GetPeerList", ex);
    61                 return null;
    62             }
    63         }
     90    private void AddError(string source, Exception ex) {
     91      MakeLog(new PeerInfo() { ProblemInstance = "ContactServer  Error at " + source }, ex.Message);
     92    }
    6493
    65         public void MakeLog(PeerInfo source, string msg)
    66         {
    67             try
    68             {
    69                 // TODO
    70 
    71             }
    72             catch (Exception ex)
    73             {
    74                 AddError("ContactServiceImpl.GetPeerList", ex);
    75             }
    76         }
    77 
    78         private void UpdateHeartbeat(PeerInfo source)
    79         {
    80             DateTime now = DateTime.Now;
    81             allPeers.AddOrUpdate(source, now, (k, v) => v = now);
    82         }
    83 
    84         private void CleanUpContactTable(object sender, ElapsedEventArgs e)
    85         {
    86             DateTime deadline = DateTime.Now;
    87             //collect items to remove
    88             List<PeerInfo> itemsToDelete = new List<PeerInfo>();
    89             foreach (PeerInfo pi in allPeers.Keys)
    90             {
    91                 DateTime tmp;
    92                 if (allPeers.TryGetValue(pi, out tmp))
    93                 {
    94                     //if (tmp.AddHours(1f) < deadline)
    95                     if (tmp < deadline.AddHours(1f))
    96                     {
    97                         itemsToDelete.Add(pi);
    98                     }
    99                 }
    100             }
    101             //remove items
    102             foreach (PeerInfo pi in itemsToDelete)
    103             {
    104                 DateTime tmp;
    105                 allPeers.TryRemove(pi, out tmp);
    106             }
    107         }
    108 
    109         private void AddError(string source, Exception ex)
    110         {
    111             MakeLog(new PeerInfo() { ProblemInstance = "ContactServer  Error at " + source }, ex.Message);
    112         }
    113 
    114     }
     94  }
    11595}
  • branches/thasling/DistributedGA/DistributedGA.ContactServer/obj/Debug/WcfConfigValidationData.tmp

    r13537 r13538  
    1 <ValidationRecord xmlns="http://schemas.datacontract.org/2004/07/Microsoft.VisualStudio.ServiceModel.Validation" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><TimeStamps xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><a:dateTime>2016-01-18T09:27:09.7709563Z</a:dateTime><a:dateTime>2016-01-18T09:31:18.4519841Z</a:dateTime><a:dateTime>2015-04-01T13:05:26.8346639Z</a:dateTime></TimeStamps></ValidationRecord>
     1<ValidationRecord xmlns="http://schemas.datacontract.org/2004/07/Microsoft.VisualStudio.ServiceModel.Validation" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><TimeStamps xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><a:dateTime>2016-01-18T10:18:36.7404261Z</a:dateTime><a:dateTime>2016-01-18T12:23:29.9363892Z</a:dateTime><a:dateTime>2015-04-01T13:05:26.8346639Z</a:dateTime></TimeStamps></ValidationRecord>
Note: See TracChangeset for help on using the changeset viewer.