Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13924 was 13924, checked in by thasling, 6 years ago

#2615:
delete unneeded project, appended code style

File size: 3.3 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 {
14
15    private ConcurrentDictionary<PeerInfo, DateTime> allPeers = null;
16
17    private Timer timer = null;
18
19    private Object logLock = new Object();
20
21    public ContactServiceImpl() {
22      allPeers = new ConcurrentDictionary<PeerInfo, DateTime>();
23
24      timer = new Timer(1000); //each hour
25      timer.Elapsed += CleanUpContactTable;
26      timer.Start();
27    }
28
29    public void RegisterPeer(PeerInfo source) {
30      try {
31        UpdateHeartbeat(source);
32      }
33      catch (Exception ex) {
34        AddError("ContactServiceImpl.RegisterPeer", ex);
35      }
36    }
37
38    public List<PeerInfo> GetPeerList(PeerInfo source) {
39      try {
40        UpdateHeartbeat(source);
41        //only return peers of the same work group and not the sender itself
42        return allPeers.Keys.Where(x => {
43          if (source.ProblemInstance.Equals(x.ProblemInstance) &&
44              (!(x.IpAddress.Equals(source.IpAddress) && (x.Port.Equals(source.Port)))))
45            return true;
46          else
47            return false;
48        }).ToList();
49      }
50      catch (Exception ex) {
51        AddError("ContactServiceImpl.GetPeerList", ex);
52        return null;
53      }
54    }
55
56    public void MakeLog(PeerInfo source, string msg) {
57      try {
58        // TODO
59        lock (logLock) {
60          File.AppendAllText("Log.txt", string.Concat(source.IpAddress, ":", source.Port, ",", source.ProblemInstance, ",", msg, Environment.NewLine));
61        }
62      }
63      catch (Exception ex) {
64        //Nothing to do because maybe called from adderror
65      }
66    }
67
68    public void UpdateHeartbeat(PeerInfo source) {
69      try {
70        Console.WriteLine("hb from {0}:{1}", source.IpAddress, source.Port);
71        DateTime now = DateTime.Now;
72        allPeers.AddOrUpdate(source, now, (k, v) => v = now);
73      }
74      catch (Exception ex) {
75        AddError("ContactServiceImpl.UpdateHeartbeat", ex);
76      }
77    }
78
79    private void CleanUpContactTable(object sender, ElapsedEventArgs e) {
80      DateTime deadline = DateTime.Now;
81      //collect items to remove
82      List<PeerInfo> itemsToDelete = new List<PeerInfo>();
83      foreach (PeerInfo pi in allPeers.Keys) {
84        DateTime tmp;
85        if (allPeers.TryGetValue(pi, out tmp)) {
86          //if (tmp.AddHours(1f) < deadline)
87          if (tmp.AddMinutes(1) < deadline)  //TODO
88                    {
89            //if (tmp < deadline.AddHours(1f)) {
90            itemsToDelete.Add(pi);
91          }
92        }
93      }
94      //remove items
95      foreach (PeerInfo pi in itemsToDelete) {
96        DateTime tmp;
97        allPeers.TryRemove(pi, out tmp);
98        Console.WriteLine(string.Format("Removed peer {0}:{1} from dictionary because last access was: {2}", pi.IpAddress, pi.Port, tmp));
99      }
100    }
101
102    private void AddError(string source, Exception ex) {
103      MakeLog(new PeerInfo() { ProblemInstance = "ContactServer  Error at " + source }, ex.Message);
104    }
105
106  }
107}
Note: See TracBrowser for help on using the repository browser.