Free cookie consent management tool by TermsFeed Policy Generator

source: branches/thasling/DistributedGA/DistributedGA.ContactServer/ContactServiceImpl.cs @ 16793

Last change on this file since 16793 was 14261, checked in by thasling, 8 years ago

#2615:
re-enabled log in P2PMigrationAnalyzer
made code formatings

File size: 3.0 KB
RevLine 
[13538]1using System;
[13524]2using System.Collections.Concurrent;
3using System.Collections.Generic;
[13538]4using System.IO;
[13524]5using System.Linq;
6using System.ServiceModel;
7using System.Timers;
[13538]8using DistributedGA.Core.Domain;
[13524]9
[13905]10namespace DistributedGA.ContactServer {
[13524]11
[13905]12  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
[14252]13  public class ContactServiceImpl : IContactService, IDisposable {
[13524]14
[13905]15    private ConcurrentDictionary<PeerInfo, DateTime> allPeers = null;
[13524]16
[13905]17    private Timer timer = null;
[13524]18
[13905]19    private Object logLock = new Object();
[13524]20
[13943]21    private Object timerLock = new Object();//if elapsed takes too long, another thread also enters the method
22
[13905]23    public ContactServiceImpl() {
24      allPeers = new ConcurrentDictionary<PeerInfo, DateTime>();
[13524]25
[14074]26      timer = new Timer(1000 * 60 * 2); //each minute
[13905]27      timer.Elapsed += CleanUpContactTable;
28      timer.Start();
29    }
[13524]30
[13905]31    public void RegisterPeer(PeerInfo source) {
32      try {
33        UpdateHeartbeat(source);
[13924]34      }
35      catch (Exception ex) {
[13905]36        AddError("ContactServiceImpl.RegisterPeer", ex);
37      }
38    }
[13524]39
[13905]40    public List<PeerInfo> GetPeerList(PeerInfo source) {
41      try {
42        UpdateHeartbeat(source);
43        //only return peers of the same work group and not the sender itself
44        return allPeers.Keys.Where(x => {
45          if (source.ProblemInstance.Equals(x.ProblemInstance) &&
46              (!(x.IpAddress.Equals(source.IpAddress) && (x.Port.Equals(source.Port)))))
47            return true;
48          else
49            return false;
50        }).ToList();
[13924]51      }
52      catch (Exception ex) {
[13905]53        AddError("ContactServiceImpl.GetPeerList", ex);
54        return null;
55      }
56    }
[13524]57
[13905]58    public void UpdateHeartbeat(PeerInfo source) {
59      try {
60        Console.WriteLine("hb from {0}:{1}", source.IpAddress, source.Port);
61        DateTime now = DateTime.Now;
62        allPeers.AddOrUpdate(source, now, (k, v) => v = now);
[13924]63      }
64      catch (Exception ex) {
[13905]65        AddError("ContactServiceImpl.UpdateHeartbeat", ex);
66      }
67    }
[13524]68
[13905]69    private void CleanUpContactTable(object sender, ElapsedEventArgs e) {
[13943]70      lock (timerLock) {
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)) {
[14074]77            if (tmp.AddMinutes(2) < deadline) {
[13943]78              itemsToDelete.Add(pi);
79            }
[13905]80          }
[13524]81        }
[13943]82        //remove items
83        foreach (PeerInfo pi in itemsToDelete) {
84          DateTime tmp;
85          allPeers.TryRemove(pi, out tmp);
86          Console.WriteLine(string.Format("Removed peer {0}:{1} from dictionary because last access was: {2}", pi.IpAddress, pi.Port, tmp));
87        }
[13905]88      }
89    }
[13524]90
[13905]91    private void AddError(string source, Exception ex) {
[14261]92
[13905]93    }
[13887]94
[14252]95    public void Dispose() {
96      timer.Stop();
97      timer.Dispose();
98      timer = null;
99    }
100
[13905]101  }
[13524]102}
Note: See TracBrowser for help on using the repository browser.