[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)]
|
---|
| 37 | class HiveServerApplication : ApplicationBase {
|
---|
[805] | 38 | const int port =
|
---|
[800] | 39 | 9000;
|
---|
[741] | 40 |
|
---|
[800] | 41 | DiscoveryService discService =
|
---|
| 42 | new DiscoveryService();
|
---|
[741] | 43 |
|
---|
[800] | 44 | private bool AddMexEndpoint(ServiceHost serviceHost) {
|
---|
[805] | 45 | if (serviceHost != null) {
|
---|
[800] | 46 | ServiceMetadataBehavior behavior =
|
---|
| 47 | new ServiceMetadataBehavior();
|
---|
[805] | 48 | serviceHost.Description.Behaviors.Add(behavior);
|
---|
[800] | 49 |
|
---|
[805] | 50 | return serviceHost.AddServiceEndpoint(
|
---|
| 51 | typeof(IMetadataExchange),
|
---|
| 52 | MetadataExchangeBindings.CreateMexTcpBinding(),
|
---|
| 53 | "mex") != null;
|
---|
[800] | 54 | } else
|
---|
| 55 | return false;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | private ServiceHost StartClientCommunicator(Uri uriTcp) {
|
---|
[1099] | 59 | IClientFacade[] clientCommunicatorInstances =
|
---|
| 60 | discService.GetInstances<IClientFacade>();
|
---|
[741] | 61 |
|
---|
[800] | 62 | if (clientCommunicatorInstances.Length > 0) {
|
---|
[741] | 63 | ServiceHost serviceHost =
|
---|
[800] | 64 | new ServiceHost(clientCommunicatorInstances[0].GetType(),
|
---|
[753] | 65 | uriTcp);
|
---|
[741] | 66 |
|
---|
[800] | 67 | System.ServiceModel.Channels.Binding binding =
|
---|
[753] | 68 | new NetTcpBinding();
|
---|
| 69 |
|
---|
| 70 | serviceHost.AddServiceEndpoint(
|
---|
| 71 | typeof(IClientCommunicator),
|
---|
| 72 | binding,
|
---|
| 73 | "ClientCommunicator");
|
---|
| 74 |
|
---|
[800] | 75 | AddMexEndpoint(serviceHost);
|
---|
[753] | 76 |
|
---|
[800] | 77 | serviceHost.Open();
|
---|
| 78 |
|
---|
| 79 | return serviceHost;
|
---|
| 80 | } else
|
---|
| 81 | return null;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | private ServiceHost StartServerConsoleFacade(Uri uriTcp) {
|
---|
| 85 | IServerConsoleFacade[] serverConsoleInstances =
|
---|
| 86 | discService.GetInstances<IServerConsoleFacade>();
|
---|
| 87 |
|
---|
| 88 | if (serverConsoleInstances.Length > 0) {
|
---|
| 89 | ServiceHost serviceHost =
|
---|
[925] | 90 | new ServiceHost(serverConsoleInstances[0].GetType(),
|
---|
[800] | 91 | uriTcp);
|
---|
| 92 |
|
---|
| 93 | System.ServiceModel.Channels.Binding binding =
|
---|
| 94 | new NetTcpBinding();
|
---|
| 95 |
|
---|
[753] | 96 | serviceHost.AddServiceEndpoint(
|
---|
[1084] | 97 | typeof(IServerConsoleFacade),
|
---|
| 98 | binding,
|
---|
| 99 | "ServerConsoleFacade");
|
---|
[753] | 100 |
|
---|
[800] | 101 | AddMexEndpoint(serviceHost);
|
---|
| 102 |
|
---|
[741] | 103 | serviceHost.Open();
|
---|
| 104 |
|
---|
[800] | 105 | return serviceHost;
|
---|
| 106 | } else
|
---|
| 107 | return null;
|
---|
| 108 | }
|
---|
[741] | 109 |
|
---|
[1120] | 110 | private ServiceHost StartExecutionEngineFacade(Uri uriTcp) {
|
---|
| 111 | IExecutionEngineFacade[] serverConsoleInstances =
|
---|
| 112 | discService.GetInstances<IExecutionEngineFacade>();
|
---|
| 113 |
|
---|
| 114 | if (serverConsoleInstances.Length > 0) {
|
---|
| 115 | ServiceHost serviceHost =
|
---|
| 116 | new ServiceHost(serverConsoleInstances[0].GetType(),
|
---|
| 117 | uriTcp);
|
---|
| 118 |
|
---|
| 119 | System.ServiceModel.Channels.Binding binding =
|
---|
| 120 | new NetTcpBinding();
|
---|
| 121 |
|
---|
| 122 | serviceHost.AddServiceEndpoint(
|
---|
| 123 | typeof(IExecutionEngineFacade),
|
---|
| 124 | binding,
|
---|
| 125 | "ExecutionEngineFacade");
|
---|
| 126 |
|
---|
| 127 | AddMexEndpoint(serviceHost);
|
---|
| 128 |
|
---|
| 129 | serviceHost.Open();
|
---|
| 130 |
|
---|
| 131 | return serviceHost;
|
---|
| 132 | } else
|
---|
| 133 | return null;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[800] | 136 | public override void Run() {
|
---|
[805] | 137 | IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
|
---|
| 138 | int index = 0;
|
---|
| 139 | if (System.Environment.OSVersion.Version.Major >= 6) {
|
---|
| 140 | for (index = addresses.Length - 1; index >= 0; index--)
|
---|
| 141 | if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
---|
| 142 | break;
|
---|
| 143 | }
|
---|
[800] | 144 |
|
---|
| 145 | Uri uriTcp =
|
---|
[994] | 146 | new Uri("net.tcp://" + addresses[index] + ":" + port + "/HiveServer/");
|
---|
[800] | 147 |
|
---|
[805] | 148 | ServiceHost clientCommunicator =
|
---|
[800] | 149 | StartClientCommunicator(uriTcp);
|
---|
| 150 |
|
---|
| 151 | uriTcp =
|
---|
[804] | 152 | new Uri("net.tcp://" + addresses[index] + ":" + port + "/HiveServerConsole/");
|
---|
[800] | 153 |
|
---|
| 154 | ServiceHost serverConsoleFacade =
|
---|
| 155 | StartServerConsoleFacade(uriTcp);
|
---|
| 156 |
|
---|
[1120] | 157 | uriTcp =
|
---|
| 158 | new Uri("net.tcp://" + addresses[index] + ":" + port + "/ExecutionEngine/");
|
---|
| 159 |
|
---|
| 160 | ServiceHost executionEngineFacade =
|
---|
| 161 | StartExecutionEngineFacade(uriTcp);
|
---|
| 162 |
|
---|
[925] | 163 | ILifecycleManager[] lifecycleManagers =
|
---|
| 164 | discService.GetInstances<ILifecycleManager>();
|
---|
[800] | 165 |
|
---|
[925] | 166 | if (lifecycleManagers.Length > 0) {
|
---|
| 167 | ILifecycleManager lifecycleManager =
|
---|
| 168 | lifecycleManagers[0];
|
---|
[800] | 169 |
|
---|
[925] | 170 | lifecycleManager.Init();
|
---|
[948] | 171 | //sync with db every 5 minutes
|
---|
| 172 | lifecycleManager.GetTransactionManager().EnableAutoUpdate(
|
---|
[1165] | 173 | new TimeSpan(0, 0, 30));
|
---|
[925] | 174 |
|
---|
| 175 | Form mainForm = new MainForm(clientCommunicator.BaseAddresses[0],
|
---|
[1120] | 176 | serverConsoleFacade.BaseAddresses[0],
|
---|
| 177 | executionEngineFacade.BaseAddresses[0]);
|
---|
[925] | 178 |
|
---|
| 179 | Application.Run(mainForm);
|
---|
| 180 |
|
---|
[948] | 181 | lifecycleManager.Shutdown();
|
---|
[925] | 182 | }
|
---|
| 183 |
|
---|
[800] | 184 | clientCommunicator.Close();
|
---|
| 185 | serverConsoleFacade.Close();
|
---|
[1120] | 186 | executionEngineFacade.Close();
|
---|
[713] | 187 | }
|
---|
| 188 | }
|
---|
| 189 | }
|
---|