1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Threading;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HEAL.Attic;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.PluginInfrastructure {
|
---|
9 | [StorableType(StorableMemberSelection.MarkedOnly, "D93CBE04-9847-417A-AAB5-7FBCA6A32247")]
|
---|
10 | public abstract class Runner : IRunner {
|
---|
11 |
|
---|
12 | #region Vars
|
---|
13 | private Thread listener;
|
---|
14 | #endregion
|
---|
15 |
|
---|
16 | #region Properties
|
---|
17 | [Storable]
|
---|
18 | public IEnumerable<AssemblyInfo> AssembliesToLoad { get; set; }
|
---|
19 |
|
---|
20 | internal RunnerHost Host { get; set; }
|
---|
21 | #endregion
|
---|
22 |
|
---|
23 | public void Pause() {
|
---|
24 | var message = new PauseRunnerMessage();
|
---|
25 | if (Host != null) Host.Send(message);
|
---|
26 | else OnRunnerMessage(message);
|
---|
27 | }
|
---|
28 |
|
---|
29 | public void Resume() {
|
---|
30 | var message = new ResumeRunnerMessage();
|
---|
31 | if (Host != null) Host.Send(message);
|
---|
32 | else OnRunnerMessage(message);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public void Cancel() {
|
---|
36 | var message = new CancelRunnerMessage();
|
---|
37 | if (Host != null) Host.Send(message);
|
---|
38 | else OnRunnerMessage(message);
|
---|
39 | }
|
---|
40 |
|
---|
41 | public void Run() {
|
---|
42 | IPluginLoader loader = PluginLoaderFactory.Create();
|
---|
43 | loader.LoadPlugins(AssembliesToLoad);
|
---|
44 | Task t = Task.Run(Execute);
|
---|
45 | StartListener();
|
---|
46 | t.Wait();
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected abstract void Execute();
|
---|
50 | protected abstract void OnRunnerMessage(RunnerMessage message);
|
---|
51 |
|
---|
52 | #region Helper
|
---|
53 |
|
---|
54 | private void StartListener() {
|
---|
55 | listener = new Thread(() => {
|
---|
56 | Stream stdin = Console.OpenStandardInput();
|
---|
57 | DateTime lastMessageRecieved = DateTime.MinValue;
|
---|
58 | while (true) {
|
---|
59 | RunnerMessage message = RunnerMessage.ReadMessageFromStream(stdin);
|
---|
60 | if (DateTime.Compare(lastMessageRecieved, message.SendTime) < 0) {
|
---|
61 | OnRunnerMessage(message);
|
---|
62 | lastMessageRecieved = message.SendTime;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | });
|
---|
66 | listener.IsBackground = true;
|
---|
67 | listener.Start();
|
---|
68 | }
|
---|
69 | #endregion
|
---|
70 | }
|
---|
71 | }
|
---|