Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2668 was 2609, checked in by kgrading, 15 years ago

added logging to the wcf behavior (#828)

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