BESCmdInterface.cc

Go to the documentation of this file.
00001 // BESCmdInterface.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004,2005 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00032 
00033 #include <unistd.h>
00034 #include <iostream>
00035 #include <sstream>
00036 
00037 using std::endl ;
00038 using std::stringstream ;
00039 
00040 #include "BESCmdInterface.h"
00041 #include "BESCmdParser.h"
00042 #include "BESInterface.h"
00043 #include "BESLog.h"
00044 #include "BESDebug.h"
00045 #include "BESBasicHttpTransmitter.h"
00046 #include "BESReturnManager.h"
00047 #include "BESTransmitException.h"
00048 #include "BESAggFactory.h"
00049 #include "BESAggregationServer.h"
00050 #include "BESTransmitterNames.h"
00051 #include "BESDataNames.h"
00052 
00060 BESCmdInterface::BESCmdInterface()
00061     : BESInterface()
00062 {
00063 }
00064 
00065 BESCmdInterface::BESCmdInterface( const string &cmd, ostream *strm )
00066     : BESInterface( strm )
00067 {
00068     _dhi.data[DATA_REQUEST] = cmd ;
00069 }
00070 
00071 BESCmdInterface::~BESCmdInterface()
00072 {
00073     clean() ;
00074 }
00075 
00084 int
00085 BESCmdInterface::execute_request( const string &from )
00086 {
00087     return BESInterface::execute_request( from ) ;
00088 }
00089 
00099 void
00100 BESCmdInterface::initialize()
00101 {
00102     // dhi has already been filled in at this point, so let's set a default
00103     // transmitter given the protocol. The transmitter might change after
00104     // parsing a request and given a return manager to use. This is done in
00105     // build_data_plan.
00106     //
00107     // The reason I moved this from the build_data_plan method is because a
00108     // registered initialization routine might throw an exception and we
00109     // will need to transmit the exception info, which needs a transmitter.
00110     // If an exception happens before this then the exception info is just
00111     // printed to cout (see BESInterface::transmit_data()). -- pcw 09/05/06
00112     string protocol = _dhi.transmit_protocol ;
00113     if( protocol != "HTTP" )
00114     {
00115         BESDEBUG( "bes", "Finding " << BASIC_TRANSMITTER << " transmitter ... " )
00116         _transmitter = BESReturnManager::TheManager()->find_transmitter( BASIC_TRANSMITTER ) ;
00117         if( !_transmitter )
00118         {
00119             string s = (string)"Unable to find transmitter "
00120                        + BASIC_TRANSMITTER ;
00121             throw BESTransmitException( s, __FILE__, __LINE__ ) ;
00122         }
00123         BESDEBUG( "bes", "OK" << endl )
00124     }
00125     else
00126     {
00127         BESDEBUG( "bes", "Finding " << HTTP_TRANSMITTER << " transmitter ... " )
00128         _transmitter = BESReturnManager::TheManager()->find_transmitter( HTTP_TRANSMITTER ) ;
00129         if( !_transmitter )
00130         {
00131             string s = (string)"Unable to find transmitter "
00132                        + HTTP_TRANSMITTER ;
00133             throw BESTransmitException( s, __FILE__, __LINE__ ) ;
00134         }
00135         BESDEBUG( "bes", "OK" << endl )
00136     }
00137 
00138     BESInterface::initialize() ;
00139 }
00140 
00143 void
00144 BESCmdInterface::validate_data_request()
00145 {
00146     BESInterface::validate_data_request() ;
00147 }
00148 
00153 void
00154 BESCmdInterface::build_data_request_plan()
00155 {
00156     BESDEBUG( "bes", "building request plan for: "<< _dhi.data[DATA_REQUEST] << " ...")
00157     if( BESLog::TheLog()->is_verbose() )
00158     {
00159         *(BESLog::TheLog()) << _dhi.data[SERVER_PID]
00160                              << " from " << _dhi.data[REQUEST_FROM]
00161                              << " [" << _dhi.data[DATA_REQUEST] << "] building"
00162                              << endl ;
00163     }
00164     BESCmdParser::parse( _dhi.data[DATA_REQUEST], _dhi ) ;
00165     BESDEBUG( "bes", " OK" << endl )
00166 
00167     // The default _transmitter (either basic or http depending on the
00168     // protocol passed) has been set in initialize. If the parsed command
00169     // sets a RETURN_CMD (a different transmitter) then look it up here. If
00170     // it's set but not found then this is an error. If it's not set then
00171     // just use the defaults.
00172     if( _dhi.data[RETURN_CMD] != "" )
00173     {
00174         BESDEBUG( "bes", "Finding transmitter: " << _dhi.data[RETURN_CMD] << " ...  " )
00175         _transmitter = BESReturnManager::TheManager()->find_transmitter( _dhi.data[RETURN_CMD] ) ;
00176         if( !_transmitter )
00177         {
00178             string s = (string)"Unable to find transmitter "
00179                        + _dhi.data[RETURN_CMD] ;
00180             throw BESTransmitException( s, __FILE__, __LINE__ ) ;
00181         }
00182         BESDEBUG( "bes", "OK" << endl )
00183     }
00184 
00185     if( BESDebug::IsSet( "bes" ) ) _dhi.dump( *(BESDebug::GetStrm()) ) ;
00186 }
00187 
00195 void
00196 BESCmdInterface::execute_data_request_plan()
00197 {
00198     if( BESLog::TheLog()->is_verbose() )
00199     {
00200         *(BESLog::TheLog()) << _dhi.data[SERVER_PID]
00201                              << " from " << _dhi.data[REQUEST_FROM]
00202                              << " [" << _dhi.data[DATA_REQUEST] << "] executing"
00203                              << endl ;
00204     }
00205     BESInterface::execute_data_request_plan() ;
00206 }
00207 
00215 void
00216 BESCmdInterface::invoke_aggregation()
00217 {
00218     if( _dhi.data[AGG_CMD] == "" )
00219     {
00220         if( BESLog::TheLog()->is_verbose() )
00221         {
00222             *(BESLog::TheLog()) << _dhi.data[SERVER_PID]
00223                                  << " from " << _dhi.data[REQUEST_FROM]
00224                                  << " [" << _dhi.data[DATA_REQUEST] << "]"
00225                                  << " not aggregating, command empty"
00226                                  << endl ;
00227         }
00228     }
00229     else
00230     {
00231         BESAggregationServer *agg = BESAggFactory::TheFactory()->find_handler( _dhi.data[AGG_HANDLER] ) ;
00232         if( !agg )
00233         {
00234             if( BESLog::TheLog()->is_verbose() )
00235             {
00236                 *(BESLog::TheLog()) << _dhi.data[SERVER_PID]
00237                                      << " from " << _dhi.data[REQUEST_FROM]
00238                                      << " [" << _dhi.data[DATA_REQUEST] << "]"
00239                                      << " not aggregating, no handler"
00240                                      << endl ;
00241             }
00242         }
00243         else
00244         {
00245             if( BESLog::TheLog()->is_verbose() )
00246             {
00247                 *(BESLog::TheLog()) << _dhi.data[SERVER_PID]
00248                                      << " from " << _dhi.data[REQUEST_FROM]
00249                                      << " [" << _dhi.data[DATA_REQUEST]
00250                                      << "] aggregating" << endl ;
00251             }
00252             //This is done in BESInterface::invoke_aggregation
00253             //agg->aggregate( _dhi ) ;
00254         }
00255     }
00256     BESInterface::invoke_aggregation() ;
00257 }
00258 
00266 void
00267 BESCmdInterface::transmit_data()
00268 {
00269     if( BESLog::TheLog()->is_verbose() )
00270     {
00271         *(BESLog::TheLog()) << _dhi.data[SERVER_PID]
00272                              << " from " << _dhi.data[REQUEST_FROM]
00273                              << " [" << _dhi.data[DATA_REQUEST]
00274                              << "] transmitting" << endl ;
00275     }
00276     BESInterface::transmit_data() ;
00277 } 
00278 
00283 void
00284 BESCmdInterface::log_status()
00285 {
00286     string result = "completed" ;
00287     if( _dhi.error_info )
00288         result = "failed" ;
00289     if( BESLog::TheLog()->is_verbose() )
00290     {
00291         *(BESLog::TheLog()) << _dhi.data[SERVER_PID]
00292                              << " from " << _dhi.data[REQUEST_FROM]
00293                              << " [" << _dhi.data[DATA_REQUEST] << "] "
00294                              << result << endl ;
00295     }
00296 }
00297 
00306 void
00307 BESCmdInterface::clean()
00308 {
00309     BESInterface::clean() ;
00310     if( BESLog::TheLog()->is_verbose() )
00311     {
00312         *(BESLog::TheLog()) << _dhi.data[SERVER_PID]
00313                              << " from " << _dhi.data[REQUEST_FROM]
00314                              << " [" << _dhi.data[DATA_REQUEST] << "] cleaning"
00315                              << endl ;
00316     }
00317 }
00318 
00325 void
00326 BESCmdInterface::dump( ostream &strm ) const
00327 {
00328     strm << BESIndent::LMarg << "BESCmdInterface::dump - ("
00329                              << (void *)this << ")" << endl ;
00330     BESIndent::Indent() ;
00331     BESInterface::dump( strm ) ;
00332     BESIndent::UnIndent() ;
00333 
00334 
00335 }
00336 

Generated on Sat Jan 19 04:05:36 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.4