Free cookie consent management tool by TermsFeed Policy Generator

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

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

Improved caching mechanism, fixed small bug related to the job data table (#372)

File size: 5.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.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  class HiveServerApplication : ApplicationBase {
38    const int port =
39      9000;
40
41    DiscoveryService discService =
42        new DiscoveryService();
43
44    private bool AddMexEndpoint(ServiceHost serviceHost) {
45      if (serviceHost != null) {
46        ServiceMetadataBehavior behavior =
47            new ServiceMetadataBehavior();
48        serviceHost.Description.Behaviors.Add(behavior);
49
50        return serviceHost.AddServiceEndpoint(
51          typeof(IMetadataExchange),
52          MetadataExchangeBindings.CreateMexTcpBinding(),
53          "mex") != null;
54      } else
55        return false;
56    }
57
58    private ServiceHost StartClientCommunicator(Uri uriTcp) {
59      IClientFacade[] clientCommunicatorInstances =
60        discService.GetInstances<IClientFacade>();
61
62      if (clientCommunicatorInstances.Length > 0) {
63        ServiceHost serviceHost =
64                new ServiceHost(clientCommunicatorInstances[0].GetType(),
65                  uriTcp);
66
67        System.ServiceModel.Channels.Binding binding =
68          new NetTcpBinding();
69
70        serviceHost.AddServiceEndpoint(
71          typeof(IClientCommunicator),
72              binding,
73              "ClientCommunicator");
74
75        AddMexEndpoint(serviceHost);
76
77        serviceHost.Open();
78
79        return serviceHost;
80      } else
81        return null;
82    }
83
84    private ServiceHost StartServerConsoleFacade(Uri uriTcp) {
85      IServerConsoleFacade[] serverConsoleInstances =
86        discService.GetInstances<IServerConsoleFacade>();
87
88      if (serverConsoleInstances.Length > 0) {
89        ServiceHost serviceHost =
90            new ServiceHost(serverConsoleInstances[0].GetType(),
91                  uriTcp);
92
93        System.ServiceModel.Channels.Binding binding =
94          new NetTcpBinding();
95
96        serviceHost.AddServiceEndpoint(
97          typeof(IServerConsoleFacade),
98            binding,
99            "ServerConsoleFacade");
100
101        AddMexEndpoint(serviceHost);
102
103        serviceHost.Open();
104
105        return serviceHost;
106      } else
107        return null;
108    }
109
110    private ServiceHost StartExecutionEngineFacade(Uri uriTcp) {
111      IExecutionEngineFacade[] serverConsoleInstances =
112        discService.GetInstances<IExecutionEngineFacade>();
113
114      if (serverConsoleInstances.Length > 0) {
115        ServiceHost serviceHost =
116            new ServiceHost(serverConsoleInstances[0].GetType(),
117                  uriTcp);
118
119        System.ServiceModel.Channels.Binding binding =
120          new NetTcpBinding();
121
122        serviceHost.AddServiceEndpoint(
123          typeof(IExecutionEngineFacade),
124            binding,
125            "ExecutionEngineFacade");
126
127        AddMexEndpoint(serviceHost);
128
129        serviceHost.Open();
130
131        return serviceHost;
132      } else
133        return null;
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      Uri uriTcp =
146          new Uri("net.tcp://" + addresses[index] + ":" + port + "/HiveServer/");
147
148      ServiceHost clientCommunicator =
149        StartClientCommunicator(uriTcp);
150
151      uriTcp =
152        new Uri("net.tcp://" + addresses[index] + ":" + port + "/HiveServerConsole/");
153
154      ServiceHost serverConsoleFacade =
155        StartServerConsoleFacade(uriTcp);
156
157      uriTcp =
158        new Uri("net.tcp://" + addresses[index] + ":" + port + "/ExecutionEngine/");
159
160      ServiceHost executionEngineFacade =
161        StartExecutionEngineFacade(uriTcp);
162
163      ILifecycleManager[] lifecycleManagers =
164         discService.GetInstances<ILifecycleManager>();
165
166      if (lifecycleManagers.Length > 0) {
167        ILifecycleManager lifecycleManager =
168          lifecycleManagers[0];
169
170        lifecycleManager.Init();
171        //sync with db every 5 minutes
172        lifecycleManager.GetTransactionManager().EnableAutoUpdate(
173          new TimeSpan(0, 0, 30));
174
175        Form mainForm = new MainForm(clientCommunicator.BaseAddresses[0],
176            serverConsoleFacade.BaseAddresses[0],
177            executionEngineFacade.BaseAddresses[0]);
178
179         Application.Run(mainForm);
180
181         lifecycleManager.Shutdown();
182      }
183
184      clientCommunicator.Close();
185      serverConsoleFacade.Close();
186      executionEngineFacade.Close();     
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.