Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4539 was 2904, checked in by kgrading, 14 years ago

added functionality (#830)

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.Text;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.PluginInfrastructure;
28using System.ServiceModel;
29using System.ServiceModel.Description;
30using System.Net;
31using HeuristicLab.Hive.Server.Core;
32using HeuristicLab.Hive.Contracts;
33using HeuristicLab.Hive.Contracts.Interfaces;
34using HeuristicLab.Hive.Server.Properties;
35using Spring.ServiceModel;
36
37namespace HeuristicLab.Hive.Server {
38  [Application("Hive Server", "Server application for the distributed hive engine.", true)]
39  public class HiveServerApplication : ApplicationBase {
40    public const string STR_ClientCommunicator = "ClientCommunicator";
41    public const string STR_ServerConsoleFacade = "ServerConsoleFacade";
42    public const string STR_ExecutionEngineFacade = "ExecutionEngineFacade";
43
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      /*IEnumerable<IClientFacade> clientCommunicatorInstances = ApplicationManager.Manager.GetInstances<IClientFacade>();
72      IEnumerable<IServerConsoleFacade> serverConsoleInstances = ApplicationManager.Manager.GetInstances<IServerConsoleFacade>();
73      IEnumerable<IExecutionEngineFacade> executionEngineInstances = ApplicationManager.Manager.GetInstances<IExecutionEngineFacade>();*/
74      SpringServiceHost serviceHost = null;
75      switch (svc) {
76        case Services.ClientCommunicator:
77        //  if (clientCommunicatorInstances.Count() > 0) {
78            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/HiveServer/");
79            //serviceHost = new ServiceHost(clientCommunicatorInstances.First().GetType(), uriTcp);
80            //serviceHost = new ServiceHost(typeof(ClientFacade), uriTcp);
81            serviceHost = new SpringServiceHost("clientFacade", uriTcp);
82            serviceHost.AddServiceEndpoint(typeof(IClientFacade), streamedBinding, STR_ClientCommunicator);
83           
84            ServiceDebugBehavior sdb = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
85            if (sdb == null) {
86              sdb = new ServiceDebugBehavior();
87              serviceHost.Description.Behaviors.Add(sdb);
88            }
89            sdb.IncludeExceptionDetailInFaults = true;
90
91           
92
93            curServiceHost = STR_ClientCommunicator;
94        //  }
95          break;
96        case Services.ServerConsoleFacade:
97        //  if (serverConsoleInstances.Count() > 0) {
98            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/HiveServerConsole/");
99            //serviceHost = new ServiceHost(serverConsoleInstances.First().GetType(), uriTcp);
100            serviceHost = new SpringServiceHost("serverConsoleFacade", uriTcp);
101            serviceHost.AddServiceEndpoint(typeof(IServerConsoleFacade), binding, STR_ServerConsoleFacade);
102            curServiceHost = STR_ServerConsoleFacade;
103        //  }
104          break;
105        case Services.ExecutionEngineFacade:
106        //  if (executionEngineInstances.Count() > 0) {
107            uriTcp = new Uri("net.tcp://" + ipAddress + ":" + port + "/ExecutionEngine/");
108            //serviceHost = new ServiceHost(executionEngineInstances.First().GetType(), uriTcp);
109            serviceHost = new SpringServiceHost("executionEngineFacade", uriTcp);
110            serviceHost.AddServiceEndpoint(typeof(IExecutionEngineFacade), streamedBinding, STR_ExecutionEngineFacade);
111            curServiceHost = STR_ExecutionEngineFacade;
112        //  }
113          break;
114        case Services.All:
115          throw new InvalidOperationException("Not supported!");
116        default:
117          return null;
118      }
119      if (!String.IsNullOrEmpty(curServiceHost)) {
120        AddMexEndpoint(serviceHost);
121        //WcfSettings.SetServiceCertificate(serviceHost);
122        serviceHost.Open();
123        runningServices.Add(curServiceHost, serviceHost);
124        return serviceHost.BaseAddresses[0];
125      } else
126        return null;
127    }
128
129    private void StopService(Services svc) {
130      ServiceHost svcHost = null;
131      switch (svc) {
132        case Services.ClientCommunicator:
133          runningServices.TryGetValue(STR_ClientCommunicator, out svcHost);
134          break;
135        case Services.ServerConsoleFacade:
136          runningServices.TryGetValue(STR_ServerConsoleFacade, out svcHost);
137          break;
138        case Services.ExecutionEngineFacade:
139          runningServices.TryGetValue(STR_ExecutionEngineFacade, out svcHost);
140          break;
141        case Services.All:
142          foreach (KeyValuePair<string, ServiceHost> item in runningServices)
143            item.Value.Close();
144          return;
145        default:
146          throw new InvalidOperationException("Not supported!");
147      }
148      svcHost.Close();
149    }
150
151    public override void Run() {
152      IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
153      int index = 0;
154      if (System.Environment.OSVersion.Version.Major >= 6) {
155        for (index = addresses.Length - 1; index >= 0; index--)
156          if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
157            break;
158      }
159
160      //Start services and record their base address
161      Dictionary<string, Uri> baseAddrDict = new Dictionary<string, Uri>();
162      baseAddrDict.Add(STR_ClientCommunicator,
163        StartService(Services.ClientCommunicator, addresses[index], WcfSettings.DEFAULTPORT));
164      baseAddrDict.Add(STR_ServerConsoleFacade,
165        StartService(Services.ServerConsoleFacade, addresses[index], WcfSettings.DEFAULTPORT));
166      baseAddrDict.Add(STR_ExecutionEngineFacade,
167        StartService(Services.ExecutionEngineFacade, addresses[index], WcfSettings.DEFAULTPORT));
168
169      IEnumerable<ILifecycleManager> lifecycleManagers = ApplicationManager.Manager.GetInstances<ILifecycleManager>();
170      if (lifecycleManagers.Count() > 0) {
171        ILifecycleManager lifecycleManager =
172          lifecycleManagers.First();
173
174        lifecycleManager.Init();
175        Form mainForm = new MainForm(baseAddrDict);
176        Application.Run(mainForm);
177
178        lifecycleManager.Shutdown();
179      }
180      StopService(Services.All);
181    }
182  }
183}
Note: See TracBrowser for help on using the repository browser.