ChildProcessDataIO Class Reference

This DataIO class is a handy cross-platform way to spawn and talk to a child process. More...

#include <ChildProcessDataIO.h>

Inheritance diagram for ChildProcessDataIO:

Inheritance graph
[legend]
List of all members.

Public Types

enum  { IO_SEEK_SET = 0, IO_SEEK_CUR, IO_SEEK_END, NUM_IO_SEEKS }
 Values to pass in to DataIO::Seek()'s second parameter. More...

Public Member Functions

 ChildProcessDataIO (bool blocking)
 Constructor.
virtual ~ChildProcessDataIO ()
 Destructor.
status_t LaunchChildProcess (int argc, char **argv)
 Launch the child process.
status_t LaunchChildProcess (const char *cmd)
 As above, but the program name and all arguments are specified as a single string.
virtual int32 Read (void *buffer, uint32 size)
 Read data from the child process's stdout stream.
virtual int32 Write (const void *buffer, uint32 size)
 Write data to the child process's stdin stream.
virtual status_t Seek (int64, int)
 Always returns B_ERROR, since you can't seek on a child process!
virtual int64 GetPosition () const
 Always returns -1, since a child process has no position to speak of.
virtual void FlushOutput ()
 Doesn't return until all outgoing have been sent.
virtual void Shutdown ()
 Kills the child process.
virtual const SocketRefGetSelectSocket () const
 Returns a socket that can be select()'d on for notifications of read/write availability.
bool IsChildProcessAvailable () const
 Returns true iff the child process is available (i.e.
void SetKillChildOnClose (bool kcoc)
 Set whether or not we should forcibly kill the child process when this DataIO object is shut down or deleted.
bool GetKillChildOnClose () const
 Returns true iff we will kill our child process when this DataIO is shut down or destroyed.
void SetWaitForChildOnClose (bool wcoc)
 Set whether or not we should block (waiting until the child process is gone) inside this DataIO object's destructor or Shutdown() call.
bool GetWaitForChildOnClose () const
 Returns true iff we will wait for our child process to exit when this DataIO object is shut down or destroyed.
virtual void ChildProcessReadyToRun ()
 Called within the child process, just before the child process's executable image is loaded in.
pid_t GetChildProcessID () const
 Returns the process ID of the child process.
status_t KillChildProcess ()
 Tries to forcibly kill the child process immediately.
virtual uint64 GetOutputStallLimit () const
 Returns the max number of microseconds to allow for an output stall, before presuming that the I/O is hosed.
virtual status_t GetReadByteTimeStamp (int32 whichByte, uint64 &retStamp) const
 Optional interface for returning information on when a given byte returned by the previous Read() call was received.
virtual bool HasBufferedOutput () const
 Optional: If your DataIO subclass is holding buffered data that it wants to output as soon as possible but hasn't been able to yet, then override this method to return true, and that will cause FlushBufferedOutput() to be called ASAP.
virtual void WriteBufferedOutput ()
 Optional: If this DataIO is holding any buffered output data, this method should be implemented to Write() as much of that data as possible.
uint32 WriteFully (const void *buffer, uint32 size)
 Convenience method: Calls Write() in a loop until the entire buffer is written, or until an error occurs.
uint32 ReadFully (void *buffer, uint32 size)
 Convenience method: Calls Read() in a loop until the entire buffer is written, or until an error occurs.
virtual int64 GetLength ()
 Convenience method: Determines the length of this DataIO stream by Seek()'ing to the end of the stream, recording the current seek position, and then Seek()'ing back to the previous position in the stream.
void IncrementRefCount () const
 Increments the counter and returns true iff the new value is zero.
bool DecrementRefCount () const
 Decrements the counter and returns true iff the new value is zero.
void SetManager (AbstractObjectManager *manager)
 Sets the recycle-pointer for this object.
AbstractObjectManagerGetManager () const
 Returns this object's current recyler pointer.
uint32 GetRefCount () const
 Returns this object's current reference count.

Detailed Description

This DataIO class is a handy cross-platform way to spawn and talk to a child process.

Any data that the child process prints to stdout can be read from this object, and any data that is written to this object will be send to the child process's stdin. Note that this class is currently only guaranteed to work under Windows, MacOS/X, BeOS, and Linux.

Definition at line 19 of file ChildProcessDataIO.h.


Member Enumeration Documentation

anonymous enum [inherited]

Values to pass in to DataIO::Seek()'s second parameter.

Enumerator:
IO_SEEK_SET  Tells Seek that its value specifies bytes-after-beginning-of-stream.
IO_SEEK_CUR  Tells Seek that its value specifies bytes-after-current-stream-position.
IO_SEEK_END  Tells Seek that its value specifies bytes-after-end-of-stream (you'll usually specify a non-positive seek value with this).
NUM_IO_SEEKS  A guard value.

Definition at line 15 of file DataIO.h.


Constructor & Destructor Documentation

ChildProcessDataIO::ChildProcessDataIO ( bool  blocking  ) 

Constructor.

Parameters:
blocking If true, I/O will be blocking; else non-blocking.
Note:
that you will need to call LaunchChildProcess() to actually start the child process going.


Member Function Documentation

status_t ChildProcessDataIO::LaunchChildProcess ( int  argc,
char **  argv 
) [inline]

Launch the child process.

Note that this method should only be called once!

Parameters:
argc The argc variable to be passed to the child process
argv The argv variable to be passed to the child process
Returns:
B_NO_ERROR on success, or B_ERROR if the launch failed.

Definition at line 36 of file ChildProcessDataIO.h.

status_t ChildProcessDataIO::LaunchChildProcess ( const char *  cmd  )  [inline]

As above, but the program name and all arguments are specified as a single string.

Parameters:
cmd String to launch the child process with
Returns:
B_NO_ERROR on success, or B_ERROR if the launch failed.

Definition at line 42 of file ChildProcessDataIO.h.

virtual int32 ChildProcessDataIO::Read ( void *  buffer,
uint32  size 
) [virtual]

Read data from the child process's stdout stream.

Parameters:
buffer The read bytes will be placed here
size Maximum number of bytes that may be placed into (buffer).
Returns:
The number of bytes placed into (buffer), or a negative value if there was an error.

Implements DataIO.

virtual int32 ChildProcessDataIO::Write ( const void *  buffer,
uint32  size 
) [virtual]

Write data to the child process's stdin stream.

Parameters:
buffer The bytes to write to the child process's stdin.
size Maximum number of bytes to read from (buffer) and written to the child process's stdin.
Returns:
The number of bytes written, or a negative value if there was an error.

Implements DataIO.

virtual const SocketRef& ChildProcessDataIO::GetSelectSocket (  )  const [virtual]

Returns a socket that can be select()'d on for notifications of read/write availability.

Even works under Windows (in non-blocking mode, anyway), despite Microsoft's best efforts to make such a thing impossible :^P Note that you should only use this socket with select(); to read or write to/from the child process, call Read() and Write() instead.

Implements DataIO.

bool ChildProcessDataIO::IsChildProcessAvailable (  )  const

Returns true iff the child process is available (i.e.

if startup succeeded).

void ChildProcessDataIO::SetKillChildOnClose ( bool  kcoc  )  [inline]

Set whether or not we should forcibly kill the child process when this DataIO object is shut down or deleted.

Default value is true.

Definition at line 84 of file ChildProcessDataIO.h.

bool ChildProcessDataIO::GetKillChildOnClose (  )  const [inline]

Returns true iff we will kill our child process when this DataIO is shut down or destroyed.

Default value is true.

Definition at line 89 of file ChildProcessDataIO.h.

void ChildProcessDataIO::SetWaitForChildOnClose ( bool  wcoc  )  [inline]

Set whether or not we should block (waiting until the child process is gone) inside this DataIO object's destructor or Shutdown() call.

Default value is true. Note that this flag will be ignored if the kill-child-on-close flag is set to true.

Definition at line 96 of file ChildProcessDataIO.h.

bool ChildProcessDataIO::GetWaitForChildOnClose (  )  const [inline]

Returns true iff we will wait for our child process to exit when this DataIO object is shut down or destroyed.

Default value is true. Note that this flag will be ignored if the kill-child-on-close flag is set to true.

Definition at line 103 of file ChildProcessDataIO.h.

virtual void ChildProcessDataIO::ChildProcessReadyToRun (  )  [virtual]

Called within the child process, just before the child process's executable image is loaded in.

Default implementation is a no-op.

Note:
This method is not called when running under Windows!

pid_t ChildProcessDataIO::GetChildProcessID (  )  const [inline]

Returns the process ID of the child process.

Not available under Windows.

Definition at line 115 of file ChildProcessDataIO.h.

status_t ChildProcessDataIO::KillChildProcess (  ) 

Tries to forcibly kill the child process immediately.

Returns:
B_NO_ERROR on success, or B_ERROR on failure.

virtual uint64 DataIO::GetOutputStallLimit (  )  const [inline, virtual, inherited]

Returns the max number of microseconds to allow for an output stall, before presuming that the I/O is hosed.

Default implementation returns MUSCLE_TIME_NEVER, aka no limit.

Reimplemented in FailoverDataIO, MultiDataIO, PacketizedDataIO, TCPSocketDataIO, and XorDataIO.

Definition at line 72 of file DataIO.h.

Referenced by MultiDataIO::GetOutputStallLimit(), and FailoverDataIO::GetOutputStallLimit().

virtual status_t DataIO::GetReadByteTimeStamp ( int32  whichByte,
uint64 &  retStamp 
) const [inline, virtual, inherited]

Optional interface for returning information on when a given byte returned by the previous Read() call was received.

Not implemented by default, and not implemented by any of the standard MUSCLE DataIO subclasses. (Used by an LCS dataIO class that needs precision timing)

Parameters:
whichByte Index of the byte in the previously returned read-buffer that you are interested in.
retStamp On success, this value is set to the timestamp of the byte.
Returns:
B_NO_ERROR if a timestamp was written into (retStamp), otherwise B_ERROR. Default implementation always returns B_ERROR.

Reimplemented in FailoverDataIO, MultiDataIO, and XorDataIO.

Definition at line 114 of file DataIO.h.

Referenced by MultiDataIO::GetReadByteTimeStamp(), and FailoverDataIO::GetReadByteTimeStamp().

virtual bool DataIO::HasBufferedOutput (  )  const [inline, virtual, inherited]

Optional: If your DataIO subclass is holding buffered data that it wants to output as soon as possible but hasn't been able to yet, then override this method to return true, and that will cause FlushBufferedOutput() to be called ASAP.

Default implementation always returns false.

Reimplemented in FailoverDataIO, MultiDataIO, PacketizedDataIO, and XorDataIO.

Definition at line 123 of file DataIO.h.

Referenced by FailoverDataIO::HasBufferedOutput().

virtual void DataIO::WriteBufferedOutput (  )  [inline, virtual, inherited]

Optional: If this DataIO is holding any buffered output data, this method should be implemented to Write() as much of that data as possible.

Default implementation is a no-op.

Reimplemented in FailoverDataIO, MultiDataIO, PacketizedDataIO, and XorDataIO.

Definition at line 130 of file DataIO.h.

Referenced by FailoverDataIO::WriteBufferedOutput().

uint32 DataIO::WriteFully ( const void *  buffer,
uint32  size 
) [inherited]

Convenience method: Calls Write() in a loop until the entire buffer is written, or until an error occurs.

This method should only be used in conjunction with blocking I/O; it will not work reliably with non-blocking I/O.

Parameters:
buffer Pointer to the first byte of the buffer to write data from.
size Number of bytes to write
Returns:
The number of bytes that were actually written. On success, This will be equal to (size). On failure, it will be a smaller value.

uint32 DataIO::ReadFully ( void *  buffer,
uint32  size 
) [inherited]

Convenience method: Calls Read() in a loop until the entire buffer is written, or until an error occurs.

This method should only be used in conjunction with blocking I/O; it will not work reliably with non-blocking I/O.

Parameters:
buffer Pointer to the first byte of the buffer to place the read data into.
size Number of bytes to read
Returns:
The number of bytes that were actually read. On success, This will be equal to (size). On failure, it will be a smaller value.

virtual int64 DataIO::GetLength (  )  [virtual, inherited]

Convenience method: Determines the length of this DataIO stream by Seek()'ing to the end of the stream, recording the current seek position, and then Seek()'ing back to the previous position in the stream.

Of course this only works with DataIOs that support seeking and have a fixed length.

Returns:
The total length of this DataIO in bytes, or -1 on error.

Reimplemented in PacketizedDataIO.

void RefCountable::IncrementRefCount (  )  const [inline, inherited]

Increments the counter and returns true iff the new value is zero.

Thread safe.

Definition at line 32 of file RefCount.h.

References AtomicCounter::AtomicIncrement().

bool RefCountable::DecrementRefCount (  )  const [inline, inherited]

Decrements the counter and returns true iff the new value is zero.

Thread safe.

Definition at line 35 of file RefCount.h.

References AtomicCounter::AtomicDecrement().

void RefCountable::SetManager ( AbstractObjectManager manager  )  [inline, inherited]

Sets the recycle-pointer for this object.

If set to non-NULL, this pointer is used by the ObjectPool class to recycle this object when it is no longer in use, so as to avoid the overhead of having to delete it and re-create it later on. The RefCountable class itself does nothing with this pointer. Default value is NULL.

Parameters:
manager Pointer to the new manager object to use, or NULL to use no manager.

Definition at line 44 of file RefCount.h.

uint32 RefCountable::GetRefCount (  )  const [inline, inherited]

Returns this object's current reference count.

Note that the value returned by this method is volatile in multithreaded environments, so it may already be wrong by the time it is returned. Be careful!

Definition at line 54 of file RefCount.h.

References AtomicCounter::GetCount().


The documentation for this class was generated from the following file:
Generated on Thu Jun 5 17:48:01 2008 for MUSCLE by  doxygen 1.5.1