source: branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs @ 13547

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

Added Dispose() to MessageHandler and WCF service host

File size: 4.8 KB
Line 
1using System;
2using System.Collections.Concurrent;
3using System.Collections.Generic;
4using System.Configuration;
5using System.Linq;
6using System.Net;
7using DistributedGA.Core.Domain;
8using DistributedGA.Core.Interface;
9
10namespace DistributedGA.Core.Implementation {
11  public class PeerNetworkMessageHandler : IMessageHandler {
12    //own peer-instance Information
13    private PeerInfo ownInstance = null;
14
15    //uses peer-list from IPeerListManager to decide which peers to contact
16    private IPeerListManager peerListManager;
17
18    //uses IMessageSender to send populations to peers
19    private IMessageSender sender = null;
20
21    //provides current population for the higher layer IMigrationOperator
22    //to queues are used to gather and and provide population more efficiently
23    private object activeQueueLocker = new object();
24    private ConcurrentQueue<SolutionInfo> writeQueue;
25    private ConcurrentQueue<SolutionInfo> readQueue;
26
27    //uses IMessageService for recieving population from one peer at once
28    private IMessageService host = null;
29
30
31
32    public void Init() {
33      try {
34        ownInstance = new PeerInfo() {
35          IpAddress = GetExternalIpAddress(),
36          Port = 0,
37          ProblemInstance = "TestProblem"
38        }; // TODO: get own peerinfo
39
40        writeQueue = new ConcurrentQueue<SolutionInfo>();
41        readQueue = new ConcurrentQueue<SolutionInfo>();
42
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);
46
47        peerListManager = new WcfPeerListManager();
48        //peerListManager = new TestPeerListManager();
49        peerListManager.Init(ownInstance);
50
51        sender = new WcfMessageSender();
52        sender.Init(ownInstance);
53
54      } catch (Exception ex) {
55        AddError("PeerNetworkMessageHandler.Init", ex);
56      }
57    }
58
59    public void Dispose() {
60      try {
61        host.Dispose();
62      } catch (Exception ex) {
63        AddError("PeerNetworkMessageHandler.Dispose", ex);
64      }
65    }
66
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";
80            }
81            sender.SendPopulation(peer, population);
82
83          } catch (Exception ex) {
84            //TBD
85          }
86        }
87      } catch (Exception ex) {
88        AddError("PeerNetworkMessageHandler.PublishMigrationInfo", ex);
89      }
90    }
91
92    public SolutionInfo[] GetMigrationInfo() {
93      try {
94        List<SolutionInfo> res = new List<SolutionInfo>();
95        SolutionInfo item = null;
96        lock (activeQueueLocker) {
97          var tmp = readQueue;
98          readQueue = writeQueue;
99          writeQueue = tmp;
100        }
101
102        //creating resultset
103        while (!readQueue.IsEmpty) {
104          if (readQueue.TryDequeue(out item)) {
105            res.Add(item);
106          }
107        }
108        return res.ToArray();
109      } catch (Exception ex) {
110        AddError("PeerNetworkMessageHandler.GetMigrationInfo", ex);
111        return null;
112      }
113    }
114
115    public PeerInfo GetPeerInfo() {
116      return ownInstance;
117    }
118
119    public List<PeerInfo> GetCurrentNetwork() {
120      return peerListManager.GetPeerList();
121    }
122
123    private string GetExternalIpAddress() {
124      try {
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"]));
133      } catch { return null; }
134    }
135
136    private void OnPopulationRecieved(object sender, MessageRecieveEventArgs e) {
137      if (e != null) {
138        lock (activeQueueLocker) {
139          foreach (SolutionInfo si in e.Population) {
140            writeQueue.Enqueue(si);
141          }
142        }
143      }
144    }
145
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      }
152    }
153
154  }
155}
Note: See TracBrowser for help on using the repository browser.