Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveStatistics/sources/HeuristicLab.Clients.Hive/3.3/HiveServiceLocator.cs @ 9793

Last change on this file since 9793 was 9461, checked in by pfleck, 12 years ago

#2030
Configured Tcp binding for Hive service on Hive clients.
Added binding-configuration priority list in Hive settings for managing multiple binding-configurations.
Added retry mechanism for multiple Hive-service bindings.

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Clients.Common;
24
25namespace HeuristicLab.Clients.Hive {
26  public class HiveServiceLocator : IHiveServiceLocator {
27    private static IHiveServiceLocator instance = null;
28    public static IHiveServiceLocator Instance {
29      get {
30        if (instance == null) {
31          instance = new HiveServiceLocator();
32        }
33        return instance;
34      }
35    }
36
37    private string username;
38    public string Username {
39      get { return username; }
40      set { username = value; }
41    }
42
43    private string password;
44    public string Password {
45      get { return password; }
46      set { password = value; }
47    }
48
49    private int endpointRetries;
50
51    private string workingEndpoint;
52
53    private HiveServiceClient NewServiceClient() {
54      if (endpointRetries >= Settings.Default.MaxEndpointRetries) {
55        return CreateClient(workingEndpoint);
56      }
57
58      var configurations = Settings.Default.EndpointConfigurationPriorities;
59
60      Exception exception = null;
61      foreach (var endpointConfigurationName in configurations) {
62        try {
63          var cl = CreateClient(endpointConfigurationName);
64          cl.Open();
65          workingEndpoint = endpointConfigurationName;
66          return cl;
67        }
68        catch (Exception exc) {
69          exception = exc;
70          endpointRetries++;
71        }
72      }
73
74      throw exception;
75    }
76
77    private HiveServiceClient CreateClient(string endpointConfigurationName) {
78      HiveServiceClient cl = null;
79
80      if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
81        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName);
82      else
83        cl = ClientFactory.CreateClient<HiveServiceClient, IHiveService>(endpointConfigurationName, null, username, password);
84
85      return cl;
86    }
87
88    public T CallHiveService<T>(Func<IHiveService, T> call) {
89      HiveServiceClient client = NewServiceClient();
90      HandleAnonymousUser(client);
91
92      try {
93        return call(client);
94      }
95      finally {
96        try {
97          client.Close();
98        }
99        catch (Exception) {
100          client.Abort();
101        }
102      }
103    }
104
105    public void CallHiveService(Action<IHiveService> call) {
106      HiveServiceClient client = NewServiceClient();
107      HandleAnonymousUser(client);
108
109      try {
110        call(client);
111      }
112      finally {
113        try {
114          client.Close();
115        }
116        catch (Exception) {
117          client.Abort();
118        }
119      }
120    }
121
122    private void HandleAnonymousUser(HiveServiceClient client) {
123      if (client.ClientCredentials.UserName.UserName == Settings.Default.AnonymousUserName) {
124        try {
125          client.Close();
126        }
127        catch (Exception) {
128          client.Abort();
129        }
130        throw new AnonymousUserException();
131      }
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.