hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
StarterExecutor.cpp
Go to the documentation of this file.
1 #include <sys/wait.h>
2 #include <fstream>
3 
4 #include <Poco/Thread.h>
5 #include <Poco/Process.h>
6 #include <Poco/TemporaryFile.h>
7 
8 #include "StarterExecutor.hpp"
9 
10 namespace HCE
11 {
12 //-----------------------------------------------------------------------------
13 StarterExecutor::StarterExecutor(const std::string& homeDir_, const std::string& starterFileName_)
14 : outPipe(), errPipe(), outInputStream(outPipe), errInputStream(errPipe), outStream(), errStream(),
15  starterFileName(""), homeDir(""), inputStreamFileName(""), pid(0), statusExecution(StatusExecution::seUnknown)
16 {
17  setStarterFileName(starterFileName_);
18  setHomeDir(homeDir_);
19 }
20 //-----------------------------------------------------------------------------
22 {
23  Poco::File inputStreamFile(inputStreamFileName);
24  if (inputStreamFile.exists())
25  inputStreamFile.remove();
26 }
27 //-----------------------------------------------------------------------------
28 void StarterExecutor::setStarterFileName(const std::string& starterFileName_)
29 {
30  starterFileName = (starterFileName_.empty())?starter_const::starterFileName:starterFileName_;
31 }
32 //-----------------------------------------------------------------------------
33 void StarterExecutor::setHomeDir(const std::string& homeDir_)
34 {
35  if (homeDir != homeDir_)
36  {
37  homeDir=homeDir_;
38 
39  Poco::File starterFile(getStarterFullName());
40  if (!starterFile.exists())
41  {
42  Poco::File starterPath = homeDir+starter_const::starterPathName;
43  starterPath.createDirectories();
44 
45  std::ofstream ofs(starterFile.path().c_str(), std::fstream::trunc);
47  ofs.close();
48 
49  starterFile.setExecutable(true);
50  }
51  }
52 }
53 //-----------------------------------------------------------------------------
54 std::string StarterExecutor::getStarterFullName(const std::string& homeDir, const std::string& scriptName)
55 {
56  return homeDir+starter_const::starterPathName+"/"+scriptName;
57 }
58 //-----------------------------------------------------------------------------
59 std::string StarterExecutor::getStarterFullName(void) const
60 {
61  return getStarterFullName(homeDir, starterFileName);
62 }
63 //-----------------------------------------------------------------------------
64 pid_t StarterExecutor::execute(const std::string& cmd, const std::string& inputStream, const Poco::Process::Env& env)
65 {
66  inputStreamFileName = Poco::TemporaryFile::tempName();
67 
68  std::fstream fs(inputStreamFileName.c_str(), std::fstream::out|std::fstream::trunc|std::fstream::binary);
69  fs << inputStream;
70  fs.close();
71 
72  Poco::Process::Args args={cmd, inputStreamFileName};
73  Poco::ProcessHandle ph = Poco::Process::launch(getStarterFullName(), args, homeDir, 0, &outPipe, &errPipe, env);
74  pid = ph.id();
75 
76  return pid;
77 }
78 //-----------------------------------------------------------------------------
79 unsigned int StarterExecutor::wait(unsigned int waitTime)
80 {
81  int status = 0;
82 
83  /* getting result data */
84  outInputStream >> outStream.rdbuf();
85  errInputStream >> errStream.rdbuf();
86 
87  /* wait process finish */
88  statusExecution = StatusExecution::seUnknown;
89 
90  waitpid(pid, &status, 0);
91 
92 // if (WIFEXITED(status))
93 // printf("child status %d\n", WEXITSTATUS(status));
94 // else
95 // printf("child signal %d\n", WTERMSIG(status));
96 
97  if (WIFEXITED(status))
98  statusExecution = StatusExecution::seFinished;
99  else
100  statusExecution = StatusExecution::seTerminated;
101 
102  /* getting teil of result data */
103  outInputStream >> outStream.rdbuf();
104  errInputStream >> errStream.rdbuf();
105 
106  /* remove temporary file */
107  Poco::File inputStreamFile(inputStreamFileName);
108  if (inputStreamFile.exists())
109  inputStreamFile.remove();
110 
111  /* return exit status */
112  return static_cast<unsigned int>(WEXITSTATUS(status));
113 }
114 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
116 bool StarterExecutor::isExistShellScript(const std::string& homeDir, const std::string& scriptName)
117 {
118  Poco::File scriptFile(getStarterFullName(homeDir, scriptName));
119  return scriptFile.exists();
120 }
121 //-----------------------------------------------------------------------------
122 bool StarterExecutor::canExecuteShellScript(const std::string& homeDir, const std::string& scriptName)
123 {
124  Poco::File scriptFile(getStarterFullName(homeDir, scriptName));
125  return scriptFile.canExecute();
126 }
127 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
129 } // end namespace HCE