Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3932 was 3895, checked in by abeham, 15 years ago

#866

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