Free cookie consent management tool by TermsFeed Policy Generator

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

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

Rename "Client" to "Slave" #1157

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