1 | using DistributedGA.Core.Domain;
|
---|
2 | using DistributedGA.Core.Interface;
|
---|
3 | using System;
|
---|
4 | using System.Collections.Concurrent;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Net;
|
---|
8 | using System.Text;
|
---|
9 | using System.Text.RegularExpressions;
|
---|
10 | using System.Threading.Tasks;
|
---|
11 |
|
---|
12 | namespace DistributedGA.Core.Implementation
|
---|
13 | {
|
---|
14 | public class PeerNetworkMessageHandler : IMessageHandler
|
---|
15 | {
|
---|
16 | //own peer-instance Information
|
---|
17 | private PeerInfo ownInstance = null;
|
---|
18 |
|
---|
19 | //uses peer-list from IPeerListManager to decide which peers to contact
|
---|
20 | private IPeerListManager peerListManager;
|
---|
21 |
|
---|
22 | //uses IMessageSender to send populations to peers
|
---|
23 | private IMessageSender sender = null;
|
---|
24 |
|
---|
25 | //provides current population for the higher layer IMigrationOperator
|
---|
26 | //to queues are used to gather and and provide population more efficiently
|
---|
27 | private ConcurrentQueue<SolutionInfo> collectedResults1;
|
---|
28 | private ConcurrentQueue<SolutionInfo> collectedResults2;
|
---|
29 | private bool collectingInFirstQueue = true; //currently first queue is filled with recieved populations
|
---|
30 |
|
---|
31 | //uses IMessageService for recieving population from one peer at once
|
---|
32 | private IMessageService host = null;
|
---|
33 |
|
---|
34 | public void Init()
|
---|
35 | {
|
---|
36 | try
|
---|
37 | {
|
---|
38 | ownInstance = new PeerInfo()
|
---|
39 | {
|
---|
40 | IpAddress = GetExternalIpAddress(),
|
---|
41 | Port = 0,
|
---|
42 | ProblemInstance = "TestProblem"
|
---|
43 | }; // TODO: get own peerinfo
|
---|
44 |
|
---|
45 | collectedResults1 = new ConcurrentQueue<SolutionInfo>();
|
---|
46 | collectedResults2 = new ConcurrentQueue<SolutionInfo>();
|
---|
47 |
|
---|
48 | host = new WcfMessageService();
|
---|
49 | ownInstance.Port = host.Init(ownInstance.IpAddress); //getting port, on which service is hostet
|
---|
50 | host.OnPopulationRecieved += new EventHandler<MessageRecieveEventArgs>(OnPopulationRecieved);
|
---|
51 |
|
---|
52 | peerListManager = new WcfPeerListManager();
|
---|
53 | //peerListManager = new TestPeerListManager();
|
---|
54 | peerListManager.Init(ownInstance);
|
---|
55 |
|
---|
56 | sender = new WcfMessageSender();
|
---|
57 | sender.Init(ownInstance);
|
---|
58 |
|
---|
59 | }
|
---|
60 | catch (Exception ex)
|
---|
61 | {
|
---|
62 | AddError("PeerNetworkMessageHandler.Init", ex);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void PublishMigrationInfo(SolutionInfo[] population)
|
---|
67 | {
|
---|
68 | try
|
---|
69 | {
|
---|
70 | foreach (PeerInfo peer in peerListManager.GetPeerList())
|
---|
71 | {
|
---|
72 | //maybe something will go wrong during network communication...
|
---|
73 | try
|
---|
74 | {
|
---|
75 | //TODO: NEEDED WHEN PEER IS IN LAN, TOO????
|
---|
76 | //Note: If IP is same as own: it is a "localhost"
|
---|
77 | if(peer.IpAddress.Equals(ownInstance.IpAddress))
|
---|
78 | {
|
---|
79 | peer.IpAddress = "localhost";
|
---|
80 | }
|
---|
81 | sender.SendPopulation(peer, population);
|
---|
82 |
|
---|
83 | }
|
---|
84 | catch (Exception ex)
|
---|
85 | {
|
---|
86 | //TBD
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | catch (Exception ex)
|
---|
91 | {
|
---|
92 | AddError("PeerNetworkMessageHandler.PublishMigrationInfo", ex);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public SolutionInfo[] GetMigrationInfo()
|
---|
97 | {
|
---|
98 | try
|
---|
99 | {
|
---|
100 | List<SolutionInfo> res = new List<SolutionInfo>();
|
---|
101 | ConcurrentQueue<SolutionInfo> source = null;
|
---|
102 | SolutionInfo item = null;
|
---|
103 | if (collectingInFirstQueue)
|
---|
104 | {
|
---|
105 | collectingInFirstQueue = false; //switching queue
|
---|
106 | source = collectedResults1;
|
---|
107 | }
|
---|
108 | else
|
---|
109 | {
|
---|
110 | collectingInFirstQueue = true; //switching queue
|
---|
111 | source = collectedResults2;
|
---|
112 | }
|
---|
113 | //creating resultset
|
---|
114 | while (!source.IsEmpty)
|
---|
115 | {
|
---|
116 | if (source.TryDequeue(out item))
|
---|
117 | {
|
---|
118 | res.Add(item);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | return res.ToArray();
|
---|
122 | }
|
---|
123 | catch (Exception ex)
|
---|
124 | {
|
---|
125 | AddError("PeerNetworkMessageHandler.GetMigrationInfo", ex);
|
---|
126 | return null;
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | public PeerInfo GetPeerInfo()
|
---|
131 | {
|
---|
132 | return ownInstance;
|
---|
133 | }
|
---|
134 |
|
---|
135 | public List<PeerInfo> GetCurrentNetwork()
|
---|
136 | {
|
---|
137 | return peerListManager.GetPeerList();
|
---|
138 | }
|
---|
139 |
|
---|
140 | private string GetExternalIpAddress()
|
---|
141 | {
|
---|
142 | try
|
---|
143 | {
|
---|
144 | string externalIP;
|
---|
145 | externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
|
---|
146 | externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
|
---|
147 | .Matches(externalIP)[0].ToString();
|
---|
148 | return externalIP;
|
---|
149 | }
|
---|
150 | catch { return null; }
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void OnPopulationRecieved(object sender, MessageRecieveEventArgs e)
|
---|
154 | {
|
---|
155 | if (e != null)
|
---|
156 | {
|
---|
157 | foreach (SolutionInfo si in e.Population)
|
---|
158 | {
|
---|
159 | if (collectingInFirstQueue)
|
---|
160 | {
|
---|
161 | collectedResults1.Enqueue(si);
|
---|
162 | }
|
---|
163 | else
|
---|
164 | {
|
---|
165 | collectedResults2.Enqueue(si);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | private void AddError(string source, Exception ex)
|
---|
172 | {
|
---|
173 | throw new NotImplementedException();
|
---|
174 | }
|
---|
175 |
|
---|
176 | }
|
---|
177 | }
|
---|