source: branches/ClientUserManagement/HeuristicLab.Clients.Access/3.3/ClientInformation.cs @ 7553

Last change on this file since 7553 was 7553, checked in by ascheibe, 8 years ago

#1648 adapted client management for use with Hive

File size: 3.0 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 System.ServiceModel.Security;
24
25namespace HeuristicLab.Clients.Access {
26  public sealed class ClientInformation {
27    private static ClientInformation instance;
28    public static ClientInformation Instance {
29      get {
30        if (instance == null) instance = new ClientInformation();
31        return instance;
32      }
33    }
34
35    private Client clientInfo;
36    public Client ClientInfo {
37      get { return clientInfo; }
38    }
39
40    private bool clientExists = false;
41    public bool ClientExists {
42      get { return clientExists; }
43    }
44
45    private bool errorOccured = false;
46    public bool ErrorOccured {
47      get { return errorOccured; }
48    }
49
50    private Exception occuredException;
51    public Exception OccuredException {
52      get { return occuredException; }
53    }
54
55    private ClientInformation() {
56      if (ClientInformationUtils.IsClientHeuristicLab()) {
57        FetchClientInformationFromServer();
58      } else {
59        // this means we are executed by an Hive slave, therefore we just get our machine id (e.g. for OKB Algs)
60        // because the slave has already done the registration process
61        GenerateLocalClientConfig();
62      }
63    }
64
65    private void GenerateLocalClientConfig() {
66      clientExists = true;
67      errorOccured = false;
68      occuredException = null;
69      clientInfo = new Client();
70      clientInfo.Id = ClientInformationUtils.GetUniqueMachineId();
71    }
72
73    private void FetchClientInformationFromServer() {
74      Guid clientId = ClientInformationUtils.GetUniqueMachineId();
75
76      try {
77        AccessClient.CallAccessService(x => clientInfo = x.GetClient(clientId));
78        if (clientInfo != null)
79          clientExists = true;
80        errorOccured = false;
81        occuredException = null;
82      }
83      catch (MessageSecurityException e) {
84        //wrong username or password
85        clientExists = false;
86        errorOccured = true;
87        occuredException = e;
88      }
89      catch (Exception e) {
90        clientExists = false;
91        errorOccured = true;
92        occuredException = e;
93      }
94    }
95
96    public void Refresh() {
97      FetchClientInformationFromServer();
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.