Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2021 was 1939, checked in by svonolfe, 15 years ago

Large amounts of data are now transferred streamed (fixed ticket #660)

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