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