Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Clients.Access/3.3/ClientInformation.cs @ 8614

Last change on this file since 8614 was 8076, checked in by ascheibe, 13 years ago

#1648 fetch user and client information when the optimizer starts

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