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