Free cookie consent management tool by TermsFeed Policy Generator

source: branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageService.cs @ 13545

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

added new empty project for test on hive
changed scheme for ip determination
changed handling of queues in messagehandler

File size: 1.8 KB
Line 
1using System;
2using System.Net;
3using System.Net.Sockets;
4using System.ServiceModel;
5using System.Threading;
6using DistributedGA.Core.Domain;
7using DistributedGA.Core.Interface;
8
9namespace DistributedGA.Core.Implementation {
10  public class WcfMessageService : IMessageService {
11    public event EventHandler<MessageRecieveEventArgs> OnPopulationRecieved;
12
13    private static ManualResetEvent _ResetEvent = new ManualResetEvent(false);
14
15    private IMessageContract messageContract = null;
16
17    public int Init(string ip) {
18      int port = 0;
19      port = FreeTcpPort();
20
21      messageContract = new MessageContractImpl();
22      messageContract.MessageRecieved += new EventHandler<MessageRecieveEventArgs>(OnMessageRecieved);
23
24      var serviceUrl = "DistributedGA.svc";
25      new Thread(() => {
26        var baseUri = new Uri(string.Concat("http://", ip, ":", port, "/DistributedGA"));
27        var serviceUri = new Uri(baseUri, serviceUrl);
28        BasicHttpBinding binding = new BasicHttpBinding();
29        //using (var host = new ServiceHost(typeof(MessageContractImpl), serviceUri))
30
31        using (var host = new ServiceHost(messageContract, serviceUri)) {
32          host.AddServiceEndpoint(typeof(IMessageContract), binding, serviceUri);
33
34          host.Open();
35
36          _ResetEvent.WaitOne();
37        }
38      }).Start();
39      //close service again:
40      //  _ResetEvent.Set();
41      return port;
42    }
43
44    private int FreeTcpPort() {
45      TcpListener l = new TcpListener(IPAddress.Loopback, 0);
46      l.Start();
47      int port = ((IPEndPoint)l.LocalEndpoint).Port;
48      l.Stop();
49      return port;
50    }
51
52    private void OnMessageRecieved(object sender, MessageRecieveEventArgs e) {
53      if (OnPopulationRecieved != null) {
54        OnPopulationRecieved(this, e);
55      }
56    }
57
58  }
59}
Note: See TracBrowser for help on using the repository browser.