hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sample.cpp
Go to the documentation of this file.
1 
14 #include <iostream>
15 #include <sstream>
16 
17 #include "Process.hpp"
18 #include "ProcExec.hpp"
19 #include "CommandExecuter.hpp"
20 
21 void printTitle(const std::string& title)
22 {
23  std::cout << std::string(title.length(), '=') << std::endl << title << std::endl << std::string(title.length(), '=') << std::endl;
24 }
25 
27 {
28  printTitle("Execute command Sort");
29 
30  std::string command = "sort";
31  HCE::Process::Args args;
32  std::string initialDirectory = "/tmp";
33  std::stringstream inMsg("def\nabc\n123\n");
34  std::stringstream outMsg, errMsg;
35  HCE::Process::Env env = {{"key1", "value1"}};
36 
37  try
38  {
39  HCE::ProcessHandle ph = HCE::Process::launch(command, args, initialDirectory, inMsg, outMsg, errMsg, env);
40 
41  std::cout << "id: " << ph.id() << std::endl;
42  std::cout << "wait: " << ph.wait() << std::endl;
43  std::cout << "cout: " << outMsg.str() << std::endl;
44  std::cout << "cerr: " << errMsg.str() << std::endl;
45 
46  }
47  catch(std::exception& e)
48  {
49  std::cerr << "Exception: " << e.what() << std::endl;
50  }
51 }
52 
54 {
55  printTitle("Execute command Reverse Sort");
56 
57  std::string command = "sort";
58  HCE::Process::Args args = {"-r"};
59  std::stringstream inMsg("def\nabc\n123\n");
60  std::stringstream outMsg, errMsg;
61 
62  try
63  {
64  HCE::ProcessHandle ph = HCE::Process::launch(command, args, inMsg, outMsg, errMsg);
65 
66  std::cout << "id: " << ph.id() << std::endl;
67  std::cout << "wait: " << ph.wait() << std::endl;
68  std::cout << "cout: " << outMsg.str() << std::endl;
69  std::cout << "cerr: " << errMsg.str() << std::endl;
70 
71  }
72  catch(std::exception& e)
73  {
74  std::cerr << "Exception: " << e.what() << std::endl;
75  }
76 }
77 
79 {
80  printTitle("Request Terminate");
81 
82  std::string command = "sleep";
83  HCE::Process::Args args = {"5"};
84  std::stringstream inMsg, outMsg, errMsg;
85  try
86  {
87  HCE::ProcessHandle ph = HCE::Process::launch(command, args, inMsg, outMsg, errMsg);
88 
89 // HCE::Process::requestTermination(ph);
91 
92  std::cout << "id: " << ph.id() << std::endl;
93  std::cout << "wait: " << ph.wait() << std::endl;
94  std::cout << "cout: " << outMsg.str() << std::endl;
95  std::cout << "cerr: " << errMsg.str() << std::endl;
96  }
97  catch(std::exception& e)
98  {
99  std::cerr << "Exception: " << e.what() << std::endl;
100  }
101 }
102 
104 {
105  printTitle("Get Current Process ID");
106 
107  std::string command = "ps";
108  HCE::Process::Args args = {"auxf"};
109  try
110  {
111  HCE::ProcessHandle ph = HCE::Process::launch(command, args);
112 
114 
115  std::cout << "Current process ID: " << HCE::Process::id() << std::endl;
116  std::cout << "wait: " << ph.wait() << std::endl;
117  }
118  catch(std::exception& e)
119  {
120  std::cerr << "Exception: " << e.what() << std::endl;
121  }
122 }
123 
124 void sampleProcExec(void)
125 {
126  printTitle("Use ProcExec");
127 
128  std::string command = "sort";
129  HCE::Process::Args args = {"-r"};
130  std::stringstream inMsg("def\nabc\n123\n");
131 
132  std::stringstream outMsg, errMsg;
133  HCE::ProcExec procExec(command, args);
134  std::cout << "Exec: " << std::boolalpha << procExec.exec(inMsg, outMsg, errMsg) << std::endl;
135  std::cout << "isError: " << std::boolalpha << procExec.isError() << std::endl;
136  std::cout << "Out: " << outMsg.str() << std::endl;
137  std::cout << "Err: " << errMsg.str() << std::endl;
138 }
139 
141 {
142  printTitle("Use CommandExecutor");
143 
144  std::string command = "sort -r";
145  std::string shell = "bash -c";
146  std::stringstream inMsg("def\nabc\n123\n");
147 
148  HCE::Command cmd(command);
149  cmd.setShell(shell);
150  cmd.setInputStream(inMsg);
151  HCE::CommandExecuter commandExecuter;
152  HCE::CommandResultData commandResultData = commandExecuter.execute(cmd);
153 
154  std::cout << "isError: " << std::boolalpha << commandResultData.getIsError() << std::endl;
155  std::cout << "ExitStatus: " << commandResultData.getExitStatus() << std::endl;
156  std::cout << "Out: " << commandResultData.getOutStream().str() << std::endl;
157  std::cout << "Err: " << commandResultData.getErrStream().str() << std::endl;
158 }
159 
160 int main(void)
161 {
166  sampleProcExec();
168 
169  return 0;
170 }
171