Free cookie consent management tool by TermsFeed Policy Generator

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

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

#586 fixed wcf binding

File size: 5.1 KB
Line 
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>();
25    private NetTcpBinding binding = (NetTcpBinding)HeuristicLab.Hive.Contracts.WcfSettings.GetBinding();
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
46    private String StartService(Services svc, IPAddress ipAddress, int port) {
47      string curServiceHost = "";
48      Uri uriTcp;
49      String result = "";
50      ISecurityManager[] securityManagerInstances = discService.GetInstances<ISecurityManager>();
51      IPermissionManager[] permissionManagerInstances = discService.GetInstances<IPermissionManager>();
52      ServiceHost serviceHost = null;
53      switch (svc) {
54        case Services.PermissionManager:
55          if (permissionManagerInstances.Length > 0) {
56            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/SecurityServer/");
57            serviceHost = new ServiceHost(permissionManagerInstances[0].GetType(), uriTcp);
58            serviceHost.AddServiceEndpoint(typeof(IPermissionManager), binding, STR_PermissionManager);
59            curServiceHost = STR_PermissionManager;
60            result = uriTcp.ToString() + STR_PermissionManager;
61          }
62          break;
63        case Services.SecurityManager:
64          if (securityManagerInstances.Length > 0) {
65            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/SecurityServer/");
66            serviceHost = new ServiceHost(securityManagerInstances[0].GetType(), uriTcp);
67            serviceHost.AddServiceEndpoint(typeof(ISecurityManager), binding, STR_SecurityManager);
68            curServiceHost = STR_SecurityManager;
69            result = uriTcp.ToString() + STR_SecurityManager;
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);
82        return result;
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
116      Dictionary<string, String> baseAddrDict = new Dictionary<string, String>();
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    }
128  }                                                                                     
129}
Note: See TracBrowser for help on using the repository browser.