Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7225 was 6519, checked in by abeham, 13 years ago

#1516

  • fixed code formatting
  • added some license headers
  • removed unnecessary resx file
File size: 5.3 KB
RevLine 
[3872]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3872]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;
[4722]23using HeuristicLab.Common;
[3872]24using HeuristicLab.Core;
[4068]25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
[3872]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.ExternalEvaluation {
30  [Item("EvaluationServiceClient", "An RPC client that evaluates a solution.")]
31  [StorableClass]
32  public class EvaluationServiceClient : ParameterizedNamedItem, IEvaluationServiceClient {
[6470]33
[3872]34    public override bool CanChangeName { get { return false; } }
35    public override bool CanChangeDescription { get { return false; } }
36
[6470]37    #region Parameters
[3872]38    public IValueParameter<IEvaluationChannel> ChannelParameter {
39      get { return (IValueParameter<IEvaluationChannel>)Parameters["Channel"]; }
40    }
[3895]41    public IValueParameter<IntValue> RetryParameter {
42      get { return (IValueParameter<IntValue>)Parameters["Retry"]; }
43    }
[3872]44
45    private IEvaluationChannel Channel {
46      get { return ChannelParameter.Value; }
47    }
[6470]48    #endregion
[3872]49
[4722]50
[6470]51    #region Construction & Cloning
[4722]52    [StorableConstructor]
53    protected EvaluationServiceClient(bool deserializing) : base(deserializing) { }
[6519]54    protected EvaluationServiceClient(EvaluationServiceClient original, Cloner cloner)
55      : base(original, cloner) {
[6470]56      RegisterEvents();
[6519]57    }
[3872]58    public EvaluationServiceClient()
59      : base() {
60      Parameters.Add(new ValueParameter<IEvaluationChannel>("Channel", "The channel over which to call the remote function."));
[3895]61      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)));
[6470]62      RegisterEvents();
[3872]63    }
[6470]64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new EvaluationServiceClient(this, cloner);
66    }
67    [StorableHook(HookType.AfterDeserialization)]
68    private void AfterDeserialization() {
69      ChannelParameter_ValueChanged(this, EventArgs.Empty);
70      RegisterEvents();
71    }
72    #endregion
[3872]73
74    #region IEvaluationServiceClient Members
75    public QualityMessage Evaluate(SolutionMessage solution) {
[3895]76      int tries = 0, maxTries = RetryParameter.Value.Value;
77      bool success = false;
78      QualityMessage result = null;
79      while (!success) {
80        try {
81          tries++;
82          CheckAndOpenChannel();
83          Channel.Send(solution);
84          result = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
85          success = true;
[6519]86        } catch (InvalidOperationException) {
[3895]87          throw;
[6519]88        } catch {
[3895]89          if (tries >= maxTries)
90            throw;
91        }
92      }
93      return result;
[3872]94    }
95
96    public void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
[3895]97      int tries = 0, maxTries = RetryParameter.Value.Value;
98      bool success = false;
99      while (!success) {
100        try {
101          tries++;
102          CheckAndOpenChannel();
103          Channel.Send(solution);
104          success = true;
[6519]105        } catch (InvalidOperationException) {
[3895]106          throw;
[6519]107        } catch {
[3895]108          if (tries >= maxTries)
109            throw;
110        }
111      }
[3872]112      System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveAsync), callback);
113    }
114    #endregion
115
[6470]116    #region Auxiliary Methods
[3872]117    private void CheckAndOpenChannel() {
118      if (Channel == null) throw new InvalidOperationException(Name + ": The channel is not defined.");
119      if (!Channel.IsInitialized) {
120        try {
121          Channel.Open();
[6519]122        } catch (Exception e) {
[3872]123          throw new InvalidOperationException(Name + ": The channel could not be opened.", e);
124        }
125      }
126    }
127
128    private void ReceiveAsync(object callback) {
[3895]129      QualityMessage message = null;
130      try {
131        message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
[6519]132      } catch { }
[3872]133      ((Action<QualityMessage>)callback).Invoke(message);
134    }
[6470]135
136    private void RegisterEvents() {
137      ChannelParameter.ValueChanged += new EventHandler(ChannelParameter_ValueChanged);
138    }
139
140    void ChannelParameter_ValueChanged(object sender, EventArgs e) {
141      if (ChannelParameter.Value == null)
142        name = "Empty EvaluationServiceClient";
143      else
144        name = String.Format("{0} ServiceClient", ChannelParameter.Value.Name);
145      OnNameChanged();
146    }
147    #endregion
[3872]148  }
149}
Note: See TracBrowser for help on using the repository browser.