Free cookie consent management tool by TermsFeed Policy Generator

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

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

#528: Refactoring and Integration of WCF Security

File size: 7.2 KB
RevLine 
[713]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;
[741]27using System.ServiceModel;
[753]28using System.ServiceModel.Description;
29using System.Net;
30using HeuristicLab.Hive.Contracts;
[780]31using HeuristicLab.Hive.Contracts.Interfaces;
[713]32
33namespace HeuristicLab.Hive.Server {
34  [ClassInfo(Name = "Hive Server",
35      Description = "Server application for the distributed hive engine.",
36      AutoRestart = true)]
[1376]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";
[741]42
[1376]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);
[741]47
[1376]48    private enum Services {
49      ClientCommunicator,
50      ServerConsoleFacade,
51      ExecutionEngineFacade,
52      All
53    }
54   
[800]55    private bool AddMexEndpoint(ServiceHost serviceHost) {
[805]56      if (serviceHost != null) {
[1376]57        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
[805]58        serviceHost.Description.Behaviors.Add(behavior);
[800]59
[805]60        return serviceHost.AddServiceEndpoint(
61          typeof(IMetadataExchange),
62          MetadataExchangeBindings.CreateMexTcpBinding(),
63          "mex") != null;
[800]64      } else
65        return false;
66    }
67
[1376]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))) {
[800]106        AddMexEndpoint(serviceHost);
107        serviceHost.Open();
[1376]108        runningServices.Add(curServiceHost, serviceHost);
109        return serviceHost.BaseAddresses[0];
[800]110      } else
111        return null;
112    }
113
[1376]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();
[800]134    }
[741]135
[800]136    public override void Run() {
[805]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      }
[1376]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));
[800]153
[1376]154      ILifecycleManager[] lifecycleManagers =  discService.GetInstances<ILifecycleManager>();
[925]155      if (lifecycleManagers.Length > 0) {
[1376]156        ILifecycleManager lifecycleManager =
[925]157          lifecycleManagers[0];
[800]158
[925]159        lifecycleManager.Init();
[948]160        //sync with db every 5 minutes
161        lifecycleManager.GetTransactionManager().EnableAutoUpdate(
[1165]162          new TimeSpan(0, 0, 30));
[925]163
[1376]164        Form mainForm = new MainForm(baseAddrDict);
[925]165
[1376]166        Application.Run(mainForm);
[925]167
[1376]168        lifecycleManager.Shutdown();
[925]169      }
[1376]170      StopService(Services.All);
[713]171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.