Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1468 was 1468, checked in by svonolfe, 16 years ago

Added transaction management (#527)

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    private const int DEFAULT_PORT = 9000;
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 = new NetTcpBinding(SecurityMode.None, true);
46    //private WSHttpBinding binding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential, true);
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(IClientCommunicator), binding, 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), binding, 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        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], DEFAULT_PORT));
149      baseAddrDict.Add(STR_ServerConsoleFacade,
150        StartService(Services.ServerConsoleFacade, addresses[index], DEFAULT_PORT));
151      baseAddrDict.Add(STR_ExecutionEngineFacade,
152        StartService(Services.ExecutionEngineFacade, addresses[index], DEFAULT_PORT));
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.