[13538] | 1 | using System;
|
---|
[13524] | 2 | using System.Collections.Concurrent;
|
---|
| 3 | using System.Collections.Generic;
|
---|
[13541] | 4 | using System.Configuration;
|
---|
| 5 | using System.Linq;
|
---|
[13524] | 6 | using System.Net;
|
---|
[13538] | 7 | using DistributedGA.Core.Domain;
|
---|
| 8 | using DistributedGA.Core.Interface;
|
---|
[13524] | 9 |
|
---|
[13538] | 10 | namespace DistributedGA.Core.Implementation {
|
---|
| 11 | public class PeerNetworkMessageHandler : IMessageHandler {
|
---|
| 12 | //own peer-instance Information
|
---|
| 13 | private PeerInfo ownInstance = null;
|
---|
[13524] | 14 |
|
---|
[13538] | 15 | //uses peer-list from IPeerListManager to decide which peers to contact
|
---|
| 16 | private IPeerListManager peerListManager;
|
---|
[13524] | 17 |
|
---|
[13538] | 18 | //uses IMessageSender to send populations to peers
|
---|
| 19 | private IMessageSender sender = null;
|
---|
[13524] | 20 |
|
---|
[13538] | 21 | //provides current population for the higher layer IMigrationOperator
|
---|
| 22 | //to queues are used to gather and and provide population more efficiently
|
---|
[13541] | 23 | private object activeQueueLocker = new object();
|
---|
| 24 | private ConcurrentQueue<SolutionInfo> writeQueue;
|
---|
| 25 | private ConcurrentQueue<SolutionInfo> readQueue;
|
---|
[13524] | 26 |
|
---|
[13538] | 27 | //uses IMessageService for recieving population from one peer at once
|
---|
| 28 | private IMessageService host = null;
|
---|
[13524] | 29 |
|
---|
[13541] | 30 |
|
---|
| 31 |
|
---|
[13538] | 32 | public void Init() {
|
---|
| 33 | try {
|
---|
| 34 | ownInstance = new PeerInfo() {
|
---|
| 35 | IpAddress = GetExternalIpAddress(),
|
---|
| 36 | Port = 0,
|
---|
| 37 | ProblemInstance = "TestProblem"
|
---|
| 38 | }; // TODO: get own peerinfo
|
---|
[13524] | 39 |
|
---|
[13541] | 40 | writeQueue = new ConcurrentQueue<SolutionInfo>();
|
---|
| 41 | readQueue = new ConcurrentQueue<SolutionInfo>();
|
---|
[13524] | 42 |
|
---|
[13538] | 43 | host = new WcfMessageService();
|
---|
| 44 | ownInstance.Port = host.Init(ownInstance.IpAddress); //getting port, on which service is hostet
|
---|
| 45 | host.OnPopulationRecieved += new EventHandler<MessageRecieveEventArgs>(OnPopulationRecieved);
|
---|
[13524] | 46 |
|
---|
[13538] | 47 | peerListManager = new WcfPeerListManager();
|
---|
| 48 | //peerListManager = new TestPeerListManager();
|
---|
| 49 | peerListManager.Init(ownInstance);
|
---|
[13524] | 50 |
|
---|
[13538] | 51 | sender = new WcfMessageSender();
|
---|
| 52 | sender.Init(ownInstance);
|
---|
[13524] | 53 |
|
---|
[13538] | 54 | } catch (Exception ex) {
|
---|
| 55 | AddError("PeerNetworkMessageHandler.Init", ex);
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
[13524] | 58 |
|
---|
[13547] | 59 | public void Dispose() {
|
---|
| 60 | try {
|
---|
| 61 | host.Dispose();
|
---|
| 62 | } catch (Exception ex) {
|
---|
| 63 | AddError("PeerNetworkMessageHandler.Dispose", ex);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[13538] | 67 | public void PublishMigrationInfo(SolutionInfo[] population) {
|
---|
| 68 | try {
|
---|
| 69 | foreach (PeerInfo peer in peerListManager.GetPeerList()) {
|
---|
| 70 | //HACK: manipulate for monitoring in test
|
---|
| 71 | foreach (SolutionInfo si in population) {
|
---|
| 72 | si.IterationNumber = ownInstance.Port;
|
---|
| 73 | }
|
---|
| 74 | //maybe something will go wrong during network communication...
|
---|
| 75 | try {
|
---|
| 76 | //TODO: NEEDED WHEN PEER IS IN LAN, TOO????
|
---|
| 77 | //Note: If IP is same as own: it is a "localhost"
|
---|
| 78 | if (peer.IpAddress.Equals(ownInstance.IpAddress)) {
|
---|
| 79 | peer.IpAddress = "localhost";
|
---|
[13524] | 80 | }
|
---|
[13538] | 81 | sender.SendPopulation(peer, population);
|
---|
[13524] | 82 |
|
---|
[13538] | 83 | } catch (Exception ex) {
|
---|
| 84 | //TBD
|
---|
| 85 | }
|
---|
[13524] | 86 | }
|
---|
[13538] | 87 | } catch (Exception ex) {
|
---|
| 88 | AddError("PeerNetworkMessageHandler.PublishMigrationInfo", ex);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
[13524] | 91 |
|
---|
[13538] | 92 | public SolutionInfo[] GetMigrationInfo() {
|
---|
| 93 | try {
|
---|
| 94 | List<SolutionInfo> res = new List<SolutionInfo>();
|
---|
| 95 | SolutionInfo item = null;
|
---|
[13541] | 96 | lock (activeQueueLocker) {
|
---|
| 97 | var tmp = readQueue;
|
---|
| 98 | readQueue = writeQueue;
|
---|
| 99 | writeQueue = tmp;
|
---|
[13524] | 100 | }
|
---|
[13541] | 101 |
|
---|
[13538] | 102 | //creating resultset
|
---|
[13541] | 103 | while (!readQueue.IsEmpty) {
|
---|
| 104 | if (readQueue.TryDequeue(out item)) {
|
---|
[13538] | 105 | res.Add(item);
|
---|
| 106 | }
|
---|
[13524] | 107 | }
|
---|
[13538] | 108 | return res.ToArray();
|
---|
| 109 | } catch (Exception ex) {
|
---|
| 110 | AddError("PeerNetworkMessageHandler.GetMigrationInfo", ex);
|
---|
| 111 | return null;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
[13524] | 114 |
|
---|
[13538] | 115 | public PeerInfo GetPeerInfo() {
|
---|
| 116 | return ownInstance;
|
---|
| 117 | }
|
---|
[13524] | 118 |
|
---|
[13538] | 119 | public List<PeerInfo> GetCurrentNetwork() {
|
---|
| 120 | return peerListManager.GetPeerList();
|
---|
| 121 | }
|
---|
[13524] | 122 |
|
---|
[13538] | 123 | private string GetExternalIpAddress() {
|
---|
| 124 | try {
|
---|
[13541] | 125 | var strHostName = Dns.GetHostName();
|
---|
| 126 | // Then using host name, get the IP address list..
|
---|
| 127 | IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
|
---|
| 128 | IPAddress[] addr = ipEntry.AddressList;
|
---|
| 129 |
|
---|
| 130 | return addr
|
---|
| 131 | .Select(ip => ip.ToString())
|
---|
| 132 | .First(str => str.StartsWith(ConfigurationManager.AppSettings["LanIpPrefix"]));
|
---|
[13538] | 133 | } catch { return null; }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | private void OnPopulationRecieved(object sender, MessageRecieveEventArgs e) {
|
---|
| 137 | if (e != null) {
|
---|
[13541] | 138 | lock (activeQueueLocker) {
|
---|
| 139 | foreach (SolutionInfo si in e.Population) {
|
---|
| 140 | writeQueue.Enqueue(si);
|
---|
[13538] | 141 | }
|
---|
[13524] | 142 | }
|
---|
[13538] | 143 | }
|
---|
| 144 | }
|
---|
[13524] | 145 |
|
---|
[13538] | 146 | private void AddError(string source, Exception ex) {
|
---|
| 147 | if (peerListManager != null) {
|
---|
| 148 | try {
|
---|
| 149 | peerListManager.SendLogToServer(string.Concat("Source: ", source, ", Exception: ", ex.Message));
|
---|
| 150 | } catch { }
|
---|
| 151 | }
|
---|
[13524] | 152 | }
|
---|
[13547] | 153 |
|
---|
[13538] | 154 | }
|
---|
[13524] | 155 | }
|
---|