[13538] | 1 | using System;
|
---|
[13524] | 2 | using System.Collections.Concurrent;
|
---|
| 3 | using System.Collections.Generic;
|
---|
[13538] | 4 | using System.IO;
|
---|
[13524] | 5 | using System.Linq;
|
---|
| 6 | using System.ServiceModel;
|
---|
| 7 | using System.Timers;
|
---|
[13538] | 8 | using DistributedGA.Core.Domain;
|
---|
[13524] | 9 |
|
---|
[13538] | 10 | namespace DistributedGA.ContactServer {
|
---|
[13524] | 11 |
|
---|
[13538] | 12 | [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
|
---|
| 13 | public class ContactServiceImpl : IContactService {
|
---|
[13524] | 14 |
|
---|
[13538] | 15 | private ConcurrentDictionary<PeerInfo, DateTime> allPeers = null;
|
---|
[13524] | 16 |
|
---|
[13538] | 17 | private Timer timer = null;
|
---|
[13524] | 18 |
|
---|
[13538] | 19 | private Object logLock = new Object();
|
---|
[13524] | 20 |
|
---|
[13538] | 21 | public ContactServiceImpl() {
|
---|
| 22 | allPeers = new ConcurrentDictionary<PeerInfo, DateTime>();
|
---|
[13524] | 23 |
|
---|
[13538] | 24 | timer = new Timer(60 * 1000); //each hour
|
---|
| 25 | timer.Elapsed += CleanUpContactTable;
|
---|
| 26 | timer.Start();
|
---|
| 27 | }
|
---|
[13524] | 28 |
|
---|
[13538] | 29 | public void RegisterPeer(PeerInfo source) {
|
---|
| 30 | try {
|
---|
| 31 | UpdateHeartbeat(source);
|
---|
| 32 | } catch (Exception ex) {
|
---|
| 33 | AddError("ContactServiceImpl.RegisterPeer", ex);
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
[13524] | 36 |
|
---|
[13538] | 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 | }
|
---|
[13524] | 53 |
|
---|
[13538] | 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));
|
---|
[13524] | 59 | }
|
---|
[13538] | 60 | } catch (Exception ex) {
|
---|
| 61 | //Nothing to do because maybe called from adderror
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
[13524] | 64 |
|
---|
[13538] | 65 | private void UpdateHeartbeat(PeerInfo source) {
|
---|
[13557] | 66 | Console.WriteLine("hb from {0}:{1}", source.IpAddress, source.Port);
|
---|
[13538] | 67 | DateTime now = DateTime.Now;
|
---|
| 68 | allPeers.AddOrUpdate(source, now, (k, v) => v = now);
|
---|
| 69 | }
|
---|
[13524] | 70 |
|
---|
[13538] | 71 | private void CleanUpContactTable(object sender, ElapsedEventArgs e) {
|
---|
| 72 | DateTime deadline = DateTime.Now;
|
---|
| 73 | //collect items to remove
|
---|
| 74 | List<PeerInfo> itemsToDelete = new List<PeerInfo>();
|
---|
| 75 | foreach (PeerInfo pi in allPeers.Keys) {
|
---|
| 76 | DateTime tmp;
|
---|
| 77 | if (allPeers.TryGetValue(pi, out tmp)) {
|
---|
[13555] | 78 | if (tmp.AddHours(1f) < deadline) {
|
---|
| 79 | //if (tmp < deadline.AddHours(1f)) {
|
---|
[13538] | 80 | itemsToDelete.Add(pi);
|
---|
| 81 | }
|
---|
[13524] | 82 | }
|
---|
[13538] | 83 | }
|
---|
| 84 | //remove items
|
---|
| 85 | foreach (PeerInfo pi in itemsToDelete) {
|
---|
| 86 | DateTime tmp;
|
---|
| 87 | allPeers.TryRemove(pi, out tmp);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
[13524] | 90 |
|
---|
[13538] | 91 | private void AddError(string source, Exception ex) {
|
---|
| 92 | MakeLog(new PeerInfo() { ProblemInstance = "ContactServer Error at " + source }, ex.Message);
|
---|
| 93 | }
|
---|
[13524] | 94 |
|
---|
[13538] | 95 | }
|
---|
[13524] | 96 | }
|
---|