Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13553 was 13553, checked in by thasling, 8 years ago

changed a few method names and other refactoring

File size: 4.6 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.OnDataRecieved += 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 PublishDataToNetwork(SolutionInfo[] data) {
68      try {
69        foreach (PeerInfo peer in peerListManager.GetPeerList()) {
70          //HACK: manipulate for monitoring in test
71          foreach (SolutionInfo si in data) {
72            si.IterationNumber = ownInstance.Port;
73          }
74          //maybe something will go wrong during network communication...
75          try {
76            sender.SendData(peer, data);
77          } catch (Exception ex) {
78            AddError("PeerNetworkMessageHandler.PublishMigrationInfo(during sending to one peer!)", ex);
79          }
80        }
81      } catch (Exception ex) {
82        AddError("PeerNetworkMessageHandler.PublishMigrationInfo", ex);
83      }
84    }
85
86    public SolutionInfo[] GetDataFromNetwork() {
87      try {
88        List<SolutionInfo> res = new List<SolutionInfo>();
89        SolutionInfo item = null;
90        lock (activeQueueLocker) {
91          var tmp = readQueue;
92          readQueue = writeQueue;
93          writeQueue = tmp;
94        }
95
96        //creating resultset
97        while (!readQueue.IsEmpty) {
98          if (readQueue.TryDequeue(out item)) {
99            res.Add(item);
100          }
101        }
102        return res.ToArray();
103      } catch (Exception ex) {
104        AddError("PeerNetworkMessageHandler.GetMigrationInfo", ex);
105        return null;
106      }
107    }
108
109    public PeerInfo GetPeerInfo() {
110      return ownInstance;
111    }
112
113    public List<PeerInfo> GetCurrentNetwork() {
114      return peerListManager.GetPeerList();
115    }
116
117    private string GetExternalIpAddress() {
118      try {
119        var strHostName = Dns.GetHostName();
120        // Then using host name, get the IP address list..
121        IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
122        IPAddress[] addr = ipEntry.AddressList;
123
124        return addr
125          .Select(ip => ip.ToString())
126          .First(str => str.StartsWith(ConfigurationManager.AppSettings["LanIpPrefix"]));
127      } catch { return null; }
128    }
129
130    private void OnPopulationRecieved(object sender, MessageRecieveEventArgs e) {
131      if (e != null) {
132        lock (activeQueueLocker) {
133          foreach (SolutionInfo si in e.data) {
134            writeQueue.Enqueue(si);
135          }
136        }
137      }
138    }
139
140    private void AddError(string source, Exception ex) {
141      if (peerListManager != null) {
142        try {
143          peerListManager.SendLogToServer(string.Concat("Source: ", source, ", Exception: ", ex.Message));
144        } catch { }
145      }
146    }
147
148  }
149}
Note: See TracBrowser for help on using the repository browser.