[713] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.PluginInfrastructure;
|
---|
[741] | 27 | using System.ServiceModel;
|
---|
[753] | 28 | using System.ServiceModel.Description;
|
---|
| 29 | using System.Net;
|
---|
| 30 | using HeuristicLab.Hive.Contracts;
|
---|
[780] | 31 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
[2065] | 32 | using HeuristicLab.Hive.Server.Properties;
|
---|
[713] | 33 |
|
---|
| 34 | namespace HeuristicLab.Hive.Server {
|
---|
| 35 | [ClassInfo(Name = "Hive Server",
|
---|
| 36 | Description = "Server application for the distributed hive engine.",
|
---|
| 37 | AutoRestart = true)]
|
---|
[1376] | 38 | public class HiveServerApplication : ApplicationBase {
|
---|
| 39 | public const string STR_ClientCommunicator = "ClientCommunicator";
|
---|
| 40 | public const string STR_ServerConsoleFacade = "ServerConsoleFacade";
|
---|
| 41 | public const string STR_ExecutionEngineFacade = "ExecutionEngineFacade";
|
---|
[741] | 42 |
|
---|
[1376] | 43 | private DiscoveryService discService = new DiscoveryService();
|
---|
| 44 | private Dictionary<string, ServiceHost> runningServices = new Dictionary<string, ServiceHost>();
|
---|
[1579] | 45 | private NetTcpBinding binding = (NetTcpBinding)WcfSettings.GetBinding();
|
---|
[1939] | 46 | private NetTcpBinding streamedBinding = (NetTcpBinding)WcfSettings.GetStreamedBinding();
|
---|
[741] | 47 |
|
---|
[1376] | 48 | private enum Services {
|
---|
| 49 | ClientCommunicator,
|
---|
| 50 | ServerConsoleFacade,
|
---|
| 51 | ExecutionEngineFacade,
|
---|
| 52 | All
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[800] | 55 | private bool AddMexEndpoint(ServiceHost serviceHost) {
|
---|
[805] | 56 | if (serviceHost != null) {
|
---|
[1376] | 57 | ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
|
---|
[805] | 58 | serviceHost.Description.Behaviors.Add(behavior);
|
---|
[800] | 59 |
|
---|
[805] | 60 | return serviceHost.AddServiceEndpoint(
|
---|
| 61 | typeof(IMetadataExchange),
|
---|
| 62 | MetadataExchangeBindings.CreateMexTcpBinding(),
|
---|
| 63 | "mex") != null;
|
---|
[800] | 64 | } else
|
---|
| 65 | return false;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[1376] | 68 | private Uri StartService(Services svc, IPAddress ipAddress, int port) {
|
---|
| 69 | string curServiceHost = "";
|
---|
| 70 | Uri uriTcp;
|
---|
| 71 | IClientFacade[] clientCommunicatorInstances = discService.GetInstances<IClientFacade>();
|
---|
| 72 | IServerConsoleFacade[] serverConsoleInstances = discService.GetInstances<IServerConsoleFacade>();
|
---|
| 73 | IExecutionEngineFacade[] executionEngineInstances = discService.GetInstances<IExecutionEngineFacade>();
|
---|
| 74 | ServiceHost serviceHost = null;
|
---|
| 75 | switch (svc) {
|
---|
| 76 | case Services.ClientCommunicator:
|
---|
| 77 | if (clientCommunicatorInstances.Length > 0) {
|
---|
| 78 | uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/HiveServer/");
|
---|
| 79 | serviceHost = new ServiceHost(clientCommunicatorInstances[0].GetType(), uriTcp);
|
---|
[1939] | 80 | serviceHost.AddServiceEndpoint(typeof(IClientFacade), streamedBinding, STR_ClientCommunicator);
|
---|
[1376] | 81 | curServiceHost = STR_ClientCommunicator;
|
---|
| 82 | }
|
---|
| 83 | break;
|
---|
| 84 | case Services.ServerConsoleFacade:
|
---|
| 85 | if (serverConsoleInstances.Length > 0) {
|
---|
| 86 | uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/HiveServerConsole/");
|
---|
| 87 | serviceHost = new ServiceHost(serverConsoleInstances[0].GetType(), uriTcp);
|
---|
| 88 | serviceHost.AddServiceEndpoint(typeof(IServerConsoleFacade), binding, STR_ServerConsoleFacade);
|
---|
| 89 | curServiceHost = STR_ServerConsoleFacade;
|
---|
| 90 | }
|
---|
| 91 | break;
|
---|
| 92 | case Services.ExecutionEngineFacade:
|
---|
| 93 | if (executionEngineInstances.Length > 0) {
|
---|
| 94 | uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/ExecutionEngine/");
|
---|
| 95 | serviceHost = new ServiceHost(executionEngineInstances[0].GetType(), uriTcp);
|
---|
[1939] | 96 | serviceHost.AddServiceEndpoint(typeof(IExecutionEngineFacade), streamedBinding, STR_ExecutionEngineFacade);
|
---|
[1376] | 97 | curServiceHost = STR_ExecutionEngineFacade;
|
---|
| 98 | }
|
---|
| 99 | break;
|
---|
| 100 | case Services.All:
|
---|
| 101 | throw new InvalidOperationException("Not supported!");
|
---|
| 102 | default:
|
---|
| 103 | return null;
|
---|
| 104 | }
|
---|
| 105 | if ((serviceHost != null) && (!String.IsNullOrEmpty(curServiceHost))) {
|
---|
[800] | 106 | AddMexEndpoint(serviceHost);
|
---|
[1579] | 107 | WcfSettings.SetServiceCertificate(serviceHost);
|
---|
[800] | 108 | serviceHost.Open();
|
---|
[1376] | 109 | runningServices.Add(curServiceHost, serviceHost);
|
---|
| 110 | return serviceHost.BaseAddresses[0];
|
---|
[800] | 111 | } else
|
---|
| 112 | return null;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[1376] | 115 | private void StopService(Services svc) {
|
---|
| 116 | ServiceHost svcHost = null;
|
---|
| 117 | switch (svc) {
|
---|
| 118 | case Services.ClientCommunicator:
|
---|
| 119 | runningServices.TryGetValue(STR_ClientCommunicator, out svcHost);
|
---|
| 120 | break;
|
---|
| 121 | case Services.ServerConsoleFacade:
|
---|
| 122 | runningServices.TryGetValue(STR_ServerConsoleFacade, out svcHost);
|
---|
| 123 | break;
|
---|
| 124 | case Services.ExecutionEngineFacade:
|
---|
| 125 | runningServices.TryGetValue(STR_ExecutionEngineFacade, out svcHost);
|
---|
| 126 | break;
|
---|
| 127 | case Services.All:
|
---|
| 128 | foreach (KeyValuePair<string, ServiceHost> item in runningServices)
|
---|
| 129 | item.Value.Close();
|
---|
| 130 | return;
|
---|
| 131 | default:
|
---|
| 132 | throw new InvalidOperationException("Not supported!");
|
---|
| 133 | }
|
---|
| 134 | svcHost.Close();
|
---|
[800] | 135 | }
|
---|
[741] | 136 |
|
---|
[800] | 137 | public override void Run() {
|
---|
[805] | 138 | IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
|
---|
| 139 | int index = 0;
|
---|
| 140 | if (System.Environment.OSVersion.Version.Major >= 6) {
|
---|
| 141 | for (index = addresses.Length - 1; index >= 0; index--)
|
---|
| 142 | if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
---|
| 143 | break;
|
---|
| 144 | }
|
---|
[1376] | 145 |
|
---|
| 146 | //Start services and record their base address
|
---|
| 147 | Dictionary<string, Uri> baseAddrDict = new Dictionary<string, Uri>();
|
---|
| 148 | baseAddrDict.Add(STR_ClientCommunicator,
|
---|
[1579] | 149 | StartService(Services.ClientCommunicator, addresses[index], WcfSettings.DEFAULTPORT));
|
---|
[1376] | 150 | baseAddrDict.Add(STR_ServerConsoleFacade,
|
---|
[1579] | 151 | StartService(Services.ServerConsoleFacade, addresses[index], WcfSettings.DEFAULTPORT));
|
---|
[1376] | 152 | baseAddrDict.Add(STR_ExecutionEngineFacade,
|
---|
[1579] | 153 | StartService(Services.ExecutionEngineFacade, addresses[index], WcfSettings.DEFAULTPORT));
|
---|
[800] | 154 |
|
---|
[1376] | 155 | ILifecycleManager[] lifecycleManagers = discService.GetInstances<ILifecycleManager>();
|
---|
[925] | 156 | if (lifecycleManagers.Length > 0) {
|
---|
[1376] | 157 | ILifecycleManager lifecycleManager =
|
---|
[925] | 158 | lifecycleManagers[0];
|
---|
[800] | 159 |
|
---|
[925] | 160 | lifecycleManager.Init();
|
---|
[1376] | 161 | Form mainForm = new MainForm(baseAddrDict);
|
---|
| 162 | Application.Run(mainForm);
|
---|
[2065] | 163 |
|
---|
[1376] | 164 | lifecycleManager.Shutdown();
|
---|
[925] | 165 | }
|
---|
[1376] | 166 | StopService(Services.All);
|
---|
[713] | 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|