Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3484 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
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.PluginInfrastructure;
4using System.Text;
5using System.ServiceModel;
6using System.ServiceModel.Description;
7using System.Net;
8using System.Linq;
9using HeuristicLab.Security.Contracts.Interfaces;
10using System.Windows.Forms;
11
12namespace HeuristicLab.Security.Server {
13
14  [Application("Security Server", "Server application for the security.", true)]
15  public class SecurityServerApplication : ApplicationBase {
16    public const string STR_PermissionManager = "PermissionManager";
17    public const string STR_SecurityManager = "SecurityManager";
18
19    int DEFAULT_PORT_SM = 9111;
20    int DEFAULT_PORT_PM = 9112;
21
22    private Dictionary<string, ServiceHost> runningServices = new Dictionary<string, ServiceHost>();
23    private NetTcpBinding binding = (NetTcpBinding)HeuristicLab.Hive.Contracts.WcfSettings.GetBinding();
24
25    private enum Services {
26      SecurityManager,
27      PermissionManager,
28      All
29    }
30
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
44    private String StartService(Services svc, IPAddress ipAddress, int port) {
45      string curServiceHost = "";
46      Uri uriTcp;
47      String result = "";
48      IEnumerable<ISecurityManager> securityManagerInstances = ApplicationManager.Manager.GetInstances<ISecurityManager>();
49      IEnumerable<IPermissionManager> permissionManagerInstances = ApplicationManager.Manager.GetInstances<IPermissionManager>();
50      ServiceHost serviceHost = null;
51      switch (svc) {
52        case Services.PermissionManager:
53          if (permissionManagerInstances.Count() > 0) {
54            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/SecurityServer/");
55            serviceHost = new ServiceHost(permissionManagerInstances.First().GetType(), uriTcp);
56            serviceHost.AddServiceEndpoint(typeof(IPermissionManager), binding, STR_PermissionManager);
57            curServiceHost = STR_PermissionManager;
58            result = uriTcp.ToString() + STR_PermissionManager;
59          }
60          break;
61        case Services.SecurityManager:
62          if (securityManagerInstances.Count() > 0) {
63            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/SecurityServer/");
64            serviceHost = new ServiceHost(securityManagerInstances.First().GetType(), uriTcp);
65            serviceHost.AddServiceEndpoint(typeof(ISecurityManager), binding, STR_SecurityManager);
66            curServiceHost = STR_SecurityManager;
67            result = uriTcp.ToString() + STR_SecurityManager;
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);
77        //    WcfSettings.SetServiceCertificate(serviceHost);
78        serviceHost.Open();
79        runningServices.Add(curServiceHost, serviceHost);
80        return result;
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      }
112
113      //Start services and record their base address
114      Dictionary<string, String> baseAddrDict = new Dictionary<string, String>();
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    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.