hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CommandExecutor.cpp
Go to the documentation of this file.
1 #include <string.h>
2 #include <fstream>
3 
4 #include "CommandExecutor.hpp"
5 
6 namespace HCE
7 {
8 //-----------------------------------------------------------------------------
9 Poco::SharedPtr<CommandResultData> CommandExecutor::execute(const Command& command, Process::CallbackFunction fn, unsigned int allowedTime)
10 {
11  Poco::SharedPtr<CommandResultData> pCommandResultData = new CommandResultData();
12  if (!pCommandResultData.isNull())
13  {
14  try
15  {
16  Poco::Process::Env env;
17  const Command::Env& refEnv = command.getEnvironments();
18  for (size_t i=0;i<refEnv.size();++i)
19  env[refEnv[i].first] = refEnv[i].second;
20 
21  HCE::StarterExecutor starterExecutor(command.getInitialDirectory(), command.getShellName());
22  pid_t pid = starterExecutor.execute(command.getCommandLine(), command.getInputStream(), env);
23 
24  if (fn)
25  fn(pid);
26 
27  unsigned int exitStatus = starterExecutor.wait(allowedTime);
28 
29  pCommandResultData->setExitStatus(exitStatus);
30  pCommandResultData->setProcessId(pid);
31  pCommandResultData->setOutStream(starterExecutor.getOutStream());
32  pCommandResultData->setErrStream(starterExecutor.getErrStream());
33  statusExecution = starterExecutor.getStatusExecution();
34  }
35  catch(Poco::Exception& e)
36  {
37  pCommandResultData->setIsError(true);
38  pCommandResultData->getErrStream() << " " << e.displayText();
39  }
40  catch(std::exception& e)
41  {
42  pCommandResultData->setIsError(true);
43  pCommandResultData->getErrStream() << " " << e.what();
44  }
45  }
46  return pCommandResultData;
47 }
48 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
50 } // end namespace HCE