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