Free cookie consent management tool by TermsFeed Policy Generator

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

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

Connectionstring to peerlistserver now in app.config

File size: 4.9 KB
Line 
1using System;
2using System.Collections.Concurrent;
3using System.Collections.Generic;
4using System.Net;
5using System.Text.RegularExpressions;
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 ConcurrentQueue<SolutionInfo> collectedResults1;
23    private ConcurrentQueue<SolutionInfo> collectedResults2;
24    private bool collectingInFirstQueue = true; //currently first queue is filled with recieved populations
25
26    //uses IMessageService for recieving population from one peer at once
27    private IMessageService host = null;
28
29    public void Init() {
30      try {
31        ownInstance = new PeerInfo() {
32          IpAddress = GetExternalIpAddress(),
33          Port = 0,
34          ProblemInstance = "TestProblem"
35        }; // TODO: get own peerinfo
36
37        collectedResults1 = new ConcurrentQueue<SolutionInfo>();
38        collectedResults2 = new ConcurrentQueue<SolutionInfo>();
39
40        host = new WcfMessageService();
41        ownInstance.Port = host.Init(ownInstance.IpAddress); //getting port, on which service is hostet
42        host.OnPopulationRecieved += new EventHandler<MessageRecieveEventArgs>(OnPopulationRecieved);
43
44        peerListManager = new WcfPeerListManager();
45        //peerListManager = new TestPeerListManager();
46        peerListManager.Init(ownInstance);
47
48        sender = new WcfMessageSender();
49        sender.Init(ownInstance);
50
51      } catch (Exception ex) {
52        AddError("PeerNetworkMessageHandler.Init", ex);
53      }
54    }
55
56    public void PublishMigrationInfo(SolutionInfo[] population) {
57      try {
58        foreach (PeerInfo peer in peerListManager.GetPeerList()) {
59          //HACK: manipulate for monitoring in test
60          foreach (SolutionInfo si in population) {
61            si.IterationNumber = ownInstance.Port;
62          }
63          //maybe something will go wrong during network communication...
64          try {
65            //TODO: NEEDED WHEN PEER IS IN LAN, TOO????
66            //Note: If IP is same as own: it is a "localhost"
67            if (peer.IpAddress.Equals(ownInstance.IpAddress)) {
68              peer.IpAddress = "localhost";
69            }
70            sender.SendPopulation(peer, population);
71
72          } catch (Exception ex) {
73            //TBD
74          }
75        }
76      } catch (Exception ex) {
77        AddError("PeerNetworkMessageHandler.PublishMigrationInfo", ex);
78      }
79    }
80
81    public SolutionInfo[] GetMigrationInfo() {
82      try {
83        List<SolutionInfo> res = new List<SolutionInfo>();
84        ConcurrentQueue<SolutionInfo> source = null;
85        SolutionInfo item = null;
86        if (collectingInFirstQueue) {
87          collectingInFirstQueue = false; //switching queue
88          source = collectedResults1;
89        } else {
90          collectingInFirstQueue = true; //switching queue
91          source = collectedResults2;
92        }
93        //creating resultset
94        while (!source.IsEmpty) {
95          if (source.TryDequeue(out item)) {
96            res.Add(item);
97          }
98        }
99        return res.ToArray();
100      } catch (Exception ex) {
101        AddError("PeerNetworkMessageHandler.GetMigrationInfo", ex);
102        return null;
103      }
104    }
105
106    public PeerInfo GetPeerInfo() {
107      return ownInstance;
108    }
109
110    public List<PeerInfo> GetCurrentNetwork() {
111      return peerListManager.GetPeerList();
112    }
113
114    private string GetExternalIpAddress() {
115      try {
116        string externalIP;
117        externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
118        externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
119                     .Matches(externalIP)[0].ToString();
120        return externalIP;
121      } catch { return null; }
122    }
123
124    private void OnPopulationRecieved(object sender, MessageRecieveEventArgs e) {
125      if (e != null) {
126        foreach (SolutionInfo si in e.Population) {
127          if (collectingInFirstQueue) {
128            collectedResults1.Enqueue(si);
129          } else {
130            collectedResults2.Enqueue(si);
131          }
132        }
133      }
134    }
135
136    private void AddError(string source, Exception ex) {
137      if (peerListManager != null) {
138        try {
139          peerListManager.SendLogToServer(string.Concat("Source: ", source, ", Exception: ", ex.Message));
140        } catch { }
141      }
142    }
143
144  }
145}
Note: See TracBrowser for help on using the repository browser.