Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2615: implemented Dispose()-Method in all classes
also sending a message with the wcf-sender class was rewritten, because of Dispose
added new log entries when server removes peers from its dicionary

File size: 5.7 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{
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
48                host = new WcfMessageService();
49                ownInstance.Port = host.Init(ownInstance.IpAddress); //getting port, on which service is hostet
50                host.OnDataRecieved += new EventHandler<MessageRecieveEventArgs>(OnPopulationRecieved);
51
52                peerListManager = new WcfPeerListManager();
53                peerListManager.Init(ownInstance, contactServerUrl);
54
55                sender = new WcfMessageSender();
56                sender.Init(ownInstance);
57
58            }
59            catch (Exception ex)
60            {
61                AddError("PeerNetworkMessageHandler.Init", ex);
62            }
63        }
64
65        public void Dispose()
66        {
67            try
68            {
69                host.Dispose();
70                sender.Dispose();
71                peerListManager.Dispose();
72            }
73            catch (Exception ex)
74            {
75                AddError("PeerNetworkMessageHandler.Dispose", ex);
76            }
77        }
78
79        public void PublishDataToNetwork(byte[][] data)
80        {
81            try
82            {
83                foreach (PeerInfo peer in peerListManager.GetPeerList())
84                {
85                    //maybe something will go wrong during network communication...
86                    try
87                    {
88                        sender.SendData(peer, data);
89                    }
90                    catch (Exception ex)
91                    {
92                        AddError("PeerNetworkMessageHandler.PublishDataToNetwork(during sending to one peer!)", ex);
93                    }
94                }
95            }
96            catch (Exception ex)
97            {
98                AddError("PeerNetworkMessageHandler.PublishDataToNetwork", ex);
99            }
100        }
101
102        public byte[][] GetDataFromNetwork()
103        {
104            try
105            {
106                List<byte[]> res = new List<byte[]>();
107                byte[] item = null;
108                lock (activeQueueLocker)
109                {
110                    var tmp = readQueue;
111                    readQueue = writeQueue;
112                    writeQueue = tmp;
113                }
114
115                //creating resultset
116                while (!readQueue.IsEmpty)
117                {
118                    if (readQueue.TryDequeue(out item))
119                    {
120                        res.Add(item);
121                    }
122                }
123                return res.ToArray();
124            }
125            catch (Exception ex)
126            {
127                AddError("PeerNetworkMessageHandler.GetDataFromNetwork", ex);
128                return null;
129            }
130        }
131
132        public PeerInfo GetPeerInfo()
133        {
134            return ownInstance;
135        }
136
137        public List<PeerInfo> GetCurrentNetwork()
138        {
139            return peerListManager.GetPeerList();
140        }
141
142        private string GetInternalIpAddress(string ipPrefix)
143        {
144            try
145            {
146                var strHostName = Dns.GetHostName();
147                // Then using host name, get the IP address list..
148                IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
149                IPAddress[] addr = ipEntry.AddressList;
150
151                return addr
152                  .Select(ip => ip.ToString())
153                  .First(str => str.StartsWith(ipPrefix));
154            }
155            catch { return null; }
156        }
157
158        private void OnPopulationRecieved(object sender, MessageRecieveEventArgs e)
159        {
160            if (e != null)
161            {
162                lock (activeQueueLocker)
163                {
164                    foreach (byte[] item in e.data)
165                    {
166                        writeQueue.Enqueue(item);
167                    }
168                }
169            }
170        }
171
172        private void AddError(string source, Exception ex)
173        {
174            if (peerListManager != null)
175            {
176                try
177                {
178                    peerListManager.SendLogToServer(string.Concat("Source: ", source, ", Exception: ", ex.Message));
179                }
180                catch { }
181            }
182        }
183
184    }
185}
Note: See TracBrowser for help on using the repository browser.