Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 831 was 805, checked in by kgrading, 16 years ago

work on #385, core problem still exists.

File size: 4.6 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)]
37  class HiveServerApplication : ApplicationBase {
[805]38    const int port =
[800]39      9000;
[741]40
[800]41    DiscoveryService discService =
42        new DiscoveryService();
[741]43
[800]44    private bool AddMexEndpoint(ServiceHost serviceHost) {
[805]45      if (serviceHost != null) {
[800]46        ServiceMetadataBehavior behavior =
47            new ServiceMetadataBehavior();
[805]48        serviceHost.Description.Behaviors.Add(behavior);
[800]49
[805]50        return serviceHost.AddServiceEndpoint(
51          typeof(IMetadataExchange),
52          MetadataExchangeBindings.CreateMexTcpBinding(),
53          "mex") != null;
[800]54      } else
55        return false;
56    }
57
58    private ServiceHost StartClientCommunicator(Uri uriTcp) {
59      IClientCommunicator[] clientCommunicatorInstances =
[741]60        discService.GetInstances<IClientCommunicator>();
61
[800]62      if (clientCommunicatorInstances.Length > 0) {
[741]63        ServiceHost serviceHost =
[800]64                new ServiceHost(clientCommunicatorInstances[0].GetType(),
[753]65                  uriTcp);
[741]66
[800]67        System.ServiceModel.Channels.Binding binding =
[753]68          new NetTcpBinding();
69
70        serviceHost.AddServiceEndpoint(
71          typeof(IClientCommunicator),
72              binding,
73              "ClientCommunicator");
74
[800]75        AddMexEndpoint(serviceHost);
[753]76
[800]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
[753]96        serviceHost.AddServiceEndpoint(
[800]97          typeof(IClientManager),
98              binding,
99              "ClientManager");
[753]100
[800]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
[741]113        serviceHost.Open();
114
[800]115        return serviceHost;
116      } else
117        return null;
118    }
[741]119
[800]120    public override void Run() {
[805]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      }
[800]128
129      Uri uriTcp =
[804]130          new Uri("net.tcp://" + addresses[index] + ":" + port + "/HiveServer/");
[800]131
[805]132      ServiceHost clientCommunicator =
[800]133        StartClientCommunicator(uriTcp);
134
135      uriTcp =
[804]136        new Uri("net.tcp://" + addresses[index] + ":" + port + "/HiveServerConsole/");
[800]137
138      ServiceHost serverConsoleFacade =
139        StartServerConsoleFacade(uriTcp);
140
141      Form mainForm = new MainForm(clientCommunicator.BaseAddresses[0],
142        serverConsoleFacade.BaseAddresses[0]);
143
144      Application.Run(mainForm);
145
146      clientCommunicator.Close();
147      serverConsoleFacade.Close();
[713]148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.