[15014] | 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 |
|
---|
| 24 | namespace HeuristicLab.Problems.ExternalEvaluation.Service {
|
---|
| 25 | public class PollService : Service {
|
---|
| 26 | private BlockingQueue<SolutionMessage> messageQueue;
|
---|
| 27 |
|
---|
| 28 | public PollService(IListenerFactory driverFactory)
|
---|
| 29 | : base(driverFactory) {
|
---|
| 30 | messageQueue = new BlockingQueue<SolutionMessage>(int.MaxValue);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public override void Stop() {
|
---|
| 34 | messageQueue.Close();
|
---|
| 35 | base.Stop();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public SolutionMessage GetSolution() {
|
---|
| 39 | try {
|
---|
| 40 | return messageQueue.Dequeue();
|
---|
| 41 | } catch (QueueClosedException) { }
|
---|
| 42 | return null;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public QualityMessage.Builder PrepareQualityMessage(SolutionMessage msg, double quality) {
|
---|
[16871] | 46 | SingleObjectiveQualityMessage qmsg = SingleObjectiveQualityMessage.CreateBuilder()
|
---|
| 47 | .SetQuality(quality)
|
---|
| 48 | .Build();
|
---|
[15014] | 49 | QualityMessage.Builder qualityMessageBuilder = QualityMessage.CreateBuilder();
|
---|
| 50 | qualityMessageBuilder.SetSolutionId(msg.SolutionId)
|
---|
[16871] | 51 | .SetType(QualityMessage.Types.Type.SingleObjectiveQualityMessage)
|
---|
| 52 | .SetExtension(SingleObjectiveQualityMessage.QualityMessage_, qmsg);
|
---|
[15014] | 53 | return qualityMessageBuilder;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | public void SendQualityMessage(SolutionMessage sMsg, QualityMessage qMsg) {
|
---|
| 57 | Send(qMsg, sMsg);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public void SendQuality(SolutionMessage msg, double quality) {
|
---|
[16871] | 61 | var answer = PrepareQualityMessage(msg, quality);
|
---|
| 62 | Send(answer.Build(), msg);
|
---|
[15014] | 63 | }
|
---|
| 64 |
|
---|
| 65 | public event EventHandler SolutionProduced;
|
---|
| 66 |
|
---|
| 67 | protected override void OnSolutionProduced(SolutionMessage msg, IChannel channel) {
|
---|
| 68 | base.OnSolutionProduced(msg, channel);
|
---|
| 69 | try {
|
---|
| 70 | messageQueue.Enqueue(msg);
|
---|
| 71 | } catch (QueueClosedException) {
|
---|
| 72 | return;
|
---|
| 73 | }
|
---|
| 74 | EventHandler handler = SolutionProduced;
|
---|
| 75 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | }
|
---|