Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2915 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

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