Free cookie consent management tool by TermsFeed Policy Generator

source: misc/tools/ExternalEvaluation/CSharp/ExternalEvaluation.Service/Service.cs @ 17187

Last change on this file since 17187 was 15014, checked in by pfleck, 8 years ago

Added code and tools for the ExternalEvaluationProblem. (e.g. Java-side evaluation)

File size: 4.6 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 System.Threading;
27using Google.ProtocolBuffers;
28using HeuristicLab.Problems.ExternalEvaluation;
29
30namespace HeuristicLab.Problems.ExternalEvaluation.Service {
31  public abstract class Service {
32    private static readonly int maxConnections = 10;
33    protected IListener listener;
34    private Dictionary<SolutionMessage, IChannel> recipientMemory;
35    private List<IChannel> channels;
36
37    public Service(IListenerFactory listenerFactory) {
38      channels = new List<IChannel>();
39      recipientMemory = new Dictionary<SolutionMessage, IChannel>();
40      listener = listenerFactory.CreateListener();
41      listener.Discovered += new EventHandler<EventArgs<IChannel>>(listener_Discovered);
42    }
43
44    public virtual void Start() {
45      listener.Listen();
46    }
47
48    public virtual void Stop() {
49      listener.Stop();
50      lock (channels) {
51        foreach (IChannel channel in channels)
52          channel.Close();
53        channels.Clear();
54      }
55      lock (recipientMemory) {
56        recipientMemory.Clear();
57      }
58    }
59
60    private void listener_Discovered(object sender, EventArgs<IChannel> e) {
61      lock (channels) {
62        if (channels.Count < maxConnections) {
63          channels.Add(e.Value);
64          SolutionProducer producer = new SolutionProducer(this, e.Value);
65          Thread tmp = new Thread(producer.Produce);
66          producer.Finished += new EventHandler<EventArgs<IChannel>>(producer_Finished);
67          tmp.Start();
68        } else e.Value.Close();
69      }
70    }
71
72    private void producer_Finished(object sender, EventArgs<IChannel> e) {
73      lock (channels) {
74        channels.Remove(e.Value);
75      }
76      lock (recipientMemory) {
77        var solutions = recipientMemory.Where(x => x.Value == e.Value).Select(x => x.Key).ToList();
78        foreach (SolutionMessage msg in solutions)
79          recipientMemory.Remove(msg);
80      }
81    }
82
83    protected virtual void Send(QualityMessage msg, SolutionMessage original) {
84      lock (recipientMemory) {
85        if (recipientMemory.ContainsKey(original)) {
86          recipientMemory[original].Send(msg);
87          recipientMemory.Remove(original);
88        }
89      }
90    }
91
92    protected virtual void OnSolutionProduced(SolutionMessage msg, IChannel channel) {
93      lock (recipientMemory) {
94        recipientMemory.Add(msg, channel);
95      }
96    }
97
98    private class SolutionProducer {
99      private IChannel channel;
100      private Service service;
101
102      public SolutionProducer(Service service, IChannel channel) {
103        this.service = service;
104        this.channel = channel;
105      }
106
107      public void Produce() {
108        while (true) {
109          SolutionMessage.Builder builder = SolutionMessage.CreateBuilder();
110          try {
111            SolutionMessage msg = (SolutionMessage)channel.Receive(builder);
112            MessageNotifier notifier = new MessageNotifier(msg, channel);
113            ThreadPool.QueueUserWorkItem(new WaitCallback(notifier.Notify), service);
114          } catch (Exception) {
115            break;
116          }
117        }
118        OnFinished();
119      }
120
121      public event EventHandler<EventArgs<IChannel>> Finished;
122      private void OnFinished() {
123        var handler = Finished;
124        if (handler != null) handler(this, new EventArgs<IChannel>(channel));
125      }
126
127      private class MessageNotifier {
128        SolutionMessage msg;
129        IChannel channel;
130
131        public MessageNotifier(SolutionMessage msg, IChannel channel) {
132          this.msg = msg;
133          this.channel = channel;
134        }
135
136        public void Notify(object state) {
137          Service service = (Service)state;
138          service.OnSolutionProduced(msg, channel);
139        }
140      }
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.