Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/EvaluationServiceClient.cs @ 4555

Last change on this file since 4555 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.ExternalEvaluation {
29  [Item("EvaluationServiceClient", "An RPC client that evaluates a solution.")]
30  [StorableClass]
31  public class EvaluationServiceClient : ParameterizedNamedItem, IEvaluationServiceClient {
32    public override bool CanChangeName { get { return false; } }
33    public override bool CanChangeDescription { get { return false; } }
34
35    public IValueParameter<IEvaluationChannel> ChannelParameter {
36      get { return (IValueParameter<IEvaluationChannel>)Parameters["Channel"]; }
37    }
38    public IValueParameter<IntValue> RetryParameter {
39      get { return (IValueParameter<IntValue>)Parameters["Retry"]; }
40    }
41
42    private IEvaluationChannel Channel {
43      get { return ChannelParameter.Value; }
44    }
45
46    public EvaluationServiceClient()
47      : base() {
48      Parameters.Add(new ValueParameter<IEvaluationChannel>("Channel", "The channel over which to call the remote function."));
49      Parameters.Add(new ValueParameter<IntValue>("Retry", "How many times the client should retry obtaining a quality in case there is an exception. Note that it immediately aborts when the channel cannot be opened.", new IntValue(10)));
50    }
51
52    #region IEvaluationServiceClient Members
53
54    public QualityMessage Evaluate(SolutionMessage solution) {
55      int tries = 0, maxTries = RetryParameter.Value.Value;
56      bool success = false;
57      QualityMessage result = null;
58      while (!success) {
59        try {
60          tries++;
61          CheckAndOpenChannel();
62          Channel.Send(solution);
63          result = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
64          success = true;
65        }
66        catch (InvalidOperationException) {
67          throw;
68        }
69        catch {
70          if (tries >= maxTries)
71            throw;
72        }
73      }
74      return result;
75    }
76
77    public void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
78      int tries = 0, maxTries = RetryParameter.Value.Value;
79      bool success = false;
80      while (!success) {
81        try {
82          tries++;
83          CheckAndOpenChannel();
84          Channel.Send(solution);
85          success = true;
86        }
87        catch (InvalidOperationException) {
88          throw;
89        }
90        catch {
91          if (tries >= maxTries)
92            throw;
93        }
94      }
95      System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveAsync), callback);
96    }
97
98    #endregion
99
100    private void CheckAndOpenChannel() {
101      if (Channel == null) throw new InvalidOperationException(Name + ": The channel is not defined.");
102      if (!Channel.IsInitialized) {
103        try {
104          Channel.Open();
105        }
106        catch (Exception e) {
107          throw new InvalidOperationException(Name + ": The channel could not be opened.", e);
108        }
109      }
110    }
111
112    private void ReceiveAsync(object callback) {
113      QualityMessage message = null;
114      try {
115        message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
116      }
117      catch { }
118      ((Action<QualityMessage>)callback).Invoke(message);
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.