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

Last change on this file since 1738 was 1738, checked in by asimon, 12 years ago

error correction (ticket #537)

File size: 5.2 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 = new NetTcpBinding(SecurityMode.None, true);
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      binding.MaxReceivedMessageSize = 5000000;
48      binding.SendTimeout = new TimeSpan(0, 0, 0, 0, 20);
49      string curServiceHost = "";
50      Uri uriTcp;
51      String result = "";
52      ISecurityManager[] securityManagerInstances = discService.GetInstances<ISecurityManager>();
53      IPermissionManager[] permissionManagerInstances = discService.GetInstances<IPermissionManager>();
54      ServiceHost serviceHost = null;
55      switch (svc) {
56        case Services.PermissionManager:
57          if (permissionManagerInstances.Length > 0) {
58            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/SecurityServer/");
59            serviceHost = new ServiceHost(permissionManagerInstances[0].GetType(), uriTcp);
60            serviceHost.AddServiceEndpoint(typeof(IPermissionManager), binding, STR_PermissionManager);
61            curServiceHost = STR_PermissionManager;
62            result = uriTcp.ToString() + STR_PermissionManager;
63          }
64          break;
65        case Services.SecurityManager:
66          if (securityManagerInstances.Length > 0) {
67            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/SecurityServer/");
68            serviceHost = new ServiceHost(securityManagerInstances[0].GetType(), uriTcp);
69            serviceHost.AddServiceEndpoint(typeof(ISecurityManager), binding, STR_SecurityManager);
70            curServiceHost = STR_SecurityManager;
71            result = uriTcp.ToString() + STR_SecurityManager;
72          }
73          break;
74        case Services.All:
75          throw new InvalidOperationException("Not supported!");
76        default:
77          return null;
78      }
79      if ((serviceHost != null) && (!String.IsNullOrEmpty(curServiceHost))) {
80        AddMexEndpoint(serviceHost);
81    //    WcfSettings.SetServiceCertificate(serviceHost);
82        serviceHost.Open();
83        runningServices.Add(curServiceHost, serviceHost);
84        return result;
85      } else
86        return null;
87    }
88
89    private void StopService(Services svc) {
90      ServiceHost svcHost = null;
91      switch (svc) {
92        case Services.PermissionManager:
93          runningServices.TryGetValue(STR_PermissionManager, out svcHost);
94          break;
95        case Services.SecurityManager:
96          runningServices.TryGetValue(STR_SecurityManager, out svcHost);
97          break;
98        case Services.All:
99          foreach (KeyValuePair<string, ServiceHost> item in runningServices)
100            item.Value.Close();
101          return;
102        default:
103          throw new InvalidOperationException("Not supported!");
104      }
105      svcHost.Close();
106    }
107
108    public override void Run() {
109      IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
110      int index = 0;
111      if (System.Environment.OSVersion.Version.Major >= 6) {
112        for (index = addresses.Length - 1; index >= 0; index--)
113          if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
114            break;
115      }
116     
117      //Start services and record their base address
118      Dictionary<string, String> baseAddrDict = new Dictionary<string, String>();
119      baseAddrDict.Add(STR_PermissionManager,
120        StartService(Services.PermissionManager, addresses[index], DEFAULT_PORT_PM));
121      baseAddrDict.Add(STR_SecurityManager,
122        StartService(Services.SecurityManager, addresses[index], DEFAULT_PORT_SM));
123
124      SecurityServer securityServer = new SecurityServer(baseAddrDict);
125
126      Application.Run(securityServer);
127
128      StopService(Services.All);
129    }
130  }                                                                                     
131}
Note: See TracBrowser for help on using the repository browser.