Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

ExecutableCommPort Class Reference

Run a specified executable, with the comm port connected to its stdin and stdout. More...

#include <ExecutableCommPort.h>

Inheritance diagram for ExecutableCommPort:

Detailed Description

Run a specified executable, with the comm port connected to its stdin and stdout.

This can be handy for testing with a device simulator, or if you want to interface with a device as an external process. It's more efficient to run your driver as an DeviceDriver subclass than to spawn an external process however.

If you need to keep your external program running across instances of the simulator (or just want to launch it externally), you'll need to use file system fifos (see mkfifo command), probably separate ones for reading and writing with a RedirectionCommPort to combine them (unless your platform supports bidirectional pipes... most don't)

Definition at line 18 of file ExecutableCommPort.h.

List of all members.

Public Member Functions

 ExecutableCommPort (const std::string &name)
virtual ~ExecutableCommPort ()
 destructor, checks that child process is no longer running, kills it if it is (after some delay)
virtual std::string getClassName () const
 Returns the name of the class (aka its type).
virtual streambufgetReadStreambuf ()
 Returns a std::basic_streambuf, which is expected to implement the actual work.
virtual streambufgetWriteStreambuf ()
 Returns a std::basic_streambuf, which is expected to implement the actual work.
virtual bool isWriteable ()
 Allows you to check whether the reference from getWriteStreambuf() is currently functional (if checking is supported!).
virtual bool isReadable ()
 Allows you to check whether the reference from getReadStreambuf() is currently functional (if checking is supported!).
virtual bool open ()
 launches the executable, connecting its stdin and stdout to this comm port
virtual bool close ()
 sends kill signal to the child process
virtual void plistValueChanged (const plist::PrimitiveBase &pl)
 if command is modified, close current connection (if running) and launch new one

Public Attributes

plist::Primitive< std::string > command
 shell command
plist::Primitive< std::string > shell
 shell name

Protected Types

enum  { READPIPE = 0, WRITEPIPE = 1 }

Protected Member Functions

bool waitChild (unsigned int t, unsigned int p)
 waits for child process to exit for t milliseconds, polling status every p millseconds, returns true if no longer running
bool isChildRunning () const
 returns true if child is still running

Protected Attributes

pid_t child
 process ID of the child (0 if not launched, -1 if error)
basic_netbuf< std::ios::char_type > rbuf
 reads from child process
basic_netbuf< std::ios::char_type > wbuf
 writes to child process
unsigned int openedCnt
 reference count of the number of times we've been opened (i.e. pending close()s)

Static Protected Attributes

static const std::string autoRegisterExecutableCommPort = CommPort::getRegistry().registerType<ExecutableCommPort>("ExecutableCommPort")
 holds the class name, set via registration with the CommPort registry

Member Enumeration Documentation

anonymous enum [protected]
Enumerator:
READPIPE 

pipe() system call returns read end on the first entry, lets make it symbolic so it's more clear

WRITEPIPE 

pipe() system call returns write end on the second entry, lets make it symbolic so it's more clear

Definition at line 59 of file ExecutableCommPort.h.


Constructor & Destructor Documentation

ExecutableCommPort ( const std::string &  name  )  [explicit]

Definition at line 20 of file ExecutableCommPort.h.

~ExecutableCommPort (  )  [virtual]

destructor, checks that child process is no longer running, kills it if it is (after some delay)

Definition at line 11 of file ExecutableCommPort.cc.


Member Function Documentation

bool close (  )  [virtual]

sends kill signal to the child process

Implements CommPort.

Definition at line 100 of file ExecutableCommPort.cc.

Referenced by open(), and plistValueChanged().

virtual std::string getClassName (  )  const [virtual]

Returns the name of the class (aka its type).

Suggested implementation is to declare a static string member, set it to the result of calling the registry's registerType, and then return that member here

Implements CommPort.

Definition at line 32 of file ExecutableCommPort.h.

Referenced by plistValueChanged().

virtual streambuf& getReadStreambuf (  )  [virtual]

Returns a std::basic_streambuf, which is expected to implement the actual work.

You can pass this to an istream to use the nice C++ style input and output, or you can call the streambuf functions directly. However, if you're going the latter route, probably easier to just call CommPort's own read() and write().

Depending on implementation, the streambuf this returns might be a different instance than getWriteStreambuf. If they are the same instance, then you could use an iostream instead of separate istream and ostream.

Implements CommPort.

Definition at line 34 of file ExecutableCommPort.h.

virtual streambuf& getWriteStreambuf (  )  [virtual]

Returns a std::basic_streambuf, which is expected to implement the actual work.

You can pass this to an ostream to use the nice C++ style input and output, or you can call the streambuf functions directly. However, if you're going the latter route, probably easier to just call CommPort's own read() and write().

Depending on implementation, the streambuf this returns might be a different instance than getReadStreambuf. If they are the same instance, then you could use an iostream instead of separate istream and ostream.

Implements CommPort.

Definition at line 35 of file ExecutableCommPort.h.

bool isChildRunning (  )  const [protected]

returns true if child is still running

Definition at line 138 of file ExecutableCommPort.cc.

Referenced by waitChild(), and ~ExecutableCommPort().

virtual bool isReadable (  )  [virtual]

Allows you to check whether the reference from getReadStreambuf() is currently functional (if checking is supported!).

For streambufs which don't have a way to check this, always returns true.

Reimplemented from CommPort.

Definition at line 37 of file ExecutableCommPort.h.

virtual bool isWriteable (  )  [virtual]

Allows you to check whether the reference from getWriteStreambuf() is currently functional (if checking is supported!).

For streambufs which don't have a way to check this, always returns true.

Reimplemented from CommPort.

Definition at line 36 of file ExecutableCommPort.h.

bool open (  )  [virtual]

launches the executable, connecting its stdin and stdout to this comm port

Implements CommPort.

Definition at line 36 of file ExecutableCommPort.cc.

Referenced by plistValueChanged().

void plistValueChanged ( const plist::PrimitiveBase pl  )  [virtual]

if command is modified, close current connection (if running) and launch new one

Implements PrimitiveListener.

Definition at line 113 of file ExecutableCommPort.cc.

bool waitChild ( unsigned int  t,
unsigned int  p 
) [protected]

waits for child process to exit for t milliseconds, polling status every p millseconds, returns true if no longer running

Definition at line 127 of file ExecutableCommPort.cc.

Referenced by ~ExecutableCommPort().


Member Data Documentation

const std::string autoRegisterExecutableCommPort = CommPort::getRegistry().registerType<ExecutableCommPort>("ExecutableCommPort") [static, protected]

holds the class name, set via registration with the CommPort registry

Definition at line 71 of file ExecutableCommPort.h.

Referenced by getClassName().

pid_t child [protected]

process ID of the child (0 if not launched, -1 if error)

Definition at line 52 of file ExecutableCommPort.h.

Referenced by isChildRunning(), open(), and ~ExecutableCommPort().

plist::Primitive<std::string> command

shell command

Definition at line 48 of file ExecutableCommPort.h.

Referenced by close(), ExecutableCommPort(), open(), plistValueChanged(), and ~ExecutableCommPort().

unsigned int openedCnt [protected]

reference count of the number of times we've been opened (i.e. pending close()s)

Definition at line 57 of file ExecutableCommPort.h.

Referenced by close(), open(), plistValueChanged(), and ~ExecutableCommPort().

basic_netbuf<std::ios::char_type> rbuf [protected]

reads from child process

Definition at line 54 of file ExecutableCommPort.h.

Referenced by close(), getReadStreambuf(), isReadable(), and open().

plist::Primitive<std::string> shell

shell name

Definition at line 49 of file ExecutableCommPort.h.

Referenced by close(), ExecutableCommPort(), open(), plistValueChanged(), and ~ExecutableCommPort().

basic_netbuf<std::ios::char_type> wbuf [protected]

writes to child process

Definition at line 55 of file ExecutableCommPort.h.

Referenced by close(), getWriteStreambuf(), isWriteable(), and open().


The documentation for this class was generated from the following files:

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:41 2016 by Doxygen 1.6.3