Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server/3.2/HiveServerApplication.cs @ 2591

Last change on this file since 2591 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

File size: 7.1 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.Text;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.PluginInfrastructure;
28using System.ServiceModel;
29using System.ServiceModel.Description;
30using System.Net;
31using HeuristicLab.Hive.Contracts;
32using HeuristicLab.Hive.Contracts.Interfaces;
33using HeuristicLab.Hive.Server.Properties;
34
35namespace HeuristicLab.Hive.Server {
36  [Application("Hive Server", "Server application for the distributed hive engine.", true)]
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";
41
42    private Dictionary<string, ServiceHost> runningServices = new Dictionary<string, ServiceHost>();
43    private NetTcpBinding binding = (NetTcpBinding)WcfSettings.GetBinding();
44    private NetTcpBinding streamedBinding = (NetTcpBinding)WcfSettings.GetStreamedBinding();
45
46    private enum Services {
47      ClientCommunicator,
48      ServerConsoleFacade,
49      ExecutionEngineFacade,
50      All
51    }
52
53    private bool AddMexEndpoint(ServiceHost serviceHost) {
54      if (serviceHost != null) {
55        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
56        serviceHost.Description.Behaviors.Add(behavior);
57
58        return serviceHost.AddServiceEndpoint(
59          typeof(IMetadataExchange),
60          MetadataExchangeBindings.CreateMexTcpBinding(),
61          "mex") != null;
62      } else
63        return false;
64    }
65
66    private Uri StartService(Services svc, IPAddress ipAddress, int port) {
67      string curServiceHost = "";
68      Uri uriTcp;
69      IEnumerable<IClientFacade> clientCommunicatorInstances = ApplicationManager.Manager.GetInstances<IClientFacade>();
70      IEnumerable<IServerConsoleFacade> serverConsoleInstances = ApplicationManager.Manager.GetInstances<IServerConsoleFacade>();
71      IEnumerable<IExecutionEngineFacade> executionEngineInstances = ApplicationManager.Manager.GetInstances<IExecutionEngineFacade>();
72      ServiceHost serviceHost = null;
73      switch (svc) {
74        case Services.ClientCommunicator:
75          if (clientCommunicatorInstances.Count() > 0) {
76            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/HiveServer/");
77            serviceHost = new ServiceHost(clientCommunicatorInstances.First().GetType(), uriTcp);
78            serviceHost.AddServiceEndpoint(typeof(IClientFacade), streamedBinding, STR_ClientCommunicator);
79            curServiceHost = STR_ClientCommunicator;
80          }
81          break;
82        case Services.ServerConsoleFacade:
83          if (serverConsoleInstances.Count() > 0) {
84            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/HiveServerConsole/");
85            serviceHost = new ServiceHost(serverConsoleInstances.First().GetType(), uriTcp);
86            serviceHost.AddServiceEndpoint(typeof(IServerConsoleFacade), binding, STR_ServerConsoleFacade);
87            curServiceHost = STR_ServerConsoleFacade;
88          }
89          break;
90        case Services.ExecutionEngineFacade:
91          if (executionEngineInstances.Count() > 0) {
92            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/ExecutionEngine/");
93            serviceHost = new ServiceHost(executionEngineInstances.First().GetType(), uriTcp);
94            serviceHost.AddServiceEndpoint(typeof(IExecutionEngineFacade), streamedBinding, 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))) {
104        AddMexEndpoint(serviceHost);
105        WcfSettings.SetServiceCertificate(serviceHost);
106        serviceHost.Open();
107        runningServices.Add(curServiceHost, serviceHost);
108        return serviceHost.BaseAddresses[0];
109      } else
110        return null;
111    }
112
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();
133    }
134
135    public override void Run() {
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      }
143
144      //Start services and record their base address
145      Dictionary<string, Uri> baseAddrDict = new Dictionary<string, Uri>();
146      baseAddrDict.Add(STR_ClientCommunicator,
147        StartService(Services.ClientCommunicator, addresses[index], WcfSettings.DEFAULTPORT));
148      baseAddrDict.Add(STR_ServerConsoleFacade,
149        StartService(Services.ServerConsoleFacade, addresses[index], WcfSettings.DEFAULTPORT));
150      baseAddrDict.Add(STR_ExecutionEngineFacade,
151        StartService(Services.ExecutionEngineFacade, addresses[index], WcfSettings.DEFAULTPORT));
152
153      IEnumerable<ILifecycleManager> lifecycleManagers = ApplicationManager.Manager.GetInstances<ILifecycleManager>();
154      if (lifecycleManagers.Count() > 0) {
155        ILifecycleManager lifecycleManager =
156          lifecycleManagers.First();
157
158        lifecycleManager.Init();
159        Form mainForm = new MainForm(baseAddrDict);
160        Application.Run(mainForm);
161
162        lifecycleManager.Shutdown();
163      }
164      StopService(Services.All);
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.