1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.ServiceModel.Security;
|
---|
24 | using System.Threading.Tasks;
|
---|
25 |
|
---|
26 | namespace 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 | } catch (MessageSecurityException e) {
|
---|
89 | //wrong username or password
|
---|
90 | clientExists = false;
|
---|
91 | errorOccured = true;
|
---|
92 | occuredException = e;
|
---|
93 | } catch (Exception e) {
|
---|
94 | clientExists = false;
|
---|
95 | errorOccured = true;
|
---|
96 | occuredException = e;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void Refresh() {
|
---|
101 | FetchClientInformationFromServer();
|
---|
102 | }
|
---|
103 |
|
---|
104 | private static void InitializeClientInformation() {
|
---|
105 | if (instance == null) instance = new ClientInformation();
|
---|
106 | }
|
---|
107 |
|
---|
108 | public static void InitializeAsync() {
|
---|
109 | Task.Factory.StartNew(InitializeClientInformation);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|