Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server/3.2/HiveServerApplication.cs @ 1659

Last change on this file since 1659 was 1579, checked in by mbecirov, 16 years ago

#528: WCF Service secured - you need to install the certificate in order to run the application properly!

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Windows.Forms;
26using HeuristicLab.PluginInfrastructure;
27using System.ServiceModel;
28using System.ServiceModel.Description;
29using System.Net;
30using HeuristicLab.Hive.Contracts;
31using HeuristicLab.Hive.Contracts.Interfaces;
32
33namespace HeuristicLab.Hive.Server {
34  [ClassInfo(Name = "Hive Server",
35      Description = "Server application for the distributed hive engine.",
36      AutoRestart = true)]
37  public class HiveServerApplication : ApplicationBase {
38    public const string STR_ClientCommunicator = "ClientCommunicator";
39    public const string STR_ServerConsoleFacade = "ServerConsoleFacade";
40    public const string STR_ExecutionEngineFacade = "ExecutionEngineFacade";
41
42    private DiscoveryService discService = new DiscoveryService();
43    private Dictionary<string, ServiceHost> runningServices = new Dictionary<string, ServiceHost>();
44    private NetTcpBinding binding = (NetTcpBinding)WcfSettings.GetBinding();
45
46    private enum Services {
47      ClientCommunicator,
48      ServerConsoleFacade,
49      ExecutionEngineFacade,
50      All
51    }
52   
53    private bool AddMexEndpoint(ServiceHost serviceHost) {
54      if (serviceHost != null) {
55        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
56        serviceHost.Description.Behaviors.Add(behavior);
57
58        return serviceHost.AddServiceEndpoint(
59          typeof(IMetadataExchange),
60          MetadataExchangeBindings.CreateMexTcpBinding(),
61          "mex") != null;
62      } else
63        return false;
64    }
65
66    private Uri StartService(Services svc, IPAddress ipAddress, int port) {
67      string curServiceHost = "";
68      Uri uriTcp;
69      IClientFacade[] clientCommunicatorInstances = discService.GetInstances<IClientFacade>();
70      IServerConsoleFacade[] serverConsoleInstances = discService.GetInstances<IServerConsoleFacade>();
71      IExecutionEngineFacade[] executionEngineInstances = discService.GetInstances<IExecutionEngineFacade>();
72      ServiceHost serviceHost = null;
73      switch (svc) {
74        case Services.ClientCommunicator:
75          if (clientCommunicatorInstances.Length > 0) {
76            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/HiveServer/");
77            serviceHost = new ServiceHost(clientCommunicatorInstances[0].GetType(), uriTcp);
78            serviceHost.AddServiceEndpoint(typeof(IClientCommunicator), binding, STR_ClientCommunicator);
79            curServiceHost = STR_ClientCommunicator;
80          }
81          break;
82        case Services.ServerConsoleFacade:
83          if (serverConsoleInstances.Length > 0) {
84            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/HiveServerConsole/");
85            serviceHost = new ServiceHost(serverConsoleInstances[0].GetType(), uriTcp);
86            serviceHost.AddServiceEndpoint(typeof(IServerConsoleFacade), binding, STR_ServerConsoleFacade);
87            curServiceHost = STR_ServerConsoleFacade;
88          }
89          break;
90        case Services.ExecutionEngineFacade:
91          if (executionEngineInstances.Length > 0) {
92            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/ExecutionEngine/");
93            serviceHost = new ServiceHost(executionEngineInstances[0].GetType(), uriTcp);
94            serviceHost.AddServiceEndpoint(typeof(IExecutionEngineFacade), binding, STR_ExecutionEngineFacade);
95            curServiceHost = STR_ExecutionEngineFacade;
96          }
97          break;
98        case Services.All:
99          throw new InvalidOperationException("Not supported!");
100        default:
101          return null;
102      }
103      if ((serviceHost != null) && (!String.IsNullOrEmpty(curServiceHost))) {
104        AddMexEndpoint(serviceHost);
105        WcfSettings.SetServiceCertificate(serviceHost);
106        serviceHost.Open();
107        runningServices.Add(curServiceHost, serviceHost);
108        return serviceHost.BaseAddresses[0];
109      } else
110        return null;
111    }
112
113    private void StopService(Services svc) {
114      ServiceHost svcHost = null;
115      switch (svc) {
116        case Services.ClientCommunicator:
117          runningServices.TryGetValue(STR_ClientCommunicator, out svcHost);
118          break;
119        case Services.ServerConsoleFacade:
120          runningServices.TryGetValue(STR_ServerConsoleFacade, out svcHost);
121          break;
122        case Services.ExecutionEngineFacade:
123          runningServices.TryGetValue(STR_ExecutionEngineFacade, out svcHost);
124          break;
125        case Services.All:
126          foreach (KeyValuePair<string, ServiceHost> item in runningServices)
127            item.Value.Close();
128          return;
129        default:
130          throw new InvalidOperationException("Not supported!");
131      }
132      svcHost.Close();
133    }
134
135    public override void Run() {
136      IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
137      int index = 0;
138      if (System.Environment.OSVersion.Version.Major >= 6) {
139        for (index = addresses.Length - 1; index >= 0; index--)
140          if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
141            break;
142      }
143     
144      //Start services and record their base address
145      Dictionary<string, Uri> baseAddrDict = new Dictionary<string, Uri>();
146      baseAddrDict.Add(STR_ClientCommunicator,
147        StartService(Services.ClientCommunicator, addresses[index], WcfSettings.DEFAULTPORT));
148      baseAddrDict.Add(STR_ServerConsoleFacade,
149        StartService(Services.ServerConsoleFacade, addresses[index], WcfSettings.DEFAULTPORT));
150      baseAddrDict.Add(STR_ExecutionEngineFacade,
151        StartService(Services.ExecutionEngineFacade, addresses[index], WcfSettings.DEFAULTPORT));
152
153      ILifecycleManager[] lifecycleManagers =  discService.GetInstances<ILifecycleManager>();
154      if (lifecycleManagers.Length > 0) {
155        ILifecycleManager lifecycleManager =
156          lifecycleManagers[0];
157
158        lifecycleManager.Init();
159
160        Form mainForm = new MainForm(baseAddrDict);
161
162        Application.Run(mainForm);
163
164        lifecycleManager.Shutdown();
165      }
166      StopService(Services.All);
167    }
168  }
169}
Note: See TracBrowser for help on using the repository browser.