Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server/3.3/HeuristicLabHiveServerApplication.cs @ 4254

Last change on this file since 4254 was 4254, checked in by cneumuel, 14 years ago

some small refactorings (#1159)

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