- Timestamp:
- 06/08/16 17:02:06 (9 years ago)
- Location:
- branches/thasling/DistributedGA/DistributedGA.ContactServer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/thasling/DistributedGA/DistributedGA.ContactServer/ContactServiceImpl.cs
r13557 r13887 8 8 using DistributedGA.Core.Domain; 9 9 10 namespace DistributedGA.ContactServer { 10 namespace DistributedGA.ContactServer 11 { 11 12 12 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 13 public class ContactServiceImpl : IContactService { 13 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 14 public class ContactServiceImpl : IContactService 15 { 14 16 15 private ConcurrentDictionary<PeerInfo, DateTime> allPeers = null;17 private ConcurrentDictionary<PeerInfo, DateTime> allPeers = null; 16 18 17 private Timer timer = null;19 private Timer timer = null; 18 20 19 private Object logLock = new Object();21 private Object logLock = new Object(); 20 22 21 public ContactServiceImpl() { 22 allPeers = new ConcurrentDictionary<PeerInfo, DateTime>(); 23 public ContactServiceImpl() 24 { 25 allPeers = new ConcurrentDictionary<PeerInfo, DateTime>(); 23 26 24 timer = new Timer(60 * 1000); //each hour 25 timer.Elapsed += CleanUpContactTable; 26 timer.Start(); 27 timer = new Timer(1000 * 60 * 60); //each hour 28 timer.Elapsed += CleanUpContactTable; 29 timer.Start(); 30 } 31 32 public void RegisterPeer(PeerInfo source) 33 { 34 try 35 { 36 UpdateHeartbeat(source); 37 } 38 catch (Exception ex) 39 { 40 AddError("ContactServiceImpl.RegisterPeer", ex); 41 } 42 } 43 44 public List<PeerInfo> GetPeerList(PeerInfo source) 45 { 46 try 47 { 48 UpdateHeartbeat(source); 49 //only return peers of the same work group and not the sender itself 50 return allPeers.Keys.Where(x => 51 { 52 if (source.ProblemInstance.Equals(x.ProblemInstance) && 53 (!(x.IpAddress.Equals(source.IpAddress) && (x.Port.Equals(source.Port))))) 54 return true; 55 else 56 return false; 57 }).ToList(); 58 } 59 catch (Exception ex) 60 { 61 AddError("ContactServiceImpl.GetPeerList", ex); 62 return null; 63 } 64 } 65 66 public void MakeLog(PeerInfo source, string msg) 67 { 68 try 69 { 70 // TODO 71 lock (logLock) 72 { 73 File.AppendAllText("Log.txt", string.Concat(source.IpAddress, ":", source.Port, ",", source.ProblemInstance, ",", msg, Environment.NewLine)); 74 } 75 } 76 catch (Exception ex) 77 { 78 //Nothing to do because maybe called from adderror 79 } 80 } 81 82 public void UpdateHeartbeat(PeerInfo source) 83 { 84 try 85 { 86 Console.WriteLine("hb from {0}:{1}", source.IpAddress, source.Port); 87 DateTime now = DateTime.Now; 88 allPeers.AddOrUpdate(source, now, (k, v) => v = now); 89 } 90 catch (Exception ex) 91 { 92 AddError("ContactServiceImpl.UpdateHeartbeat", ex); 93 } 94 } 95 96 private void CleanUpContactTable(object sender, ElapsedEventArgs e) 97 { 98 DateTime deadline = DateTime.Now; 99 //collect items to remove 100 List<PeerInfo> itemsToDelete = new List<PeerInfo>(); 101 foreach (PeerInfo pi in allPeers.Keys) 102 { 103 DateTime tmp; 104 if (allPeers.TryGetValue(pi, out tmp)) 105 { 106 if (tmp.AddHours(1f) < deadline) 107 { 108 //if (tmp < deadline.AddHours(1f)) { 109 itemsToDelete.Add(pi); 110 } 111 } 112 } 113 //remove items 114 foreach (PeerInfo pi in itemsToDelete) 115 { 116 DateTime tmp; 117 allPeers.TryRemove(pi, out tmp); 118 } 119 } 120 121 private void AddError(string source, Exception ex) 122 { 123 MakeLog(new PeerInfo() { ProblemInstance = "ContactServer Error at " + source }, ex.Message); 124 } 125 27 126 } 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 itself41 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 else46 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 // TODO57 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 adderror62 }63 }64 65 private void UpdateHeartbeat(PeerInfo source) {66 Console.WriteLine("hb from {0}:{1}", source.IpAddress, source.Port);67 DateTime now = DateTime.Now;68 allPeers.AddOrUpdate(source, now, (k, v) => v = now);69 }70 71 private void CleanUpContactTable(object sender, ElapsedEventArgs e) {72 DateTime deadline = DateTime.Now;73 //collect items to remove74 List<PeerInfo> itemsToDelete = new List<PeerInfo>();75 foreach (PeerInfo pi in allPeers.Keys) {76 DateTime tmp;77 if (allPeers.TryGetValue(pi, out tmp)) {78 if (tmp.AddHours(1f) < deadline) {79 //if (tmp < deadline.AddHours(1f)) {80 itemsToDelete.Add(pi);81 }82 }83 }84 //remove items85 foreach (PeerInfo pi in itemsToDelete) {86 DateTime tmp;87 allPeers.TryRemove(pi, out tmp);88 }89 }90 91 private void AddError(string source, Exception ex) {92 MakeLog(new PeerInfo() { ProblemInstance = "ContactServer Error at " + source }, ex.Message);93 }94 95 }96 127 } -
branches/thasling/DistributedGA/DistributedGA.ContactServer/IContactService.cs
r13524 r13887 19 19 20 20 [OperationContract] 21 void MakeLog(PeerInfo source, string msg); //used to log all peers at a single location 22 21 void UpdateHeartbeat(PeerInfo source); //Sends heartbeat to contact-server 22 23 [OperationContract] 24 void MakeLog(PeerInfo source, string msg); //Used to log all peers at a single location 25 23 26 } 24 27 }
Note: See TracChangeset
for help on using the changeset viewer.