BESStreamResponseHandler.cc

Go to the documentation of this file.
00001 // BESStreamResponseHandler.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-2009 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 Foundatiion; 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 <cerrno>
00034 #include <iostream>
00035 #include <fstream>
00036 #include <string>
00037 #include <cstring>
00038 
00039 using std::ifstream ;
00040 using std::ios ;
00041 using std::endl ;
00042 using std::string ;
00043 
00044 #include "BESStreamResponseHandler.h"
00045 #include "BESRequestHandlerList.h"
00046 #include "BESNotFoundError.h"
00047 #include "BESInternalError.h"
00048 #include "BESDataNames.h"
00049 #include "BESContainer.h"
00050 
00051 #define BES_STREAM_BUFFER_SIZE 4096
00052 
00053 BESStreamResponseHandler::BESStreamResponseHandler( const string &name )
00054     : BESResponseHandler( name )
00055 {
00056 }
00057 
00058 BESStreamResponseHandler::~BESStreamResponseHandler( )
00059 {
00060 }
00061 
00072 void
00073 BESStreamResponseHandler::execute( BESDataHandlerInterface &dhi )
00074 {
00075     _response = 0 ;
00076 
00077     // What if there is a special way to stream back a data file?
00078     // Should we pass this off to the request handlers and put
00079     // this code into a different class for reuse? For now
00080     // just keep it here. pcw 10/11/06
00081 
00082     // I thought about putting this in the transmit method below
00083     // but decided that this is like executing a non-buffered
00084     // request, so kept it here. Plus the idea expressed above
00085     // led me to leave the code in the execute method.
00086     // pcw 10/11/06
00087     if( dhi.containers.size() != 1 )
00088     {
00089         string err = (string)"Unable to stream file: "
00090                      + "no container specified" ;
00091         throw BESInternalError( err, __FILE__, __LINE__ ) ;
00092     }
00093 
00094     dhi.first_container() ;
00095     BESContainer *container = dhi.container ;
00096     string filename = container->access() ;
00097     if( filename.empty() )
00098     {
00099         string err = (string)"Unable to stream file: "
00100                      + "filename not specified" ;
00101         throw BESInternalError( err, __FILE__, __LINE__ ) ;
00102     }
00103 
00104     int bytes = 0 ;
00105     ifstream os ;
00106     os.open( filename.c_str(), ios::in ) ;
00107     int myerrno = errno ;
00108     if( !os )
00109     {
00110         string serr = (string)"Unable to stream file: "
00111                       + "cannot open file "
00112                       + filename + ": " ;
00113         char *err = strerror( myerrno ) ;
00114         if( err )
00115             serr += err ;
00116         else
00117             serr += "Unknown error" ;
00118 
00119         throw BESNotFoundError( err, __FILE__, __LINE__ ) ;
00120     }
00121 
00122     int nbytes ;
00123     char block[BES_STREAM_BUFFER_SIZE] ;
00124     os.read( block, sizeof block ) ;
00125     nbytes = os.gcount() ;
00126     while( nbytes )
00127     {
00128         bytes += nbytes ;
00129         dhi.get_output_stream().write( (char*)block, nbytes ) ;
00130         os.read( block, sizeof block ) ;
00131         nbytes = os.gcount() ;
00132     }
00133     os.close() ;
00134 }
00135 
00143 void
00144 BESStreamResponseHandler::transmit( BESTransmitter *transmitter,
00145                                     BESDataHandlerInterface &dhi )
00146 {
00147     // The Data is transmitted when it is read, dumped to stdout, so there is nothing
00148     // to transmot here.
00149 }
00150 
00157 void
00158 BESStreamResponseHandler::dump( ostream &strm ) const
00159 {
00160     strm << BESIndent::LMarg << "BESStreamResponseHandler::dump - ("
00161                              << (void *)this << ")" << endl ;
00162     BESIndent::Indent() ;
00163     BESResponseHandler::dump( strm ) ;
00164     BESIndent::UnIndent() ;
00165 }
00166 
00167 BESResponseHandler *
00168 BESStreamResponseHandler::BESStreamResponseBuilder( const string &name )
00169 {
00170     return new BESStreamResponseHandler( name ) ;
00171 }
00172 

Generated on Wed May 12 09:53:07 2010 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.4.7