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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.PluginInfrastructure;
|
---|
27 | using System.ServiceModel;
|
---|
28 | using System.ServiceModel.Description;
|
---|
29 | using System.Net;
|
---|
30 | using HeuristicLab.Hive.Contracts;
|
---|
31 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
32 |
|
---|
33 | namespace 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 | IClientCommunicator[] clientCommunicatorInstances =
|
---|
60 | discService.GetInstances<IClientCommunicator>();
|
---|
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(IClientManager),
|
---|
98 | binding,
|
---|
99 | "ClientManager");
|
---|
100 |
|
---|
101 | serviceHost.AddServiceEndpoint(
|
---|
102 | typeof(IJobManager),
|
---|
103 | binding,
|
---|
104 | "JobManager");
|
---|
105 |
|
---|
106 | serviceHost.AddServiceEndpoint(
|
---|
107 | typeof(IUserRoleManager),
|
---|
108 | binding,
|
---|
109 | "UserRoleManager");
|
---|
110 |
|
---|
111 | AddMexEndpoint(serviceHost);
|
---|
112 |
|
---|
113 | serviceHost.Open();
|
---|
114 |
|
---|
115 | return serviceHost;
|
---|
116 | } else
|
---|
117 | return null;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public override void Run() {
|
---|
121 | IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
|
---|
122 | int index = 0;
|
---|
123 | if (System.Environment.OSVersion.Version.Major >= 6) {
|
---|
124 | for (index = addresses.Length - 1; index >= 0; index--)
|
---|
125 | if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
|
---|
126 | break;
|
---|
127 | }
|
---|
128 |
|
---|
129 | Uri uriTcp =
|
---|
130 | new Uri("net.tcp://" + addresses[index] + ":" + port + "/HiveServer/");
|
---|
131 |
|
---|
132 | ServiceHost clientCommunicator =
|
---|
133 | StartClientCommunicator(uriTcp);
|
---|
134 |
|
---|
135 | uriTcp =
|
---|
136 | new Uri("net.tcp://" + addresses[index] + ":" + port + "/HiveServerConsole/");
|
---|
137 |
|
---|
138 | ServiceHost serverConsoleFacade =
|
---|
139 | StartServerConsoleFacade(uriTcp);
|
---|
140 |
|
---|
141 | ILifecycleManager[] lifecycleManagers =
|
---|
142 | discService.GetInstances<ILifecycleManager>();
|
---|
143 |
|
---|
144 | if (lifecycleManagers.Length > 0) {
|
---|
145 | ILifecycleManager lifecycleManager =
|
---|
146 | lifecycleManagers[0];
|
---|
147 |
|
---|
148 | lifecycleManager.Init();
|
---|
149 | //sync with db every 5 minutes
|
---|
150 | lifecycleManager.GetTransactionManager().EnableAutoUpdate(
|
---|
151 | new TimeSpan(0, 5, 0));
|
---|
152 |
|
---|
153 | Form mainForm = new MainForm(clientCommunicator.BaseAddresses[0],
|
---|
154 | serverConsoleFacade.BaseAddresses[0]);
|
---|
155 |
|
---|
156 | Application.Run(mainForm);
|
---|
157 |
|
---|
158 | lifecycleManager.Shutdown();
|
---|
159 | }
|
---|
160 |
|
---|
161 | clientCommunicator.Close();
|
---|
162 | serverConsoleFacade.Close();
|
---|
163 |
|
---|
164 |
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|