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 #include <unistd.h>
00037 #include <stdio.h>
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 "BESHandlerException.h"
00047 #include "BESDataNames.h"
00048 #include "BESContainer.h"
00049
00050 #define BES_STREAM_BUFFER_SIZE 4096
00051
00052 BESStreamResponseHandler::BESStreamResponseHandler( const string &name )
00053 : BESResponseHandler( name )
00054 {
00055 }
00056
00057 BESStreamResponseHandler::~BESStreamResponseHandler( )
00058 {
00059 }
00060
00073 void
00074 BESStreamResponseHandler::execute( BESDataHandlerInterface &dhi )
00075 {
00076 _response = 0 ;
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 if( dhi.containers.size() != 1 )
00089 {
00090 string err = (string)"Unable to stream file: "
00091 + "no container specified" ;
00092 throw BESHandlerException( err, __FILE__, __LINE__ ) ;
00093 }
00094
00095 dhi.first_container() ;
00096 BESContainer *container = dhi.container ;
00097 string filename = container->access() ;
00098 if( filename.empty() )
00099 {
00100 string err = (string)"Unable to stream file: "
00101 + "filename not specified" ;
00102 throw BESHandlerException( err, __FILE__, __LINE__ ) ;
00103 }
00104
00105 int bytes = 0 ;
00106 ifstream os ;
00107 os.open( filename.c_str(), ios::in ) ;
00108 if( !os )
00109 {
00110 string err = (string)"Unable to stream file: "
00111 + "can not open file "
00112 + filename ;
00113 throw BESHandlerException( err, __FILE__, __LINE__ ) ;
00114 }
00115
00116 int nbytes ;
00117 char block[BES_STREAM_BUFFER_SIZE] ;
00118 os.read( block, sizeof block ) ;
00119 nbytes = os.gcount() ;
00120 while( nbytes )
00121 {
00122 bytes += nbytes ;
00123 dhi.get_output_stream().write( (char*)block, nbytes ) ;
00124 os.read( block, sizeof block ) ;
00125 nbytes = os.gcount() ;
00126 }
00127 os.close() ;
00128 }
00129
00138 void
00139 BESStreamResponseHandler::transmit( BESTransmitter *transmitter,
00140 BESDataHandlerInterface & )
00141 {
00142
00143 }
00144
00151 void
00152 BESStreamResponseHandler::dump( ostream &strm ) const
00153 {
00154 strm << BESIndent::LMarg << "BESStreamResponseHandler::dump - ("
00155 << (void *)this << ")" << endl ;
00156 BESIndent::Indent() ;
00157 BESResponseHandler::dump( strm ) ;
00158 BESIndent::UnIndent() ;
00159 }
00160
00161 BESResponseHandler *
00162 BESStreamResponseHandler::BESStreamResponseBuilder( const string &name )
00163 {
00164 return new BESStreamResponseHandler( name ) ;
00165 }
00166