00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <iostream>
00034 #include <fstream>
00035 #include <string>
00036
00037 using std::ifstream ;
00038 using std::ios ;
00039 using std::endl ;
00040 using std::string ;
00041
00042 #include "BESStreamResponseHandler.h"
00043 #include "BESRequestHandlerList.h"
00044 #include "BESNotFoundError.h"
00045 #include "BESInternalError.h"
00046 #include "BESDataNames.h"
00047 #include "BESContainer.h"
00048
00049 #define BES_STREAM_BUFFER_SIZE 4096
00050
00051 BESStreamResponseHandler::BESStreamResponseHandler( const string &name )
00052 : BESResponseHandler( name )
00053 {
00054 }
00055
00056 BESStreamResponseHandler::~BESStreamResponseHandler( )
00057 {
00058 }
00059
00070 void
00071 BESStreamResponseHandler::execute( BESDataHandlerInterface &dhi )
00072 {
00073 _response = 0 ;
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 if( dhi.containers.size() != 1 )
00086 {
00087 string err = (string)"Unable to stream file: "
00088 + "no container specified" ;
00089 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00090 }
00091
00092 dhi.first_container() ;
00093 BESContainer *container = dhi.container ;
00094 string filename = container->access() ;
00095 if( filename.empty() )
00096 {
00097 string err = (string)"Unable to stream file: "
00098 + "filename not specified" ;
00099 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00100 }
00101
00102 int bytes = 0 ;
00103 ifstream os ;
00104 os.open( filename.c_str(), ios::in ) ;
00105 if( !os )
00106 {
00107 string err = (string)"Unable to stream file: "
00108 + "can not open file "
00109 + filename ;
00110 throw BESNotFoundError( err, __FILE__, __LINE__ ) ;
00111 }
00112
00113 int nbytes ;
00114 char block[BES_STREAM_BUFFER_SIZE] ;
00115 os.read( block, sizeof block ) ;
00116 nbytes = os.gcount() ;
00117 while( nbytes )
00118 {
00119 bytes += nbytes ;
00120 dhi.get_output_stream().write( (char*)block, nbytes ) ;
00121 os.read( block, sizeof block ) ;
00122 nbytes = os.gcount() ;
00123 }
00124 os.close() ;
00125 }
00126
00134 void
00135 BESStreamResponseHandler::transmit( BESTransmitter *transmitter,
00136 BESDataHandlerInterface &dhi )
00137 {
00138
00139
00140 }
00141
00148 void
00149 BESStreamResponseHandler::dump( ostream &strm ) const
00150 {
00151 strm << BESIndent::LMarg << "BESStreamResponseHandler::dump - ("
00152 << (void *)this << ")" << endl ;
00153 BESIndent::Indent() ;
00154 BESResponseHandler::dump( strm ) ;
00155 BESIndent::UnIndent() ;
00156 }
00157
00158 BESResponseHandler *
00159 BESStreamResponseHandler::BESStreamResponseBuilder( const string &name )
00160 {
00161 return new BESStreamResponseHandler( name ) ;
00162 }
00163