OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
PPTStreamBuf.cc
Go to the documentation of this file.
1 // PPTStreamBuf.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <sys/types.h>
34 
35 #include <cstdio>
36 #include <unistd.h> // for sync
37 #include <sstream>
38 #include <iomanip>
39 #include <iostream>
40 
41 using std::ostringstream ;
42 using std::hex ;
43 using std::setw ;
44 using std::setfill ;
45 
46 #include "PPTStreamBuf.h"
47 #include "BESDebug.h"
48 
49 const char* eod_marker = "0000000d";
50 const size_t eod_marker_len = 8 ;
51 
52 PPTStreamBuf::PPTStreamBuf( int fd, unsigned bufsize )
53  : d_bufsize( bufsize ),
54  d_buffer( 0 ),
55  count( 0 )
56 {
57  open( fd, bufsize ) ;
58 }
59 
61 {
62  if(d_buffer)
63  {
64  sync() ;
65  delete [] d_buffer ;
66  }
67 }
68 
69 void
70 PPTStreamBuf::open( int fd, unsigned bufsize )
71 {
72  d_fd = fd ;
73  d_bufsize = bufsize == 0 ? 1 : bufsize ;
74 
75  d_buffer = new char[d_bufsize] ;
76  setp( d_buffer, d_buffer + d_bufsize ) ;
77 }
78 
79 // We're stcuk with this return type because this is inherited from stdc++ streambuf. jhrg
80 int
82 {
83  if( pptr() > pbase() )
84  {
85  ostringstream strm ;
86  strm << hex << setw( 7 ) << setfill( '0' ) << (unsigned int)(pptr() - pbase()) << "d" ;
87  string tmp_str = strm.str() ;
88  write( d_fd, tmp_str.c_str(), tmp_str.length() ) ;
89  count += write( d_fd, d_buffer, pptr() - pbase() ) ;
90  setp( d_buffer, d_buffer + d_bufsize ) ;
91 #if 0
92  // If something doesn't look right try using fsync
93  // fsync is not supported for sockets. jhrg 5/4/11
94  fsync(d_fd);
95 #endif
96  }
97 
98  return 0 ;
99 }
100 
101 int
103 {
104  sync() ;
105  if( c != EOF )
106  {
107  *pptr() = static_cast<char>(c) ;
108  pbump( 1 ) ;
109  }
110  return c ;
111 }
112 
113 void
115 {
116  sync() ;
117 
118 #if 0
119  // jhrg 5/5/11
120  ostringstream strm ;
121  /*
122  ostringstream xstrm ;
123  xstrm << "count=" << hex << setw( 7 ) << setfill( '0' ) << how_many() << ";" ;
124  string xstr = xstrm.str() ;
125  strm << hex << setw( 7 ) << setfill( '0' ) << (unsigned int)xstr.length() << "x" << xstr ;
126  */
127  strm << hex << setw( 7 ) << setfill( '0' ) << (unsigned int)0 << "d" ;
128  string tmp_str = strm.str() ;
129 #endif
130 
131  BESDEBUG( "ppt", "PPTStreamBuf::finish - writing " << eod_marker << endl ) ;
132 
133  write( d_fd, eod_marker, eod_marker_len ) ; // tmp_str.c_str(), tmp_str.length() ) ;
134 
135 #if 0
136  // If something doesn't look right try using fsync
137  // jhrg 5/5/11
138  fsync(d_fd);
139 #endif
140  count = 0 ;
141 }
142