[8042] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8042] | 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;
|
---|
[8076] | 24 | using System.Threading.Tasks;
|
---|
[8042] | 25 |
|
---|
| 26 | namespace HeuristicLab.Clients.Access {
|
---|
| 27 | public sealed class ClientInformation {
|
---|
| 28 | private static ClientInformation instance;
|
---|
| 29 | public static ClientInformation Instance {
|
---|
| 30 | get {
|
---|
[8076] | 31 | InitializeClientInformation();
|
---|
[8042] | 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() {
|
---|
[8076] | 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 | }
|
---|
[8042] | 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 | }
|
---|
[8076] | 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 | }
|
---|
[8042] | 113 | }
|
---|
| 114 | }
|
---|