Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Server/3.3/Server.cs @ 2050

Last change on this file since 2050 was 2050, checked in by gkronber, 15 years ago

trivial refactoring of CEDMA server and server form. #644

File size: 4.1 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.Net;
28using System.ServiceModel;
29using HeuristicLab.CEDMA.DB.Interfaces;
30using HeuristicLab.CEDMA.DB;
31using System.ServiceModel.Description;
32
33namespace HeuristicLab.CEDMA.Server {
34  public class Server {
35    private static readonly string rdfFile = AppDomain.CurrentDomain.BaseDirectory + "rdf_store.db3";
36    private static readonly string rdfConnectionString = "sqlite:rdf:Data Source=\"" + rdfFile + "\"";
37
38    private ServiceHost host;
39    private Store store;
40    private IDispatcher dispatcher;
41    private IExecuter executer;
42
43    private string gridServiceUrl;
44    public string GridServiceUrl {
45      get { return gridServiceUrl; }
46      set { gridServiceUrl = value; }
47    }
48
49    private string cedmaServiceUrl;
50    public string CedmaServiceUrl {
51      get { return cedmaServiceUrl; }
52      set { cedmaServiceUrl = value; }
53    }
54
55    private int maxActiveJobs;
56    public int MaxActiveJobs {
57      get { return maxActiveJobs; }
58      set {
59        if (value > 0 && value <= 64) {
60          maxActiveJobs = value;
61        }
62      }
63    }
64
65    public Server() {
66      IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
67      // windows XP returns the external ip on index 0 while windows vista returns the external ip as one of the last entries
68      // also if IPv6 protocol is installed we want to find an entry that is IPv4
69      int index = 0;
70      if (System.Environment.OSVersion.Version.Major >= 6) {
71        for (index = addresses.Length - 1; index >= 0; index--)
72          if (addresses[index].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
73            break;
74      }
75      cedmaServiceUrl = "net.tcp://" + addresses[index] + ":8002/CEDMA";
76      store = new Store(rdfConnectionString);
77      maxActiveJobs = 10;
78    }
79
80    public void Start() {
81      host = new ServiceHost(store, new Uri(cedmaServiceUrl));
82      ServiceThrottlingBehavior throttlingBehavior = new ServiceThrottlingBehavior();
83      throttlingBehavior.MaxConcurrentSessions = 20;
84      host.Description.Behaviors.Add(throttlingBehavior);
85      try {
86        NetTcpBinding binding = new NetTcpBinding();
87        binding.SendTimeout = new TimeSpan(10, 0, 0);
88        binding.MaxReceivedMessageSize = int.MaxValue;
89        binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
90        binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
91        host.AddServiceEndpoint(typeof(IStore), binding, cedmaServiceUrl);
92        host.Open();
93      }
94      catch (CommunicationException ex) {
95        MessageBox.Show("An exception occurred: " + ex.Message);
96        host.Abort();
97      }
98    }
99
100    internal string[] GetActiveJobDescriptions() {
101      if (executer != null) return executer.GetJobs();
102      else return new string[] { };
103    }
104
105    internal void Connect(string serverUrl) {
106      dispatcher = new SimpleDispatcher(store);
107      if (serverUrl.Contains("ExecutionEngine")) {
108        executer = new HiveExecuter(dispatcher, store, serverUrl);
109      } else {
110        // default is grid backend
111        executer = new GridExecuter(dispatcher, store, serverUrl);
112      }
113      executer.Start();
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.