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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
31 | [Item("EvaluationServiceClient", "An RPC client that evaluates a solution.")]
|
---|
32 | [StorableClass]
|
---|
33 | public class EvaluationServiceClient : ParameterizedNamedItem, IEvaluationServiceClient {
|
---|
34 | public override bool CanChangeName { get { return false; } }
|
---|
35 | public override bool CanChangeDescription { get { return false; } }
|
---|
36 |
|
---|
37 | public IValueParameter<IEvaluationChannel> ChannelParameter {
|
---|
38 | get { return (IValueParameter<IEvaluationChannel>)Parameters["Channel"]; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | private IEvaluationChannel Channel {
|
---|
42 | get { return ChannelParameter.Value; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public EvaluationServiceClient()
|
---|
46 | : base() {
|
---|
47 | Parameters.Add(new ValueParameter<IEvaluationChannel>("Channel", "The channel over which to call the remote function."));
|
---|
48 | }
|
---|
49 |
|
---|
50 | #region IEvaluationServiceClient Members
|
---|
51 |
|
---|
52 | public QualityMessage Evaluate(SolutionMessage solution) {
|
---|
53 | CheckAndOpenChannel();
|
---|
54 | Channel.Send(solution);
|
---|
55 | return (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
|
---|
59 | CheckAndOpenChannel();
|
---|
60 | Channel.Send(solution);
|
---|
61 | System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ReceiveAsync), callback);
|
---|
62 | }
|
---|
63 |
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | private void CheckAndOpenChannel() {
|
---|
67 | if (Channel == null) throw new InvalidOperationException(Name + ": The channel is not defined.");
|
---|
68 | if (!Channel.IsInitialized) {
|
---|
69 | try {
|
---|
70 | Channel.Open();
|
---|
71 | } catch (Exception e) { // TODO: Change to specific exception
|
---|
72 | throw new InvalidOperationException(Name + ": The channel could not be opened.", e);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | private void ReceiveAsync(object callback) {
|
---|
78 | QualityMessage message = (QualityMessage)Channel.Receive(QualityMessage.CreateBuilder());
|
---|
79 | ((Action<QualityMessage>)callback).Invoke(message);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|