123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- // \file child.hpp
- //
- // Copyright (C) 2014 MicroNeil Research Corporation.
- //
- // This program is part of the MicroNeil Research Open Library Project. For
- // more information go to http://www.microneil.com/OpenLibrary/index.html
- //
- // This program is free software; you can redistribute it and/or modify it
- // under the terms of the GNU General Public License as published by the
- // Free Software Foundation; either version 2 of the License, or (at your
- // option) any later version.
- //
- // This program is distributed in the hope that it will be useful, but WITHOUT
- // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- // more details.
- //
- // You should have received a copy of the GNU General Public License along with
- // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- // Place, Suite 330, Boston, MA 02111-1307 USA
- //==============================================================================
-
- /*
- \brief The child module provides classes to spawn and communicate
- with child processes.
- */
-
- #ifndef CHILD_HPP
- #define CHILD_HPP
-
- #ifdef _WIN32
- #include <windows.h>
- #endif
-
- #include <cstdint>
- #include <streambuf>
- #include <iostream>
- #include <string>
- #include <vector>
-
- namespace CodeDweller {
-
- /**
- \namespace CodeDweller
-
- The CodeDweller namespace contains components providing high-level
- functionality for applications.
-
- */
-
- /** Class that abstracts the communication with a child process.
-
- This class provides functionality to create a child process,
- communicate with the child process via streams and signals, and
- obtain the exit code of the child process.
-
- */
-
- class ChildStream : public std::iostream {
-
- private:
-
- /// Streambuf class for reading from the standard output and
- /// writing to the standard input of the child process.
- class ChildStreambuf : public std::streambuf {
-
- friend class ChildStream;
-
- public:
-
- /// Constructor.
- //
- // \param[in] bufSize is the size in bytes of the input
- // buffer and output buffer.
- //
- explicit ChildStreambuf(std::size_t bufSize = 4096);
-
- /// Thread start function to send data to the child.
- void sendToChild();
-
- #ifdef _WIN32
-
- /// Set the handle to read the standard output of the child
- /// process.
- //
- // \param[in] inHandle is the input handle for the standard
- // output of the child process.
- //
- void setInputHandle(HANDLE inHandle);
-
- /// Set the handle to write the standard input of the child
- /// process.
- //
- // \param[in] outHandle is the output handle for the standard
- // input of the child process.
- //
- void setOutputHandle(HANDLE outHandle);
-
- #else
-
- /// Set the file descriptor to read the standard output of the
- /// child process.
- //
- // \param[in] inFd is the input file descriptor for the standard
- // output of the child process.
- //
- void setInputFileDescriptor(int inFd);
-
- /// Set the file descriptor to write the standard input of the
- /// child process.
- //
- // \param[in] outFd is the output file descriptor for the
- // standard input of the child process.
- //
- void setOutputFileDescriptor(int outFd);
-
- #endif
-
- private:
-
- /** Return the number of bytes that can be read without
- blocking.
-
- This method checks if any input is available from the pipe,
- and returns the number of bytes in the input buffer plus 1.
- Reading that number of bytes will not block. Reading a
- larger number of bytes might block.
-
- \returns minimum number of bytes that can be read without
- blocking.
-
- */
- size_t numBytesAvailable() const;
-
- /// Override streambuf::underflow().
- int_type underflow();
-
- /// Flush the output buffer.
- void flushBuffer();
-
- /// Override streambuf::overflow().
- int_type overflow(int_type ch);
-
- /// Override streambuf::sync().
- int sync();
-
- /// Input and output handles.
- #ifdef _WIN32
- HANDLE inputHandle;
- HANDLE outputHandle;
- #else
- int inputFileDescriptor;
- int outputFileDescriptor;
- #endif
-
- /// Size of buffers.
- std::size_t bufferSize;
-
- /// Read buffer.
- std::vector<char> readBuffer;
-
- /// Write buffer.
- std::vector<char> writeBuffer;
-
- /// Copy constructor not implemented.
- ChildStreambuf(const ChildStreambuf &) = delete;
-
- /// Assignment operator not implemented.
- ChildStreambuf &operator=(const ChildStreambuf &) = delete;
-
- };
-
- /// Stream buffer for reading from the stdout and writing to the
- /// stdin of the child process.
- ChildStreambuf childStreambuf;
-
- public:
-
- /** Constructor for spawning with command-line parameters.
-
- The constructor configures the object, and spawns the child
- process.
-
- \param[in] args contains the child executable file name and
- command-line parameters. args[0] contains the full path of the
- executable, and args[1] thru args[n] are the command-line
- parameters.
-
- \param[in] bufSize is the input and output buffer size of the
- stream used to communicate with the child process.
-
- \throws runtime_error if an error occurs.
-
- */
- ChildStream(std::vector<std::string> const &args, size_t bufSize = 4096);
-
- /** Constructor for spawning without command-line parameters.
-
- The constructor configures the object, and spawns the child
- process.
-
- \param[in] childpath contains the child executable file name.
-
- \param[in] bufSize is the input and output buffer size of the
- stream used to communicate with the child process.
-
- \throws runtime_error if an error occurs.
-
- */
- ChildStream(std::string const &childpath, size_t bufSize = 4096);
-
- /** Constructor.
-
- The constructor configures the I/O buffers, but doesn't spawn
- any child process.
-
- \param[in] bufSize is the input and output buffer size of the
- stream used to communicate with the child process.
-
- */
- ChildStream(size_t bufSize = 4096);
-
- /** Destructor terminates the child process. */
- ~ChildStream();
-
- /** Spawn the child process.
-
- \param[in] args contains the child executable file name and
- command-line parameters. args[0] contains the full path of the
- executable, and args[1] thru args[n] are the command-line
- parameters.
-
- \throws runtime_error if an error occurs.
-
- \throws runtime_error if an error occurs.
-
- */
- void open(std::vector<std::string> const &args);
-
- /** Spawn the child process.
-
- \param[in] childpath contains the child executable file name.
-
- \throws runtime_error if an error occurs.
-
- */
- void open(std::string const &childpath);
-
- /** Get the number of bytes available for input.
-
- @returns number of bytes that can be read without blocking.
-
- */
- size_t numBytesAvailable() const;
-
- /** Check whether the child process is running.
-
- \returns True if the child process is running, false
- otherwise.
-
- */
- bool isRunning() const;
-
- /** Terminite the child process.
-
- \throws runtime_error if an error occurs.
-
- \throws logic_error if the child process is not running.
-
- */
- void close();
-
- /** Check whether the child process has exited.
-
- \returns True if the child process has exited, false
- otherwise.
-
- \throws runtime_error if an error occurs.
-
- \throws logic_error if the child process is not running.
-
- */
- bool isDone();
-
- /** Get the exit value of the child process.
-
- \returns The exit value of the child process if the child
- process has exited.
-
- \throws runtime_error if an error occurs.
-
- \throws logic_error if the child process has not exited.
-
- \throws logic_error if the child process is not running.
-
- */
- int32_t result();
-
- private:
-
- /** Spawn the child process.
-
- \throws runtime_error if an error occurs.
-
- */
- void run();
-
- /// Exit code to use when terminating the child process.
- static const uint32_t terminateExitCode = 0;
-
- /// True if the child process was successfully started.
- bool childStarted;
-
- /// True if the child process has exited.
- bool childExited;
-
- /// Initialize data members.
- void init();
-
- /// Child executable path and command-line parameters.
- std::vector<std::string> cmdArgs;
-
- #ifdef _WIN32
- /// Child's process handle.
- HANDLE childProcess;
-
- /// Child's thread handle.
- HANDLE childThread;
- #else
- /// Child process ID.
- pid_t childPid;
- #endif
-
- /// Exit value of the process.
- int32_t exitCode;
-
- /// True if the exit code has been obtained.
- bool exitCodeObtainedFlag;
-
- /// Return text for the most recent error.
- //
- // \returns Human-readable description of the most recent error.
- //
- static std::string getErrorText();
-
- };
-
- }
-
- #endif // CHILD_HPP
|