Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Framework/src/AppOutputCommand.cc

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: AppOutputCommand.cc,v 1.12 2002/08/03 01:31:05 desilva Exp $
00004 //
00005 // Description:
00006 //      Class AppOutputCommand. Command handler for the "output" command for
00007 //      the standard file output module. Valid subcommands are:
00008 //
00009 //      output clear                            Empty the list of streams
00010 //      output create <stream> [<dest>]         Create stream optionally bound to destination
00011 //      output delete <stream1> [<str2> ..]     Remove stream(s) from list
00012 //      output destination <stream> [<dest>]    Bind stream to destination
00013 //      output disable <stream1> [<str2> ..]    Disable stream(s)
00014 //      output enable  <stream1> [<str2> ..]    Enable stream(s)
00015 //      output help                             Display help information
00016 //      output list                             List current streams/files
00017 //      output paths <stream> <path1>...        Bind <path1> etc. to stream
00018 //      output reset                            Synonym for clear
00019 //      output statistics                       List statistics for each stream
00020 //      output stream <stream> [<dest>]         Synonym for create
00021 //      output wipe-out                         Synonym for clear
00022 //
00023 // Environment:
00024 //      Software developed for the BaBar Detector at the SLAC B-Factory.
00025 //
00026 // Author List:
00027 //      David R. Quarrie                Original Author
00028 //      Marc Turcotte                   Merged in R.Kapur's TK interface
00029 //
00030 // Copyright Information:
00031 //      Copyright (C) 1994, 1995        Lawrence Berkeley Laboratory
00032 //      Copyright (C) 1998              University of Texas at Dallas
00033 //
00034 //------------------------------------------------------------------------
00035 #include "Experiment/Experiment.hh"
00036 
00037 //-----------------------
00038 // This Class's Header --
00039 //-----------------------
00040 #include "Framework/AppOutputCommand.hh"
00041 
00042 //-------------
00043 // C Headers --
00044 //-------------
00045 #include <assert.h>
00046 #include <string.h>
00047 #include <strstream.h>
00048 
00049 //-------------------------------
00050 // Collaborating Class Headers --
00051 //-------------------------------
00052 #include "Framework/APPFramework.hh"
00053 #include "Framework/AppStreamsOutputModule.hh"
00054 #include "Framework/AppStream.hh"
00055 #include "Framework/APPPath.hh"
00056 #include "FrameUtil/APPList.hh"
00057 #include "FrameUtil/APPListIterator.hh"
00058 #include "FrameUtil/AbsInterp.hh"
00059 #include "FrameUtil/FwkString.hh"
00060 
00061 //              ----------------------------------------
00062 //              -- Public Function Member Definitions --
00063 //              ----------------------------------------
00064 
00065 //----------------
00066 // Constructors --
00067 //----------------
00068 
00069 AppOutputCommand::AppOutputCommand( 
00070     const char* const theCommand, AppModule* theTarget )
00071     : APPCommand( theCommand, theTarget )
00072 {
00073 }
00074 
00075 //--------------
00076 // Destructor --
00077 //--------------
00078 
00079 AppOutputCommand::~AppOutputCommand( )
00080 {
00081 }
00082 
00083 //-----------
00084 // Handler --
00085 //-----------
00086 
00087 int 
00088 AppOutputCommand::handle( int argc, char* argv[] )
00089 {
00090     AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00091     int result = AbsInterp::ERROR;
00092     
00093     setArgs( argc, argv );
00094     if ( 2 <= argc ) {
00095         result = handleCommand( argc, argv );
00096         if ( AbsInterp::OK != result ) {
00097           FwkString txtmsg("invalid command name: ");
00098           for (int index=0; index < argc; index++ ) {
00099             txtmsg += " ";
00100             txtmsg += argv[index];
00101           }
00102           theOutput->setError( txtmsg );
00103         }
00104     } else {
00105         theOutput->setError( "wrong # args" );
00106     }
00107     return result;
00108 }
00109 
00110 void
00111 AppOutputCommand::show( ) const {
00112 }
00113 
00114 bool
00115 AppOutputCommand::isShowable( ) const {
00116    return true; 
00117 }
00118 
00119 //              -------------------------------------------
00120 //              -- Protected Function Member Definitions --
00121 //              -------------------------------------------
00122 
00123 int 
00124 AppOutputCommand::handleCommand( int argc, char* argv[] )
00125 {
00126     int result = AbsInterp::ERROR;
00127 
00128     if        ( 0 == strcmp( argv[1], "clear"    ) || 
00129                 0 == strcmp( argv[1], "reset"    ) ||
00130                 0 == strcmp( argv[1], "wipe"     ) || 
00131                 0 == strcmp( argv[1], "wipe-out" ) ) { 
00132         result = wipeoutHandler( );
00133     } else if ( 0 == strcmp( argv[1], "create" ) || 
00134                 0 == strcmp( argv[1], "cr"     ) ||
00135                 0 == strcmp( argv[1], "stream" ) || 
00136                 0 == strcmp( argv[1], "st"     ) ) {
00137         result = createHandler( );
00138     } else if ( 0 == strcmp( argv[1], "delete" ) ) {
00139         result = deleteHandler( );
00140     } else if ( 0 == strcmp( argv[1], "destination" ) || 
00141                 0 == strcmp( argv[1], "des"         ) ) {
00142         result = destinationHandler( );
00143     } else if ( 0 == strcmp( argv[1], "disable" ) || 
00144                 0 == strcmp( argv[1], "dis"     ) ) {
00145         result = disableHandler( );
00146     } else if ( 0 == strcmp( argv[1], "enable" ) || 
00147                 0 == strcmp( argv[1], "ena"    ) ) {
00148         result = enableHandler( );
00149     } else if ( 0 == strcmp( argv[1], "help" ) || 
00150                 0 == strcmp( argv[1], "he"   ) ) {
00151         result = helpHandler( );
00152     } else if ( 0 == strcmp( argv[1], "list" ) || 
00153                 0 == strcmp( argv[1], "li"   ) ) {
00154         result = listHandler( );
00155     } else if ( 0 == strcmp( argv[1], "paths" ) ||
00156                 0 == strcmp( argv[1], "path"  ) ) {
00157         result = pathsHandler( );
00158     } else if ( 0 == strcmp( argv[1], "statistics" ) || 
00159                 0 == strcmp( argv[1], "stat"       ) ) {
00160         result = statisticsHandler( );
00161     }
00162     return result;
00163 }
00164 
00165 int 
00166 AppOutputCommand::createHandler( )
00167 {
00168   AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00169   AppFramework*           theFrame  = theOutput->framework( );
00170   AppStream*              theStream;
00171   char*                   theName;
00172   char*                   theDestination;
00173   int index  = 2;
00174   int result = AbsInterp::ERROR;
00175   
00176     if ( NULL != ( theName = getArgument( index ) ) ) {
00177       if ( validateName( theName ) ) {
00178         if ( ! theOutput->has( theName ) ) {
00179           index++;
00180           theDestination = getArgument( index );
00181           theStream      = createStream( theName, theDestination );
00182           theOutput->append( theStream );
00183           result = AbsInterp::OK;
00184         } else {
00185           ostrstream error;
00186           error << "***** Duplicate stream name: " << theName 
00187                 << " - ignored" << endl;
00188           char* ptrerr = error.str( );
00189           theFrame->setError( ptrerr);
00190           delete [] ptrerr;  // str() freezes underlying array
00191         }
00192       }
00193     }
00194     return result;
00195 }
00196 
00197 int 
00198 AppOutputCommand::deleteHandler( )
00199 {
00200   AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00201   AppFramework*           theFrame  = theOutput->framework( );
00202   char*                   theName;
00203   AppStream*              theStream;
00204   ostrstream error;
00205   int index  = 2;
00206   int result = AbsInterp::OK;
00207   
00208   while ( NULL != ( theName = getArgument( index ) ) ) {
00209     if ( NULL != ( theStream = theOutput->fetch( theName ) ) ) {
00210       theOutput->remove( theName );
00211     } else {
00212       if ( AbsInterp::OK == result ) {
00213         error << "***** Unknown stream name(s):";
00214       }
00215       error << " " << theName;
00216       result = AbsInterp::ERROR;
00217     }
00218     index++;
00219   }
00220   if ( AbsInterp::OK != result ) {
00221     error << " -ignored" << endl;
00222     char* ptrerr = error.str( );
00223     theFrame->setError( ptrerr );
00224     delete [] ptrerr; // str() freezes underlying array
00225   }
00226   return result;
00227 }
00228 
00229 int 
00230 AppOutputCommand::destinationHandler( )
00231 {
00232   AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00233   AppFramework*           theFrame  = theOutput->framework( );
00234   AppStream*              theStream;
00235   char*                   theName;
00236   int index  = 2;
00237   int result = AbsInterp::ERROR;
00238   
00239   if ( NULL != ( theName = getArgument( index ) ) ) {
00240     if ( validateName( theName ) ) {
00241       if ( NULL != ( theStream = theOutput->fetch( theName ) ) ) {
00242         index++;
00243         theName = getArgument( index );
00244         theStream->setDestination( theName );
00245         result = AbsInterp::OK;
00246       } else {
00247         ostrstream error;
00248         error << "***** Unknown stream name: " << theName <<" - ignored" << endl;
00249         char* ptrerr = error.str(); 
00250         theFrame->setError( ptrerr );
00251         delete [] ptrerr; // str() freezes underlying array
00252         result = AbsInterp::ERROR;
00253       }
00254     }
00255   }
00256   return result;
00257 }
00258 
00259 int
00260 AppOutputCommand::disableHandler( )
00261 {
00262 
00263   AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00264   AppFramework*           theFrame  = theOutput->framework( );
00265   AppStream*              theStream;
00266   char*                   theName;
00267   ostrstream error;
00268   int index  = 2;
00269   int result = AbsInterp::OK;
00270   while ( NULL != ( theName = getArgument( index ) ) ) {
00271     if ( NULL != ( theStream = theOutput->fetch( theName ) ) ) {
00272       theStream->setEnabled( false );
00273     } else {
00274       if ( AbsInterp::OK == result ) {
00275         error << "***** Unknown stream name(s):";
00276       }
00277       error << " " << theName;
00278       result = AbsInterp::ERROR;
00279     }
00280     index++;
00281   }
00282   if ( AbsInterp::OK != result ) {
00283     error << " -ignored" << endl;
00284     char* ptrerr = error.str( );
00285     theFrame->setError(ptrerr);
00286     delete [] ptrerr; // str() freezes underlying array
00287   }
00288   return result;
00289 }
00290 
00291 int 
00292 AppOutputCommand::enableHandler( )
00293 {
00294   AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00295   AppFramework*           theFrame  = theOutput->framework( );
00296   AppStream*              theStream;
00297   char*                   theName;
00298   ostrstream error;
00299   int index  = 2;
00300   int result = AbsInterp::OK;
00301   while ( NULL != ( theName = getArgument( index ) ) ) {
00302     if ( NULL != ( theStream = theOutput->fetch( theName ) ) ) {
00303       theStream->setEnabled( true );
00304     } else {
00305       if ( AbsInterp::OK == result ) {
00306         error << "***** Unknown stream name(s):";
00307       }
00308       error << " " << theName;
00309       result = AbsInterp::ERROR;
00310     }
00311     index++;
00312   }
00313   if ( AbsInterp::OK != result ) {
00314     error << " -ignored" << endl;
00315     char* ptrerr = error.str( );
00316     theFrame->setError(ptrerr);
00317     delete [] ptrerr; // str() freezes underlying array
00318   }
00319   return result;
00320 }
00321 
00322 int 
00323 AppOutputCommand::helpHandler( )
00324 {
00325     AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00326     AppFramework*           theFrame  = theOutput->framework( );
00327     int result = AbsInterp::OK;
00328 
00329     theFrame->partialReport( "output clear                            " );
00330     theFrame->fullReport("Empty the list of streams" );
00331     theFrame->partialReport( "output create <stream> [<dest>]         " );
00332     theFrame->fullReport("Create stream" );
00333     theFrame->partialReport( "output delete <stream1> [<str2> ..]     " );
00334     theFrame->fullReport("Remove stream(s) from list" );
00335     theFrame->partialReport( "output destination <stream> [<dest>]    " );
00336     theFrame->fullReport("Bind stream to destination" );
00337     theFrame->partialReport( "output disable <stream1> [<str2> ..]    " );
00338     theFrame->fullReport("Disable stream(s)" );
00339     theFrame->partialReport( "output enable  <stream1> [<str2> ..]    " );
00340     theFrame->fullReport("Enable stream(s)" );
00341     theFrame->partialReport( "output paths <stream> <path1>...        " );
00342     theFrame->fullReport("Bind <path1> etc. to stream" );
00343     theFrame->partialReport( "output reset                            " );
00344     theFrame->fullReport("Synonym for clear" );
00345     theFrame->partialReport( "output statistics                       " );
00346     theFrame->fullReport("List statistics for each stream" );
00347     theFrame->partialReport( "output stream <stream> [<dest>]         " );
00348     theFrame->fullReport("Synonym for create" );
00349     theFrame->partialReport( "output wipe-out                         " );
00350     theFrame->fullReport("Synonym for clear" );
00351     return result;
00352 }
00353 
00354 int 
00355 AppOutputCommand::listHandler( )
00356 {
00357     AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00358     AppFramework*           theFrame  = theOutput->framework( );
00359     APPList< AppStream* >*  theList   = theOutput->streams( );
00360     APPList< APPPath* >*    thePaths;
00361     AppStream**             theStream;
00362     APPPath**               thePath;
00363     const char*             theDest;
00364 
00365     if ( ! theList->isEmpty( ) ) {
00366         APPListIterator<AppStream*> iter1( *theList );
00367         while ( theStream = iter1( ) ) {
00368             theFrame->fullReport( "" );
00369             theFrame->partialReport( (*theStream)->name( ) );
00370             theFrame->partialReport( ":" );
00371             if ( ! (*theStream)->isEnabled( ) ) {
00372                 theFrame->partialReport( " (disabled)" );
00373             }
00374             theFrame->fullReport( "" );
00375             theFrame->partialReport( "    " );
00376             theFrame->partialReport( destinationString( ) );
00377             theFrame->partialReport( ": " );
00378             if ( NULL != ( theDest = (*theStream)->destination( ) ) ) {
00379                 theFrame->partialReport( theDest );
00380             } else {
00381                 theFrame->partialReport( "(none)" );
00382             }
00383             theFrame->fullReport("");
00384             listInformation( *theStream, theFrame );
00385             theFrame->partialReport( "    Paths: " );
00386             thePaths = (*theStream)->paths( );
00387             if ( ! thePaths->isEmpty( ) ) {
00388                 APPListIterator<APPPath*> iter2( *thePaths );
00389                 while ( thePath = iter2( ) ) {
00390                     theFrame->partialReport( (*thePath)->name( ) );
00391                     theFrame->partialReport(" ");
00392                 }
00393             } else {
00394                 theFrame->partialReport( "(none)" );
00395             }
00396             theFrame->fullReport(""); 
00397         }
00398     } else {
00399         theFrame->fullReport( "No output streams are defined" );
00400     }
00401     return AbsInterp::OK;
00402 
00403 }
00404 
00405 int 
00406 AppOutputCommand::pathsHandler( )
00407 {
00408   AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00409   AppFramework*           theFrame  = theOutput->framework( );
00410   AppStream*              theStream;
00411   APPPath*                thePath;
00412   char*                   theName;
00413   bool add   = true;
00414   int index  = 2;
00415   
00416   int result = AbsInterp::OK;
00417   
00418   
00419   if ( NULL != ( theName = getArgument( index ) ) ) {
00420     if ( NULL != ( theStream = theOutput->fetch( theName ) ) ) {
00421       index++;
00422       while ( NULL != ( theName = getArgument( index ) ) ) {
00423         if (        0 == strcmp( theName, "-add"    ) ||
00424                     0 == strcmp( theName, "-a"      ) ) {
00425           add = true;
00426         } else if ( 0 == strcmp( theName, "-remove" ) ||
00427                     0 == strcmp( theName, "-r"      ) ) {
00428           add = false;
00429         } else {
00430           if ( NULL != ( thePath = theFrame->fetchPath( theName ) ) ) {
00431             if ( add ) {
00432               theStream->append( thePath );
00433             } else {
00434               theStream->remove( thePath );
00435             }
00436           } else {
00437             theFrame->setError("unknown path name");
00438             result = AbsInterp::ERROR;
00439           }
00440         }
00441         index++;
00442       }
00443     } else {
00444       ostrstream error;
00445       error << "***** Unknown stream name: " << theName <<" - ignored" << endl;
00446       char* ptrerr = error.str();
00447       theFrame->setError(ptrerr);
00448       delete [] ptrerr; // str() freezes underlying array
00449       result = AbsInterp::ERROR;
00450     }
00451   } else {
00452     theFrame->setError("no arguments supplied");
00453     result = AbsInterp::ERROR;
00454   }
00455   return result;
00456 }
00457 
00458 int 
00459 AppOutputCommand::statisticsHandler( )
00460 {
00461   AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00462   AppFramework*           theFrame  = theOutput->framework( );
00463   APPList< AppStream* >*  theList   = theOutput->streams( );
00464   APPList< APPPath* >*    thePaths;
00465   AppStream**             theStream;
00466   
00467   if ( ! theList->isEmpty( ) ) {
00468     APPListIterator<AppStream*> iter1( *theList );
00469     while ( theStream = iter1( ) ) {
00470       theFrame->fullReport( "" );
00471       theFrame->partialReport( (*theStream)->name( ) );
00472       theFrame->partialReport( ":   " );
00473       ostrstream stream;
00474       stream << (*theStream)->events( ) << '\0';
00475       char* bufptr = stream.str( );
00476       theFrame->partialReport( bufptr );
00477       delete [] bufptr; // str() freezes underlying array
00478       theFrame->fullReport( " events" );
00479       statisticsInformation( *theStream, theFrame );
00480     }
00481   } else {
00482     theFrame->fullReport( "No output streams are defined" );
00483   }
00484   return AbsInterp::OK;
00485   
00486 }
00487 
00488 int 
00489 AppOutputCommand::wipeoutHandler( ) {
00490     AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00491     theOutput->wipeout( );
00492     return AbsInterp::OK;
00493 }
00494 
00495 void 
00496 AppOutputCommand::listInformation( AppStream* theStream, AppFramework* theFrame )
00497 {
00498 }
00499 
00500 void 
00501 AppOutputCommand::statisticsInformation( AppStream* theStream, AppFramework* theFrame )
00502 {
00503 }
00504 
00505 void 
00506 AppOutputCommand::dumpState(AppConfigRecorder* r) {
00507 
00508   char* var[10];
00509   AppStreamsOutputModule* theOutput = (AppStreamsOutputModule*)target( );
00510   if (theOutput->streams( ) != 0) {
00511     APPListIterator<AppStream*> theIterator( *theOutput->streams( ) );
00512     AppStream** theStream;
00513     var[0] = "output";
00514     while (theStream = theIterator()) {
00515       
00516       // create the stream
00517       var[1] = "create";
00518       var[2] = const_cast<char*>((*theStream)->name());
00519       if ((*theStream)->destination() != 0) {
00520         var[3] = const_cast<char*>((*theStream)->destination());
00521         r->store(4, var);
00522       } else {
00523         r->store(3, var);
00524       }
00525       
00526       // enable or disable stream
00527       if ((*theStream)->isEnabled()) {
00528         var[1] = "enable";
00529       } else {
00530         var[1] = "disable";
00531       }
00532       var[2] = const_cast<char*>((*theStream)->name());
00533       r->store(3, var);
00534       
00535       // paths bound to this stream
00536       APPPath** thePath;
00537       APPListIterator<APPPath*> itsPaths(*(*theStream)->paths()); 
00538       while (thePath = itsPaths()) {
00539         var[1] = "paths";
00540         var[2] = const_cast<char*>((*theStream)->name());
00541         var[3] = const_cast<char*>((*thePath)->name());
00542         r->store(4, var);
00543       }
00544     }
00545   }
00546 } 
00547 
00548 void 
00549 AppOutputCommand::restoreState(char* argv[]) {
00550   int i=0;
00551   while(argv[i]){
00552     i++;
00553   }
00554   int argc = i;
00555   int rval = handle(argc, argv);
00556 }
00557 

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002