Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

/magnus/back_end/general/include/BlackBox.h

Go to the documentation of this file.
00001 /*
00002  *   $Id: BlackBox.h,v 1.2 1997/02/03 22:50:30 alex Exp $
00003  */
00004 
00005 // Copyright (C) 1994 The New York Group Theory Cooperative
00006 // See magnus/doc/COPYRIGHT for the full notice.
00007 
00008 // Contents: Definition of the BlackBox class.
00009 //           A BlackBox is a wrapper for an external binary which communicates
00010 //           exclusively through its standard I/O.
00011 //           The binary must accept some sort of `quit' command from its
00012 //           standard input.
00013 //
00014 // Principal Author: Roger Needham
00015 //
00016 // Status: experimental
00017 //
00018 // Revision History:
00019 
00020 #ifndef _BLACKBOX_H_
00021 #define _BLACKBOX_H_
00022 
00023 
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include <string.h>
00030 
00031 #include <iostream.h>
00032 #include <strstream.h>
00033 #include <fstream.h>
00034 #include <stdiostream.h>
00035 
00036 #include "global.h"
00037 #include "Chars.h"
00038 
00039 extern "C" {
00040   char* tempnam(const char*,const char*); // Lib functions SunOS doesn't declare.
00041 }
00042 
00043 
00044 /*!           A BlackBox is a wrapper for an external binary which communicates
00045            exclusively through its standard I/O.
00046            The binary must accept some sort of `quit' command from its
00047            standard input. 
00048 */
00049 class BlackBox {
00050 
00051 public:
00052 
00053   BlackBox(const Chars& startCommand, const Chars& restartCommand);
00054   /*!< Instantiate a BlackBox by supplying sh commands which start and
00055    restart the executable. The executable is actually invoked by the
00056    start and restart methods. */
00057 
00058   BlackBox(const Chars& startCommand);
00059   /*!< A version of the above where the restart command is the same as the
00060     start command, or undesired. */
00061 
00062   ~BlackBox( );
00063   /*!< Unlink the pipes, thus killing the executable. */
00064 
00065   Bool start(const char* greeting = NULL);
00066   /*!< Invoke the executable with the startCommand argument to the constructor.
00067    This attaches named pipes to the executable's standard i/o.
00068    The arg is a (possibly proper) prefix of the first non-blank line you
00069    expect the binary to write to cout. We might use regexps some day.
00070    This returns TRUE iff it finds the greeting (if not NULL), and all other
00071    initialization was successful.
00072    If you call this a second time, it just returns the status: TRUE == good. */
00073 
00074   Bool restart(const char* greeting = NULL);
00075   /*!< Unlink the pipes which were attached by start or any previous restart,
00076    thus killing the executable. Invoke the executable again with the
00077    restartCommand argument to the constructor, and re-attach the pipes.
00078    The arg is the same as for start.
00079    If you call this before you call start, it fails and returns FALSE. */
00080 
00081   Bool stillRunning( );
00082   /*!< This attempts to determine whether the executable is still running. */
00083   //@rn It makes no real attempt yet.
00084 
00085   //@rn Maybe there's a better way to do this:
00086 
00087   ostream& toCommand( );
00088   /*!< Returns an ostream bound to the binary's standard input. */
00089 
00090   istream& fromCommand( );
00091   /*!< Returns an istream bound to the binary's standard output.*/
00092 
00093 
00094 private:
00095 
00096   // Data members:
00097 
00098   int status;                  //!< 1 means good, 0 means bad.
00099   Bool started_p;              //!< Set to TRUE when start is attempted.
00100 
00101   Chars theStartCmd;           //!< sh command to start executable
00102   Chars theRestartCmd;         //!< sh command to restart executable
00103 
00104   ofstream* streamToCommand;   //!< Bound to command's cin, or NULL
00105   ifstream* streamFromCommand; //!< Bound to command's cout, or NULL
00106 
00107 //  char fyle_in[L_tmpnam];      // Name of pipe bound to command's cin
00108 //  char fyle_out[L_tmpnam];     // Name of pipe bound to command's cout
00109   char fyle_in[100];      //!< Name of pipe bound to command's cin
00110   char fyle_out[100];     //!< Name of pipe bound to command's cout
00111 
00112 
00113   // Private functions:
00114 
00115   void initialize(const Chars& startCommand, const Chars& restartCommand);
00116   Bool doStart(const Chars& command, const char* greeting);
00117   void closeStreams( );
00118   Bool checkGreeting(const char* greeting);
00119 
00120   // Hide these:
00121 
00122   BlackBox(const BlackBox&) {
00123          error("called BlackBox copy constructor");
00124   }
00125   //!< Copy constructor is prohibited
00126   BlackBox& operator = ( const BlackBox& ) {
00127          error("called BlackBox op=");
00128   }
00129   //!< Assignment operator is prohibited
00130   
00131 };
00132 
00133 #endif
00134 
00135 

Generated at Tue Jun 19 09:49:35 2001 for Magnus Classes by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001