Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2615: implemented Dispose()-Method in all classes
also sending a message with the wcf-sender class was rewritten, because of Dispose
added new log entries when server removes peers from its dicionary

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      } 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));
59        }
60      } catch (Exception ex) {
61        //Nothing to do because maybe called from adderror
62      }
63    }
64
65    public void UpdateHeartbeat(PeerInfo source) {
66      try {
67        Console.WriteLine("hb from {0}:{1}", source.IpAddress, source.Port);
68        DateTime now = DateTime.Now;
69        allPeers.AddOrUpdate(source, now, (k, v) => v = now);
70      } catch (Exception ex) {
71        AddError("ContactServiceImpl.UpdateHeartbeat", ex);
72      }
73    }
74
75    private void CleanUpContactTable(object sender, ElapsedEventArgs e) {
76      DateTime deadline = DateTime.Now;
77      //collect items to remove
78      List<PeerInfo> itemsToDelete = new List<PeerInfo>();
79      foreach (PeerInfo pi in allPeers.Keys) {
80        DateTime tmp;
81        if (allPeers.TryGetValue(pi, out tmp)) {
82          //if (tmp.AddHours(1f) < deadline)
83          if (tmp.AddMinutes(1) < deadline)  //TODO
84                    {
85            //if (tmp < deadline.AddHours(1f)) {
86            itemsToDelete.Add(pi);
87          }
88        }
89      }
90      //remove items
91      foreach (PeerInfo pi in itemsToDelete) {
92        DateTime tmp;
93        allPeers.TryRemove(pi, out tmp);
94        Console.WriteLine(string.Format("Removed peer {0}:{1} from dictionary because last access was: {2}", pi.IpAddress, pi.Port, tmp));
95      }
96    }
97
98    private void AddError(string source, Exception ex) {
99      MakeLog(new PeerInfo() { ProblemInstance = "ContactServer  Error at " + source }, ex.Message);
100    }
101
102  }
103}
Note: See TracBrowser for help on using the repository browser.