Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone3/sources/HeuristicLab.Hive.Server/3.2/HiveServerApplication.cs @ 5664

Last change on this file since 5664 was 2065, checked in by mbecirov, 15 years ago

#586: Added authorization components.

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