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

Last change on this file since 13556 was 13556, checked in by thasling, 5 years ago

new hive project

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