Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1106 was 1099, checked in by msteinbi, 15 years ago

Implementing Lifecycle Management (#453)

File size: 4.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    public override void Run() {
111      IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
112      int index = 0;
113      if (System.Environment.OSVersion.Version.Major >= 6) {
114        for (index = addresses.Length - 1; index >= 0; index--)
115          if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
116            break;
117      }
118
119      Uri uriTcp =
120          new Uri("net.tcp://" + addresses[index] + ":" + port + "/HiveServer/");
121
122      ServiceHost clientCommunicator =
123        StartClientCommunicator(uriTcp);
124
125      uriTcp =
126        new Uri("net.tcp://" + addresses[index] + ":" + port + "/HiveServerConsole/");
127
128      ServiceHost serverConsoleFacade =
129        StartServerConsoleFacade(uriTcp);
130
131      ILifecycleManager[] lifecycleManagers =
132         discService.GetInstances<ILifecycleManager>();
133
134      if (lifecycleManagers.Length > 0) {
135        ILifecycleManager lifecycleManager =
136          lifecycleManagers[0];
137
138        lifecycleManager.Init();
139        //sync with db every 5 minutes
140        lifecycleManager.GetTransactionManager().EnableAutoUpdate(
141          new TimeSpan(0, 0, 10));
142
143        Form mainForm = new MainForm(clientCommunicator.BaseAddresses[0],
144            serverConsoleFacade.BaseAddresses[0]);
145
146         Application.Run(mainForm);
147
148         lifecycleManager.Shutdown();
149      }
150
151      clientCommunicator.Close();
152      serverConsoleFacade.Close();
153
154     
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.