Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/RunCreationClient.cs @ 7554

Last change on this file since 7554 was 7331, checked in by ascheibe, 13 years ago

#1174

  • switched to new build style for branches and improved config merging
  • updated year in license headers and assembly infos
  • updated version in plugin files and assembly infos
File size: 4.4 KB
RevLine 
[4426]1#region License Information
2/* HeuristicLab
[7331]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4426]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;
[4466]23using System.Collections.Generic;
[4426]24using HeuristicLab.Clients.Common;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
[5640]28namespace HeuristicLab.Clients.OKB.RunCreation {
29  [Item("RunCreationClient", "OKB run creation client.")]
30  public sealed class RunCreationClient : IContent {
31    private static RunCreationClient instance;
32    public static RunCreationClient Instance {
[4433]33      get {
[5640]34        if (instance == null) instance = new RunCreationClient();
[4433]35        return instance;
36      }
37    }
38
[4481]39    #region Properties
[5640]40    private List<Algorithm> algorithms;
41    public IEnumerable<Algorithm> Algorithms {
[4426]42      get { return algorithms; }
43    }
[5640]44    private List<Problem> problems;
45    public IEnumerable<Problem> Problems {
[4481]46      get { return problems; }
[4466]47    }
[4481]48    #endregion
[4426]49
[5640]50    private RunCreationClient() {
51      algorithms = new List<Algorithm>();
52      problems = new List<Problem>();
[4549]53    }
[4426]54
[4481]55    #region Refresh
[4433]56    public void Refresh() {
57      OnRefreshing();
[5640]58      algorithms = new List<Algorithm>();
59      problems = new List<Problem>();
60      try {
61        algorithms.AddRange(CallRunCreationService<List<Algorithm>>(s => s.GetAlgorithms("HeuristicLab 3.3")));
62        problems.AddRange(CallRunCreationService<List<Problem>>(s => s.GetProblems("HeuristicLab 3.3")));
63      }
64      finally {
65        OnRefreshed();
66      }
67    }
68    public void RefreshAsync(Action<Exception> exceptionCallback) {
[4441]69      var call = new Func<Exception>(delegate() {
70        try {
[5640]71          Refresh();
[4441]72        }
73        catch (Exception ex) {
74          return ex;
75        }
[5640]76        return null;
[4433]77      });
[4441]78      call.BeginInvoke(delegate(IAsyncResult result) {
79        Exception ex = call.EndInvoke(result);
[5640]80        if (ex != null) exceptionCallback(ex);
[4433]81      }, null);
[4426]82    }
[4481]83    #endregion
[4426]84
[4481]85    #region Algorithm Methods
[5640]86    public static byte[] GetAlgorithmData(long algorithmId) {
87      return CallRunCreationService<byte[]>(s => s.GetAlgorithmData(algorithmId));
[4466]88    }
[4481]89    #endregion
[4466]90
[4481]91    #region Problem Methods
[5640]92    public static byte[] GetProblemData(long problemId) {
93      return CallRunCreationService<byte[]>(s => s.GetProblemData(problemId));
[4481]94    }
[4943]95    #endregion
96
[4587]97    #region Run Methods
[5640]98    public void AddRun(Run run) {
99      CallRunCreationService(s => s.AddRun(run));
[4943]100    }
[4587]101    #endregion
102
[4481]103    #region Events
[4433]104    public event EventHandler Refreshing;
105    private void OnRefreshing() {
106      EventHandler handler = Refreshing;
107      if (handler != null) handler(this, EventArgs.Empty);
108    }
109    public event EventHandler Refreshed;
110    private void OnRefreshed() {
111      EventHandler handler = Refreshed;
112      if (handler != null) handler(this, EventArgs.Empty);
113    }
[4481]114    #endregion
[4433]115
116    #region Helpers
[5640]117    private static void CallRunCreationService(Action<IRunCreationService> call) {
118      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
[4433]119      try {
120        call(client);
121      }
122      finally {
[4426]123        try {
[4433]124          client.Close();
[4426]125        }
[4433]126        catch (Exception) {
127          client.Abort();
[4426]128        }
129      }
130    }
[5640]131    private static T CallRunCreationService<T>(Func<IRunCreationService, T> call) {
132      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
[4433]133      try {
134        return call(client);
[4426]135      }
[4433]136      finally {
137        try {
138          client.Close();
139        }
140        catch (Exception) {
141          client.Abort();
142        }
143      }
[4426]144    }
[4433]145    #endregion
[4426]146  }
147}
Note: See TracBrowser for help on using the repository browser.