1 | using DistributedGA.Core.Domain;
|
---|
2 | using System;
|
---|
3 | using System.Collections.Concurrent;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Linq;
|
---|
6 | using System.ServiceModel;
|
---|
7 | using System.Text;
|
---|
8 | using System.Threading.Tasks;
|
---|
9 | using System.Timers;
|
---|
10 |
|
---|
11 | namespace DistributedGA.ContactServer
|
---|
12 | {
|
---|
13 |
|
---|
14 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
|
---|
15 | public class ContactServiceImpl : IContactService
|
---|
16 | {
|
---|
17 |
|
---|
18 | private ConcurrentDictionary<PeerInfo, DateTime> allPeers = null;
|
---|
19 |
|
---|
20 | private Timer timer = null;
|
---|
21 |
|
---|
22 | public ContactServiceImpl()
|
---|
23 | {
|
---|
24 | allPeers = new ConcurrentDictionary<PeerInfo, DateTime>();
|
---|
25 |
|
---|
26 | timer = new Timer(60 * 1000); //each hour
|
---|
27 | timer.Elapsed += CleanUpContactTable;
|
---|
28 | timer.Start();
|
---|
29 | }
|
---|
30 |
|
---|
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 | }
|
---|
41 | }
|
---|
42 |
|
---|
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 | }
|
---|
64 |
|
---|
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 | }
|
---|
115 | }
|
---|