Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Security.Server/3.2/SecurityServerApplication.cs @ 2273

Last change on this file since 2273 was 1766, checked in by mbecirov, 16 years ago

#586 fixed wcf binding

File size: 5.1 KB
RevLine 
[1713]1using System;
2using System.Collections.Generic;
3using HeuristicLab.PluginInfrastructure;
4using System.Text;
5using System.ServiceModel;
6using System.ServiceModel.Description;
7using System.Net;
8using HeuristicLab.Security.Contracts.Interfaces;
9using System.Windows.Forms;
10
11namespace HeuristicLab.Security.Server {
12
13  [ClassInfo(Name = "Security Server",
14  Description = "Server application for the security.",
15  AutoRestart = true)]
16  public class SecurityServerApplication : ApplicationBase {
17    public const string STR_PermissionManager = "PermissionManager";
18    public const string STR_SecurityManager = "SecurityManager";   
19
20    int DEFAULT_PORT_SM = 9111;
21    int DEFAULT_PORT_PM = 9112;
22
23    private DiscoveryService discService = new DiscoveryService();
24    private Dictionary<string, ServiceHost> runningServices = new Dictionary<string, ServiceHost>();
[1766]25    private NetTcpBinding binding = (NetTcpBinding)HeuristicLab.Hive.Contracts.WcfSettings.GetBinding();
[1713]26
27    private enum Services {
28      SecurityManager,
29      PermissionManager,
30      All
31    }
32   
33    private bool AddMexEndpoint(ServiceHost serviceHost) {
34      if (serviceHost != null) {
35        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
36        serviceHost.Description.Behaviors.Add(behavior);
37
38        return serviceHost.AddServiceEndpoint(
39          typeof(IMetadataExchange),
40          MetadataExchangeBindings.CreateMexTcpBinding(),
41          "mex") != null;
42      } else
43        return false;
44    }
45
[1737]46    private String StartService(Services svc, IPAddress ipAddress, int port) {
[1713]47      string curServiceHost = "";
48      Uri uriTcp;
[1737]49      String result = "";
[1713]50      ISecurityManager[] securityManagerInstances = discService.GetInstances<ISecurityManager>();
51      IPermissionManager[] permissionManagerInstances = discService.GetInstances<IPermissionManager>();
52      ServiceHost serviceHost = null;
53      switch (svc) {
54        case Services.PermissionManager:
[1738]55          if (permissionManagerInstances.Length > 0) {
[1737]56            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/SecurityServer/");
[1713]57            serviceHost = new ServiceHost(permissionManagerInstances[0].GetType(), uriTcp);
58            serviceHost.AddServiceEndpoint(typeof(IPermissionManager), binding, STR_PermissionManager);
59            curServiceHost = STR_PermissionManager;
[1737]60            result = uriTcp.ToString() + STR_PermissionManager;
[1713]61          }
62          break;
63        case Services.SecurityManager:
64          if (securityManagerInstances.Length > 0) {
[1737]65            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/SecurityServer/");
[1713]66            serviceHost = new ServiceHost(securityManagerInstances[0].GetType(), uriTcp);
67            serviceHost.AddServiceEndpoint(typeof(ISecurityManager), binding, STR_SecurityManager);
68            curServiceHost = STR_SecurityManager;
[1737]69            result = uriTcp.ToString() + STR_SecurityManager;
[1713]70          }
71          break;
72        case Services.All:
73          throw new InvalidOperationException("Not supported!");
74        default:
75          return null;
76      }
77      if ((serviceHost != null) && (!String.IsNullOrEmpty(curServiceHost))) {
78        AddMexEndpoint(serviceHost);
79    //    WcfSettings.SetServiceCertificate(serviceHost);
80        serviceHost.Open();
81        runningServices.Add(curServiceHost, serviceHost);
[1737]82        return result;
[1713]83      } else
84        return null;
85    }
86
87    private void StopService(Services svc) {
88      ServiceHost svcHost = null;
89      switch (svc) {
90        case Services.PermissionManager:
91          runningServices.TryGetValue(STR_PermissionManager, out svcHost);
92          break;
93        case Services.SecurityManager:
94          runningServices.TryGetValue(STR_SecurityManager, out svcHost);
95          break;
96        case Services.All:
97          foreach (KeyValuePair<string, ServiceHost> item in runningServices)
98            item.Value.Close();
99          return;
100        default:
101          throw new InvalidOperationException("Not supported!");
102      }
103      svcHost.Close();
104    }
105
106    public override void Run() {
107      IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
108      int index = 0;
109      if (System.Environment.OSVersion.Version.Major >= 6) {
110        for (index = addresses.Length - 1; index >= 0; index--)
111          if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
112            break;
113      }
114     
115      //Start services and record their base address
[1737]116      Dictionary<string, String> baseAddrDict = new Dictionary<string, String>();
[1713]117      baseAddrDict.Add(STR_PermissionManager,
118        StartService(Services.PermissionManager, addresses[index], DEFAULT_PORT_PM));
119      baseAddrDict.Add(STR_SecurityManager,
120        StartService(Services.SecurityManager, addresses[index], DEFAULT_PORT_SM));
121
122      SecurityServer securityServer = new SecurityServer(baseAddrDict);
123
124      Application.Run(securityServer);
125
126      StopService(Services.All);
127    }
[1738]128  }                                                                                     
[1713]129}
Note: See TracBrowser for help on using the repository browser.