[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;
|
---|
[2591] | 25 | using System.Linq;
|
---|
[713] | 26 | using System.Windows.Forms;
|
---|
| 27 | using HeuristicLab.PluginInfrastructure;
|
---|
[741] | 28 | using System.ServiceModel;
|
---|
[753] | 29 | using System.ServiceModel.Description;
|
---|
| 30 | using System.Net;
|
---|
[2904] | 31 | using HeuristicLab.Hive.Server.Core;
|
---|
[753] | 32 | using HeuristicLab.Hive.Contracts;
|
---|
[780] | 33 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
[2065] | 34 | using HeuristicLab.Hive.Server.Properties;
|
---|
[2904] | 35 | using Spring.ServiceModel;
|
---|
[713] | 36 |
|
---|
| 37 | namespace HeuristicLab.Hive.Server {
|
---|
[2591] | 38 | [Application("Hive Server", "Server application for the distributed hive engine.", true)]
|
---|
[1376] | 39 | public class HiveServerApplication : ApplicationBase {
|
---|
| 40 | public const string STR_ClientCommunicator = "ClientCommunicator";
|
---|
| 41 | public const string STR_ServerConsoleFacade = "ServerConsoleFacade";
|
---|
| 42 | public const string STR_ExecutionEngineFacade = "ExecutionEngineFacade";
|
---|
[741] | 43 |
|
---|
[1376] | 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 | }
|
---|
[2591] | 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;
|
---|
[2904] | 71 | /*IEnumerable<IClientFacade> clientCommunicatorInstances = ApplicationManager.Manager.GetInstances<IClientFacade>();
|
---|
[2591] | 72 | IEnumerable<IServerConsoleFacade> serverConsoleInstances = ApplicationManager.Manager.GetInstances<IServerConsoleFacade>();
|
---|
[2904] | 73 | IEnumerable<IExecutionEngineFacade> executionEngineInstances = ApplicationManager.Manager.GetInstances<IExecutionEngineFacade>();*/
|
---|
| 74 | SpringServiceHost serviceHost = null;
|
---|
[1376] | 75 | switch (svc) {
|
---|
| 76 | case Services.ClientCommunicator:
|
---|
[2904] | 77 | // if (clientCommunicatorInstances.Count() > 0) {
|
---|
[2591] | 78 | uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/HiveServer/");
|
---|
[2904] | 79 | //serviceHost = new ServiceHost(clientCommunicatorInstances.First().GetType(), uriTcp);
|
---|
| 80 | //serviceHost = new ServiceHost(typeof(ClientFacade), uriTcp);
|
---|
| 81 | serviceHost = new SpringServiceHost("clientFacade", uriTcp);
|
---|
[1939] | 82 | serviceHost.AddServiceEndpoint(typeof(IClientFacade), streamedBinding, STR_ClientCommunicator);
|
---|
[2609] | 83 |
|
---|
| 84 | ServiceDebugBehavior sdb = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
|
---|
| 85 | if (sdb == null) {
|
---|
| 86 | sdb = new ServiceDebugBehavior();
|
---|
| 87 | serviceHost.Description.Behaviors.Add(sdb);
|
---|
| 88 | }
|
---|
| 89 | sdb.IncludeExceptionDetailInFaults = true;
|
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 |
|
---|
[1376] | 93 | curServiceHost = STR_ClientCommunicator;
|
---|
[2904] | 94 | // }
|
---|
[1376] | 95 | break;
|
---|
| 96 | case Services.ServerConsoleFacade:
|
---|
[2904] | 97 | // if (serverConsoleInstances.Count() > 0) {
|
---|
[1376] | 98 | uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/HiveServerConsole/");
|
---|
[2904] | 99 | //serviceHost = new ServiceHost(serverConsoleInstances.First().GetType(), uriTcp);
|
---|
| 100 | serviceHost = new SpringServiceHost("serverConsoleFacade", uriTcp);
|
---|
[1376] | 101 | serviceHost.AddServiceEndpoint(typeof(IServerConsoleFacade), binding, STR_ServerConsoleFacade);
|
---|
| 102 | curServiceHost = STR_ServerConsoleFacade;
|
---|
[2904] | 103 | // }
|
---|
[1376] | 104 | break;
|
---|
| 105 | case Services.ExecutionEngineFacade:
|
---|
[2904] | 106 | // if (executionEngineInstances.Count() > 0) {
|
---|
[1376] | 107 | uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/ExecutionEngine/");
|
---|
[2904] | 108 | //serviceHost = new ServiceHost(executionEngineInstances.First().GetType(), uriTcp);
|
---|
| 109 | serviceHost = new SpringServiceHost("executionEngineFacade", uriTcp);
|
---|
[1939] | 110 | serviceHost.AddServiceEndpoint(typeof(IExecutionEngineFacade), streamedBinding, STR_ExecutionEngineFacade);
|
---|
[1376] | 111 | curServiceHost = STR_ExecutionEngineFacade;
|
---|
[2904] | 112 | // }
|
---|
[1376] | 113 | break;
|
---|
| 114 | case Services.All:
|
---|
| 115 | throw new InvalidOperationException("Not supported!");
|
---|
| 116 | default:
|
---|
| 117 | return null;
|
---|
| 118 | }
|
---|
[2904] | 119 | if (!String.IsNullOrEmpty(curServiceHost)) {
|
---|
[800] | 120 | AddMexEndpoint(serviceHost);
|
---|
[2609] | 121 | //WcfSettings.SetServiceCertificate(serviceHost);
|
---|
[800] | 122 | serviceHost.Open();
|
---|
[1376] | 123 | runningServices.Add(curServiceHost, serviceHost);
|
---|
| 124 | return serviceHost.BaseAddresses[0];
|
---|
[800] | 125 | } else
|
---|
| 126 | return null;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[1376] | 129 | private void StopService(Services svc) {
|
---|
| 130 | ServiceHost svcHost = null;
|
---|
| 131 | switch (svc) {
|
---|
| 132 | case Services.ClientCommunicator:
|
---|
| 133 | runningServices.TryGetValue(STR_ClientCommunicator, out svcHost);
|
---|
| 134 | break;
|
---|
| 135 | case Services.ServerConsoleFacade:
|
---|
| 136 | runningServices.TryGetValue(STR_ServerConsoleFacade, out svcHost);
|
---|
| 137 | break;
|
---|
| 138 | case Services.ExecutionEngineFacade:
|
---|
| 139 | runningServices.TryGetValue(STR_ExecutionEngineFacade, out svcHost);
|
---|
| 140 | break;
|
---|
| 141 | case Services.All:
|
---|
| 142 | foreach (KeyValuePair<string, ServiceHost> item in runningServices)
|
---|
| 143 | item.Value.Close();
|
---|
| 144 | return;
|
---|
| 145 | default:
|
---|
| 146 | throw new InvalidOperationException("Not supported!");
|
---|
| 147 | }
|
---|
| 148 | svcHost.Close();
|
---|
[800] | 149 | }
|
---|
[741] | 150 |
|
---|
[800] | 151 | public override void Run() {
|
---|
[805] | 152 | IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
|
---|
| 153 | int index = 0;
|
---|
| 154 | if (System.Environment.OSVersion.Version.Major >= 6) {
|
---|
| 155 | for (index = addresses.Length - 1; index >= 0; index--)
|
---|
| 156 | if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
---|
| 157 | break;
|
---|
| 158 | }
|
---|
[2591] | 159 |
|
---|
[1376] | 160 | //Start services and record their base address
|
---|
| 161 | Dictionary<string, Uri> baseAddrDict = new Dictionary<string, Uri>();
|
---|
| 162 | baseAddrDict.Add(STR_ClientCommunicator,
|
---|
[1579] | 163 | StartService(Services.ClientCommunicator, addresses[index], WcfSettings.DEFAULTPORT));
|
---|
[1376] | 164 | baseAddrDict.Add(STR_ServerConsoleFacade,
|
---|
[1579] | 165 | StartService(Services.ServerConsoleFacade, addresses[index], WcfSettings.DEFAULTPORT));
|
---|
[1376] | 166 | baseAddrDict.Add(STR_ExecutionEngineFacade,
|
---|
[1579] | 167 | StartService(Services.ExecutionEngineFacade, addresses[index], WcfSettings.DEFAULTPORT));
|
---|
[800] | 168 |
|
---|
[2591] | 169 | IEnumerable<ILifecycleManager> lifecycleManagers = ApplicationManager.Manager.GetInstances<ILifecycleManager>();
|
---|
| 170 | if (lifecycleManagers.Count() > 0) {
|
---|
[1376] | 171 | ILifecycleManager lifecycleManager =
|
---|
[2591] | 172 | lifecycleManagers.First();
|
---|
[800] | 173 |
|
---|
[925] | 174 | lifecycleManager.Init();
|
---|
[1376] | 175 | Form mainForm = new MainForm(baseAddrDict);
|
---|
| 176 | Application.Run(mainForm);
|
---|
[2591] | 177 |
|
---|
[1376] | 178 | lifecycleManager.Shutdown();
|
---|
[925] | 179 | }
|
---|
[1376] | 180 | StopService(Services.All);
|
---|
[713] | 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|