Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.OKB/3.3/RunCreation/RunCreationClient.cs @ 11845

Last change on this file since 11845 was 11170, checked in by ascheibe, 10 years ago

#2115 updated copyright year in stable branch

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections.Generic;
24using HeuristicLab.Clients.Common;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27
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 {
33      get {
34        if (instance == null) instance = new RunCreationClient();
35        return instance;
36      }
37    }
38
39    #region Properties
40    private List<Algorithm> algorithms;
41    public IEnumerable<Algorithm> Algorithms {
42      get { return algorithms; }
43    }
44    private List<Problem> problems;
45    public IEnumerable<Problem> Problems {
46      get { return problems; }
47    }
48    #endregion
49
50    private RunCreationClient() {
51      algorithms = new List<Algorithm>();
52      problems = new List<Problem>();
53    }
54
55    #region Refresh
56    public void Refresh() {
57      OnRefreshing();
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) {
69      var call = new Func<Exception>(delegate() {
70        try {
71          Refresh();
72        }
73        catch (Exception ex) {
74          return ex;
75        }
76        return null;
77      });
78      call.BeginInvoke(delegate(IAsyncResult result) {
79        Exception ex = call.EndInvoke(result);
80        if (ex != null) exceptionCallback(ex);
81      }, null);
82    }
83    #endregion
84
85    #region Algorithm Methods
86    public static byte[] GetAlgorithmData(long algorithmId) {
87      return CallRunCreationService<byte[]>(s => s.GetAlgorithmData(algorithmId));
88    }
89    #endregion
90
91    #region Problem Methods
92    public static byte[] GetProblemData(long problemId) {
93      return CallRunCreationService<byte[]>(s => s.GetProblemData(problemId));
94    }
95    #endregion
96
97    #region Run Methods
98    public void AddRun(Run run) {
99      CallRunCreationService(s => s.AddRun(run));
100    }
101    #endregion
102
103    #region Events
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    }
114    #endregion
115
116    #region Helpers
117    private static void CallRunCreationService(Action<IRunCreationService> call) {
118      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
119      try {
120        call(client);
121      }
122      finally {
123        try {
124          client.Close();
125        }
126        catch (Exception) {
127          client.Abort();
128        }
129      }
130    }
131    private static T CallRunCreationService<T>(Func<IRunCreationService, T> call) {
132      RunCreationServiceClient client = ClientFactory.CreateClient<RunCreationServiceClient, IRunCreationService>();
133      try {
134        return call(client);
135      }
136      finally {
137        try {
138          client.Close();
139        }
140        catch (Exception) {
141          client.Abort();
142        }
143      }
144    }
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.