1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Diagnostics;
|
---|
4 | using System.IO;
|
---|
5 | using System.Text;
|
---|
6 | using System.Xml;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Communication.Data {
|
---|
11 | public class ProcessData : ItemBase, IDataStream {
|
---|
12 | private LocalProcessDriverConfiguration config;
|
---|
13 | public IDriverConfiguration Configuration {
|
---|
14 | get { return config; }
|
---|
15 | set { config = (LocalProcessDriverConfiguration)value; }
|
---|
16 | }
|
---|
17 | private Process process;
|
---|
18 | public Process Process {
|
---|
19 | get { return process; }
|
---|
20 | set { process = value; }
|
---|
21 | }
|
---|
22 |
|
---|
23 | public ProcessData() {
|
---|
24 | process = null;
|
---|
25 | config = null;
|
---|
26 | }
|
---|
27 |
|
---|
28 | // A process cannot be cloned
|
---|
29 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
30 | ProcessData clone = new ProcessData();
|
---|
31 | clonedObjects.Add(Guid, clone);
|
---|
32 | clone.process = process;
|
---|
33 | clone.config = (LocalProcessDriverConfiguration)Auxiliary.Clone(config, clonedObjects);
|
---|
34 | return clone;
|
---|
35 | }
|
---|
36 |
|
---|
37 | #region persistence
|
---|
38 | // A process cannot be persisted
|
---|
39 | // but information can be persisted that will allow it to be recreated
|
---|
40 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
41 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
42 | XmlNode configNode = PersistenceManager.Persist("Configuration", config, document, persistedObjects);
|
---|
43 | node.AppendChild(configNode);
|
---|
44 | return node;
|
---|
45 | }
|
---|
46 |
|
---|
47 | // A process cannot be persisted
|
---|
48 | // but information can be persisted that will allow it to be recreated
|
---|
49 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
50 | base.Populate(node, restoredObjects);
|
---|
51 | Configuration = (LocalProcessDriverConfiguration)PersistenceManager.Restore(node.SelectSingleNode("Configuration"), restoredObjects);
|
---|
52 | StartProcess();
|
---|
53 | }
|
---|
54 | #endregion
|
---|
55 |
|
---|
56 | private void StartProcess() {
|
---|
57 | process = new Process();
|
---|
58 | process.StartInfo.FileName = config.ExecutablePath.Data;
|
---|
59 | process.StartInfo.Arguments = config.Arguments.Data;
|
---|
60 | process.StartInfo.UseShellExecute = false;
|
---|
61 | process.StartInfo.RedirectStandardInput = true;
|
---|
62 | process.StartInfo.RedirectStandardError = true;
|
---|
63 | process.StartInfo.RedirectStandardOutput = true;
|
---|
64 | process.Start();
|
---|
65 | }
|
---|
66 |
|
---|
67 | public void Initialize(IDriverConfiguration configuration) {
|
---|
68 | Configuration = configuration;
|
---|
69 | StartProcess();
|
---|
70 | }
|
---|
71 |
|
---|
72 | public bool Connect() {
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void Close() {
|
---|
77 | if (!process.HasExited) process.Kill();
|
---|
78 | process.Close();
|
---|
79 | process = null;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void Write(string s) {
|
---|
83 | StreamWriter writer = process.StandardInput;
|
---|
84 | writer.WriteLine(s);
|
---|
85 | writer.WriteLine(".");
|
---|
86 | }
|
---|
87 |
|
---|
88 | public string Read() {
|
---|
89 | StreamReader reader = process.StandardOutput;
|
---|
90 | StringBuilder buffer = new StringBuilder();
|
---|
91 | string line = "";
|
---|
92 | do {
|
---|
93 | line = reader.ReadLine();
|
---|
94 | if (line.Equals(".")) break;
|
---|
95 | buffer.AppendLine(line);
|
---|
96 | } while (true);
|
---|
97 | return buffer.ToString();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | } |
---|