[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;
|
---|
[713] | 32 |
|
---|
| 33 | namespace HeuristicLab.Hive.Server {
|
---|
| 34 | [ClassInfo(Name = "Hive Server",
|
---|
| 35 | Description = "Server application for the distributed hive engine.",
|
---|
| 36 | AutoRestart = 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 DiscoveryService discService = new DiscoveryService();
|
---|
| 43 | private Dictionary<string, ServiceHost> runningServices = new Dictionary<string, ServiceHost>();
|
---|
[1579] | 44 | private NetTcpBinding binding = (NetTcpBinding)WcfSettings.GetBinding();
|
---|
[741] | 45 |
|
---|
[1376] | 46 | private enum Services {
|
---|
| 47 | ClientCommunicator,
|
---|
| 48 | ServerConsoleFacade,
|
---|
| 49 | ExecutionEngineFacade,
|
---|
| 50 | All
|
---|
| 51 | }
|
---|
| 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;
|
---|
| 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))) {
|
---|
[800] | 104 | AddMexEndpoint(serviceHost);
|
---|
[1579] | 105 | WcfSettings.SetServiceCertificate(serviceHost);
|
---|
[800] | 106 | serviceHost.Open();
|
---|
[1376] | 107 | runningServices.Add(curServiceHost, serviceHost);
|
---|
| 108 | return serviceHost.BaseAddresses[0];
|
---|
[800] | 109 | } else
|
---|
| 110 | return null;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[1376] | 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();
|
---|
[800] | 133 | }
|
---|
[741] | 134 |
|
---|
[800] | 135 | public override void Run() {
|
---|
[805] | 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 | }
|
---|
[1376] | 143 |
|
---|
| 144 | //Start services and record their base address
|
---|
| 145 | Dictionary<string, Uri> baseAddrDict = new Dictionary<string, Uri>();
|
---|
| 146 | baseAddrDict.Add(STR_ClientCommunicator,
|
---|
[1579] | 147 | StartService(Services.ClientCommunicator, addresses[index], WcfSettings.DEFAULTPORT));
|
---|
[1376] | 148 | baseAddrDict.Add(STR_ServerConsoleFacade,
|
---|
[1579] | 149 | StartService(Services.ServerConsoleFacade, addresses[index], WcfSettings.DEFAULTPORT));
|
---|
[1376] | 150 | baseAddrDict.Add(STR_ExecutionEngineFacade,
|
---|
[1579] | 151 | StartService(Services.ExecutionEngineFacade, addresses[index], WcfSettings.DEFAULTPORT));
|
---|
[800] | 152 |
|
---|
[1376] | 153 | ILifecycleManager[] lifecycleManagers = discService.GetInstances<ILifecycleManager>();
|
---|
[925] | 154 | if (lifecycleManagers.Length > 0) {
|
---|
[1376] | 155 | ILifecycleManager lifecycleManager =
|
---|
[925] | 156 | lifecycleManagers[0];
|
---|
[800] | 157 |
|
---|
[925] | 158 | lifecycleManager.Init();
|
---|
| 159 |
|
---|
[1376] | 160 | Form mainForm = new MainForm(baseAddrDict);
|
---|
[925] | 161 |
|
---|
[1376] | 162 | Application.Run(mainForm);
|
---|
[925] | 163 |
|
---|
[1376] | 164 | lifecycleManager.Shutdown();
|
---|
[925] | 165 | }
|
---|
[1376] | 166 | StopService(Services.All);
|
---|
[713] | 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|