Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2615:
delete unneeded project, appended code style

File size: 4.5 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<byte[]> writeQueue;
24    private ConcurrentQueue<byte[]> 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 = GetInternalIpAddress(lanIpPrefix),
35          Port = 0,
36          ProblemInstance = "TestProblem"
37        }; // TODO: get own peerinfo
38
39        writeQueue = new ConcurrentQueue<byte[]>();
40        readQueue = new ConcurrentQueue<byte[]>();
41
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.Init(ownInstance, contactServerUrl);
49
50        sender = new WcfMessageSender();
51        sender.Init(ownInstance);
52
53      }
54      catch (Exception ex) {
55        AddError("PeerNetworkMessageHandler.Init", ex);
56      }
57    }
58
59    public void Dispose() {
60      try {
61        host.Dispose();
62        sender.Dispose();
63        peerListManager.Dispose();
64      }
65      catch (Exception ex) {
66        AddError("PeerNetworkMessageHandler.Dispose", ex);
67      }
68    }
69
70    public void PublishDataToNetwork(byte[][] data) {
71      try {
72        foreach (PeerInfo peer in peerListManager.GetPeerList()) {
73          //maybe something will go wrong during network communication...
74          try {
75            sender.SendData(peer, data);
76          }
77          catch (Exception ex) {
78            AddError("PeerNetworkMessageHandler.PublishDataToNetwork(during sending to one peer!)", ex);
79          }
80        }
81      }
82      catch (Exception ex) {
83        AddError("PeerNetworkMessageHandler.PublishDataToNetwork", ex);
84      }
85    }
86
87    public byte[][] GetDataFromNetwork() {
88      try {
89        List<byte[]> res = new List<byte[]>();
90        byte[] item = null;
91        lock (activeQueueLocker) {
92          var tmp = readQueue;
93          readQueue = writeQueue;
94          writeQueue = tmp;
95        }
96
97        //creating resultset
98        while (!readQueue.IsEmpty) {
99          if (readQueue.TryDequeue(out item)) {
100            res.Add(item);
101          }
102        }
103        return res.ToArray();
104      }
105      catch (Exception ex) {
106        AddError("PeerNetworkMessageHandler.GetDataFromNetwork", ex);
107        return null;
108      }
109    }
110
111    public PeerInfo GetPeerInfo() {
112      return ownInstance;
113    }
114
115    public List<PeerInfo> GetCurrentNetwork() {
116      return peerListManager.GetPeerList();
117    }
118
119    private string GetInternalIpAddress(string ipPrefix) {
120      try {
121        var strHostName = Dns.GetHostName();
122        // Then using host name, get the IP address list..
123        IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
124        IPAddress[] addr = ipEntry.AddressList;
125
126        return addr
127          .Select(ip => ip.ToString())
128          .First(str => str.StartsWith(ipPrefix));
129      }
130      catch { return null; }
131    }
132
133    private void OnPopulationRecieved(object sender, MessageRecieveEventArgs e) {
134      if (e != null) {
135        lock (activeQueueLocker) {
136          foreach (byte[] item in e.data) {
137            writeQueue.Enqueue(item);
138          }
139        }
140      }
141    }
142
143    private void AddError(string source, Exception ex) {
144      if (peerListManager != null) {
145        try {
146          peerListManager.SendLogToServer(string.Concat("Source: ", source, ", Exception: ", ex.Message));
147        }
148        catch { }
149      }
150    }
151
152  }
153}
Note: See TracBrowser for help on using the repository browser.