OPeNDAP Hyrax Back End Server (BES)
Updated for version 3.8.3
|
00001 // PPTStreamBuf.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 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 <sys/types.h> 00034 00035 #include <cstdio> 00036 #include <unistd.h> // for sync 00037 #include <sstream> 00038 #include <iomanip> 00039 #include <iostream> 00040 00041 using std::ostringstream ; 00042 using std::hex ; 00043 using std::setw ; 00044 using std::setfill ; 00045 00046 #include "PPTStreamBuf.h" 00047 #include "BESDebug.h" 00048 00049 const char* eod_marker = "0000000d"; 00050 const size_t eod_marker_len = 8 ; 00051 00052 PPTStreamBuf::PPTStreamBuf( int fd, unsigned bufsize ) 00053 : d_bufsize( bufsize ), 00054 d_buffer( 0 ), 00055 count( 0 ) 00056 { 00057 open( fd, bufsize ) ; 00058 } 00059 00060 PPTStreamBuf::~PPTStreamBuf() 00061 { 00062 if(d_buffer) 00063 { 00064 sync() ; 00065 delete [] d_buffer ; 00066 } 00067 } 00068 00069 void 00070 PPTStreamBuf::open( int fd, unsigned bufsize ) 00071 { 00072 d_fd = fd ; 00073 d_bufsize = bufsize == 0 ? 1 : bufsize ; 00074 00075 d_buffer = new char[d_bufsize] ; 00076 setp( d_buffer, d_buffer + d_bufsize ) ; 00077 } 00078 00079 // We're stcuk with this return type because this is inherited from stdc++ streambuf. jhrg 00080 int 00081 PPTStreamBuf::sync() 00082 { 00083 if( pptr() > pbase() ) 00084 { 00085 ostringstream strm ; 00086 strm << hex << setw( 7 ) << setfill( '0' ) << (unsigned int)(pptr() - pbase()) << "d" ; 00087 string tmp_str = strm.str() ; 00088 write( d_fd, tmp_str.c_str(), tmp_str.length() ) ; 00089 count += write( d_fd, d_buffer, pptr() - pbase() ) ; 00090 setp( d_buffer, d_buffer + d_bufsize ) ; 00091 #if 0 00092 // If something doesn't look right try using fsync 00093 // fsync is not supported for sockets. jhrg 5/4/11 00094 fsync(d_fd); 00095 #endif 00096 } 00097 00098 return 0 ; 00099 } 00100 00101 int 00102 PPTStreamBuf::overflow( int c ) 00103 { 00104 sync() ; 00105 if( c != EOF ) 00106 { 00107 *pptr() = static_cast<char>(c) ; 00108 pbump( 1 ) ; 00109 } 00110 return c ; 00111 } 00112 00113 void 00114 PPTStreamBuf::finish() 00115 { 00116 sync() ; 00117 00118 #if 0 00119 // jhrg 5/5/11 00120 ostringstream strm ; 00121 /* 00122 ostringstream xstrm ; 00123 xstrm << "count=" << hex << setw( 7 ) << setfill( '0' ) << how_many() << ";" ; 00124 string xstr = xstrm.str() ; 00125 strm << hex << setw( 7 ) << setfill( '0' ) << (unsigned int)xstr.length() << "x" << xstr ; 00126 */ 00127 strm << hex << setw( 7 ) << setfill( '0' ) << (unsigned int)0 << "d" ; 00128 string tmp_str = strm.str() ; 00129 #endif 00130 00131 BESDEBUG( "ppt", "PPTStreamBuf::finish - writing " << eod_marker << endl ) ; 00132 00133 write( d_fd, eod_marker, eod_marker_len ) ; // tmp_str.c_str(), tmp_str.length() ) ; 00134 00135 #if 0 00136 // If something doesn't look right try using fsync 00137 // jhrg 5/5/11 00138 fsync(d_fd); 00139 #endif 00140 count = 0 ; 00141 } 00142