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