EmbASP-CSharp
DesktopService.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Diagnostics;
5 using System.IO;
6 using System.Text;
7 using System.Threading;
8 
10 {
11  using ICallback = it.unical.mat.embasp.@base.ICallback;
12  using InputProgram = it.unical.mat.embasp.@base.InputProgram;
13  using OptionDescriptor = it.unical.mat.embasp.@base.OptionDescriptor;
14  using Output = it.unical.mat.embasp.@base.Output;
15  using Service = it.unical.mat.embasp.@base.Service;
16 
17  public abstract class DesktopService : Service
18  {
19  protected internal string exe_path;
20  protected internal string load_from_STDIN_option;
21 
22  public DesktopService(string exe_path) => this.exe_path = exe_path;
23 
24  public virtual string ExePath { get => exe_path; set => this.exe_path = value; }
25 
26  protected internal abstract Output GetOutput(string output, string error);
27 
28  public void StartAsync(ICallback callback, IList<InputProgram> programs, IList<OptionDescriptor> options)
29  {
30  new Thread(() =>
31  {
32  callback.Callback(StartSync(programs, options));
33  }).Start();
34  }
35 
36  public virtual Output StartSync(IList<InputProgram> programs, IList<OptionDescriptor> options)
37  {
38  string option = "";
39  foreach (OptionDescriptor o in options)
40  {
41  if (o != null)
42  {
43  option += o.Options;
44  option += o.Separator;
45  }
46  else
47  Console.Error.WriteLine("Warning : wrong " + typeof(OptionDescriptor).FullName);
48  }
49 
50  string files_paths = "";
51  string final_program = "";
52 
53  foreach (InputProgram p in programs)
54  {
55  if (p != null)
56  {
57  final_program += p.Programs;
58  foreach (String program_file in p.FilesPaths)
59  {
60  FileAttributes f = File.GetAttributes(@program_file);
61  if (File.Exists(program_file) && !f.HasFlag(FileAttributes.Directory))
62  {
63  files_paths += program_file;
64  files_paths += " ";
65  }
66  else
67  Console.Error.WriteLine("Warning : the file " + Path.GetFullPath(@program_file) + " does not exists.");
68  }
69  }
70  else
71  Console.Error.WriteLine("Warning : wrong " + typeof(InputProgram).FullName);
72  }
73 
74  string solverOutput = "EMPTY_OUTPUT";
75  string solverError = "EMPTY_ERROR";
76 
77  try
78  {
79  var watch = System.Diagnostics.Stopwatch.StartNew();
80 
81  StringBuilder stringBuffer = new StringBuilder();
82  StringBuilder options_string = new StringBuilder();
83  if (exe_path is null)
84  {
85  return new Output("", "Error: executable not found");
86  }
87 
88  Process solver_process = new Process();
89  stringBuffer.Append(exe_path).Append(" ").Append(option).Append(" ").Append(files_paths);
90  solver_process.StartInfo.FileName = exe_path;
91  options_string.Append(option).Append(" ").Append(files_paths);
92 
93  if (final_program.Length > 0)
94  {
95  options_string.Append(this.load_from_STDIN_option);
96  stringBuffer.Append(this.load_from_STDIN_option);
97  }
98 
99  Console.Error.WriteLine(stringBuffer.ToString());
100  solver_process.EnableRaisingEvents = true;
101  solver_process.StartInfo.Arguments = @options_string.ToString();
102  solver_process.StartInfo.UseShellExecute = false;
103  solver_process.StartInfo.CreateNoWindow = true;
104  solver_process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
105  solver_process.StartInfo.RedirectStandardInput = true;
106  solver_process.StartInfo.RedirectStandardOutput = true;
107  solver_process.StartInfo.RedirectStandardError = true;
108  solver_process.Start();
109 
110  if (final_program.Length > 0)
111  {
112  StreamWriter writer = solver_process.StandardInput;
113  writer.WriteLine(final_program);
114  if (writer != null)
115  {
116  writer.Close();
117  }
118  }
119 
120  solverOutput = solver_process.StandardOutput.ReadToEnd().ToString();
121  solverError = solver_process.StandardError.ReadToEnd().ToString();
122 
123  solver_process.WaitForExit();
124  solver_process.Close();
125 
126  watch.Stop();
127 
128  Console.Error.WriteLine("Total time : " + watch.ElapsedMilliseconds);
129 
130  return GetOutput(solverOutput.ToString(), solverError.ToString());
131  }
132  catch (Win32Exception e2)
133  {
134  Console.Error.WriteLine(e2.ToString());
135  Console.Error.Write(e2.StackTrace);
136  }
137 
138  return GetOutput("", "");
139 
140  }
141 
142 
143  }
144 }
it.unical.mat.embasp.platforms.desktop.DesktopService
Definition: DesktopService.cs:17
base.ICallback
Definition: Callback.cs:3
it.unical.mat.embasp.platforms.desktop
Definition: DesktopHandler.cs:3
base
Definition: Callback.cs:1
it.unical.mat.embasp
Definition: AnswerSet.cs:5
base.Service
Definition: Service.cs:5
it.unical
Definition: AnswerSet.cs:5
it
Definition: AnswerSet.cs:5
base.OptionDescriptor
Definition: OptionDescriptor.cs:3
base.Output
Definition: Output.cs:5
base.InputProgram
Definition: InputProgram.cs:7
it.unical.mat
Definition: AnswerSet.cs:5