Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.Contracts/3.3/WcfSettings.cs @ 4423

Last change on this file since 4423 was 4423, checked in by cneumuel, 14 years ago
  • Refactored HL.Hive.Experiment. JobItems are not called HiveJobs and OptimizerJobs do not contain a hierarchy anymore.
  • Dynamic generation of jobs on a slave are not reflected on the client user interface.
  • Optimizer-Trees are now strictly synchronized with the HiveJob-Trees (also the ComputeInParallel property is taken into account when the Child HiveJobs are created)
  • Improved the way a class can report progress and lock the UI (IProgressReporter, IProgress, Progress, ProgressView)
  • Changes were made to the config-files, so that server and clients work with blade12.hpc.fh-hagenberg.at
  • Lots of small changes and bugfixes
File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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//#define USE_MSG_BINDING
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.ServiceModel.Channels;
27using System.ServiceModel;
28using System.Security.Cryptography.X509Certificates;
29using System.Net;
30using System.ServiceModel.Description;
31using HeuristicLab.Tracing;
32
33namespace HeuristicLab.Hive.Contracts {
34  public static class WcfSettings {
35    public const string SlaveServiceName = "SlaveService";
36    public const string ServerConsoleServiceName = "ServerConsoleService";
37    public const string ClientServiceName = "ClientService";
38    public const string ClientStreamedServiceName = "ClientServiceStreamed";
39
40    public const int DefaultPort = 9000;
41   
42    /// <summary>
43    /// Gets a pre-defined binding using TCP for secure transport.
44    /// </summary>
45    /// <returns>A binding type of <see cref="NetTcpBinding"/></returns>
46    public static Binding GetBinding() {
47#if USE_MSG_BINDING
48      NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
49#else
50      NetTcpBinding binding = new NetTcpBinding();
51#endif
52      binding.MaxBufferSize = int.MaxValue;
53      binding.MaxReceivedMessageSize = int.MaxValue;
54      binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
55      binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
56      binding.CloseTimeout = new TimeSpan(0, 5, 0);
57      binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
58      binding.SendTimeout = new TimeSpan(0, 5, 0);
59      return binding;
60    }
61
62    public static Binding GetStreamedBinding() {
63#if USE_MSG_BINDING
64      NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
65#else
66      NetTcpBinding binding = new NetTcpBinding();
67#endif
68      binding.TransferMode = TransferMode.Streamed;
69      binding.MaxReceivedMessageSize = int.MaxValue;
70      binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
71      binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
72      binding.CloseTimeout = new TimeSpan(0, 5, 0);
73      binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
74      binding.SendTimeout = new TimeSpan(0, 5, 0);
75      //Disabling security mode, for the moment
76      binding.Security.Mode = SecurityMode.None;
77     
78      return binding;
79    }
80
81//    /// <summary>
82//    /// Defines the used certificate for authentification located in a certification store.
83//    /// </summary>
84//    /// <param name="svchost">A service for which this certificate is applicable.</param>
85//    public static void SetServiceCertificate(ServiceHost svchost) {
86//#if USE_MSG_BINDING
87//      svchost.Credentials.ServiceCertificate.SetCertificate(
88//        StoreLocation.LocalMachine,
89//        StoreName.My,
90//        X509FindType.FindBySubjectName,
91//        SERVERCERT);
92//#endif
93//    }
94
95    /// <summary>
96    /// Gets the currently active IP address.
97    /// <remarks>If more than one IP connections is active, the first one will be used.</remarks>
98    /// </summary>
99    /// <returns></returns>
100    public static IPAddress GetActiveIP() {
101      return System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()[0].LocalEndPoint.Address;
102    }
103
104    /// <summary>
105    /// Gets the default port used for HIVE services.
106    /// </summary>
107    /// <returns></returns>
108    public static int GetDefaultPort() {
109      return DefaultPort;
110    }
111   
112    /// <summary>
113    /// This method changes the endpoint-address while preserving the identity-certificate defined in the config file
114    /// </summary>
115    public static void SetEndpointAddress(ServiceEndpoint endpoint, string hostAddress) {
116      EndpointAddressBuilder builder = new EndpointAddressBuilder(endpoint.Address);
117      UriBuilder uriBuilder = new UriBuilder(builder.Uri);
118      uriBuilder.Host = hostAddress;
119      builder.Uri = uriBuilder.Uri;
120      endpoint.Address = builder.ToEndpointAddress();
121    }
122
123    /// <summary>
124    /// Securely disposes a WCF client proxy object
125    /// </summary>
126    /// <param name="obj"></param>
127    public static void DisposeWcfClient(ICommunicationObject obj) {
128      if (obj != null) {
129        if (obj.State != CommunicationState.Faulted &&
130            obj.State != CommunicationState.Closed) {
131          try { obj.Close(); }
132          catch (CommunicationObjectFaultedException) { obj.Abort(); }
133          catch (TimeoutException) { obj.Abort(); }
134          catch (Exception e) {
135            obj.Abort();
136            Logger.Error(e);
137          }
138        } else
139          obj.Abort();
140      }
141    }
142
143  }
144
145  /// <summary>
146  /// This class verifies the certificate defined by <see cref="SetServerCertificate"></see> method. Normally,
147  /// the verification process is managed by the underlying operating system.
148  /// </summary>
149  /// <remarks>
150  /// WARNUNG: Dieser Code wird nur für Testzertifikate benötigt, wie sie beispielsweise von makecert erstellt werden.
151  /// Sie sollten diesen Code nicht in einer Produktionsumgebung verwenden.
152  /// </remarks>
153  /*
154  public class PermissiveCertificatePolicy {
155    string subjectName;
156    static PermissiveCertificatePolicy currentPolicy;
157    PermissiveCertificatePolicy(string subjectName) {
158      this.subjectName = subjectName;
159      ServicePointManager.ServerCertificateValidationCallback +=
160          new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate);
161    }
162
163    public static void Enact(string subjectName) {
164      currentPolicy = new PermissiveCertificatePolicy(subjectName);
165    }
166
167    bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) {
168      if (cert.Subject == subjectName) {
169        return true;
170      }
171      return false;
172    }
173  }             */
174}
Note: See TracBrowser for help on using the repository browser.