Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 3.0 KB
Line 
1using System;
2using System.Collections.Concurrent;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.ServiceModel;
7using System.Timers;
8using DistributedGA.Core.Domain;
9
10namespace DistributedGA.ContactServer {
11
12  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
13  public class ContactServiceImpl : IContactService, IDisposable {
14
15    private ConcurrentDictionary<PeerInfo, DateTime> allPeers = null;
16
17    private Timer timer = null;
18
19    private Object logLock = new Object();
20
21    private Object timerLock = new Object();//if elapsed takes too long, another thread also enters the method
22
23    public ContactServiceImpl() {
24      allPeers = new ConcurrentDictionary<PeerInfo, DateTime>();
25
26      timer = new Timer(1000 * 60 * 2); //each minute
27      timer.Elapsed += CleanUpContactTable;
28      timer.Start();
29    }
30
31    public void RegisterPeer(PeerInfo source) {
32      try {
33        UpdateHeartbeat(source);
34      }
35      catch (Exception ex) {
36        AddError("ContactServiceImpl.RegisterPeer", ex);
37      }
38    }
39
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();
51      }
52      catch (Exception ex) {
53        AddError("ContactServiceImpl.GetPeerList", ex);
54        return null;
55      }
56    }
57
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);
63      }
64      catch (Exception ex) {
65        AddError("ContactServiceImpl.UpdateHeartbeat", ex);
66      }
67    }
68
69    private void CleanUpContactTable(object sender, ElapsedEventArgs e) {
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)) {
77            if (tmp.AddMinutes(2) < deadline) {
78              itemsToDelete.Add(pi);
79            }
80          }
81        }
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        }
88      }
89    }
90
91    private void AddError(string source, Exception ex) {
92
93    }
94
95    public void Dispose() {
96      timer.Stop();
97      timer.Dispose();
98      timer = null;
99    }
100
101  }
102}
Note: See TracBrowser for help on using the repository browser.