00001
00002
00003 #include "XDRStreamMarshaller.h"
00004
00005 #include "Vector.h"
00006 #include "util.h"
00007
00008 char *XDRStreamMarshaller::_buf = 0 ;
00009
00010 XDRStreamMarshaller::XDRStreamMarshaller( ostream &out )
00011 : _sink( 0 ),
00012 _out( out )
00013 {
00014 if( !_buf )
00015 _buf = (char *)malloc( DODS_MAX_ARRAY ) ;
00016 if ( !_buf )
00017 throw Error("Failed to allocate memory for data serialization.");
00018
00019 _sink = new XDR ;
00020 xdrmem_create( _sink, _buf, DODS_MAX_ARRAY, XDR_ENCODE ) ;
00021 }
00022
00023 XDRStreamMarshaller::~XDRStreamMarshaller( )
00024 {
00025 if( _sink )
00026 delete_xdrstdio( _sink ) ;
00027 }
00028
00029 void
00030 XDRStreamMarshaller::put_byte( dods_byte val )
00031 {
00032 if( !xdr_setpos( _sink, 0 ) )
00033 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00034 if( !xdr_char( _sink, (char *)&val ) )
00035 throw Error("Network I/O Error. Could not send byte data.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00036 unsigned int bytes_written = xdr_getpos( _sink ) ;
00037 if( !bytes_written )
00038 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00039 _out.write( _buf, bytes_written ) ;
00040 }
00041
00042 void
00043 XDRStreamMarshaller::put_int16( dods_int16 val )
00044 {
00045 if( !xdr_setpos( _sink, 0 ) )
00046 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00047 if( !XDR_INT16( _sink, &val ) )
00048 throw Error("Network I/O Error. Could not send int 16 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
00049 unsigned int bytes_written = xdr_getpos( _sink ) ;
00050 if( !bytes_written )
00051 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00052 _out.write( _buf, bytes_written ) ;
00053 }
00054
00055 void
00056 XDRStreamMarshaller::put_int32( dods_int32 val )
00057 {
00058 if( !xdr_setpos( _sink, 0 ) )
00059 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00060 if( !XDR_INT32( _sink, &val ) )
00061 throw Error("Network I/O Error. Culd not read int 32 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
00062 unsigned int bytes_written = xdr_getpos( _sink ) ;
00063 if( !bytes_written )
00064 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00065 _out.write( _buf, bytes_written ) ;
00066 }
00067
00068 void
00069 XDRStreamMarshaller::put_float32( dods_float32 val )
00070 {
00071 if( !xdr_setpos( _sink, 0 ) )
00072 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00073 if( !xdr_float( _sink, &val ) )
00074 throw Error("Network I/O Error. Could not send float 32 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
00075 unsigned int bytes_written = xdr_getpos( _sink ) ;
00076 if( !bytes_written )
00077 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00078 _out.write( _buf, bytes_written ) ;
00079 }
00080
00081 void
00082 XDRStreamMarshaller::put_float64( dods_float64 val )
00083 {
00084 if( !xdr_setpos( _sink, 0 ) )
00085 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00086 if( !xdr_double( _sink, &val ) )
00087 throw Error("Network I/O Error. Could not send float 64 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
00088 unsigned int bytes_written = xdr_getpos( _sink ) ;
00089 if( !bytes_written )
00090 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00091 _out.write( _buf, bytes_written ) ;
00092 }
00093
00094 void
00095 XDRStreamMarshaller::put_uint16( dods_uint16 val )
00096 {
00097 if( !xdr_setpos( _sink, 0 ) )
00098 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00099 if( !XDR_UINT16( _sink, &val ) )
00100 throw Error("Network I/O Error. Could not send uint 16 data. This may be due to a\nbug in libdap or a problem with the network connection.");
00101 unsigned int bytes_written = xdr_getpos( _sink ) ;
00102 if( !bytes_written )
00103 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00104 _out.write( _buf, bytes_written ) ;
00105 }
00106
00107 void
00108 XDRStreamMarshaller::put_uint32( dods_uint32 val )
00109 {
00110 if( !xdr_setpos( _sink, 0 ) )
00111 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00112 if( !XDR_UINT32( _sink, &val ) )
00113 throw Error("Network I/O Error. Could not send uint 32 data. This may be due to a\nbug in libdap or a problem with the network connection.");
00114 unsigned int bytes_written = xdr_getpos( _sink ) ;
00115 if( !bytes_written )
00116 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00117 _out.write( _buf, bytes_written ) ;
00118 }
00119
00120 void
00121 XDRStreamMarshaller::put_str( const string &val )
00122 {
00123 if( !xdr_setpos( _sink, 0 ) )
00124 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00125
00126 const char *out_tmp = val.c_str() ;
00127 if( !xdr_string( _sink, (char **)&out_tmp, max_str_len) )
00128 throw Error("Network I/O Error. Could not send string data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
00129
00130 unsigned int bytes_written = xdr_getpos( _sink ) ;
00131 if( !bytes_written )
00132 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00133 _out.write( _buf, bytes_written ) ;
00134 }
00135
00136 void
00137 XDRStreamMarshaller::put_url( const string &val )
00138 {
00139 put_str( val ) ;
00140 }
00141
00142 void
00143 XDRStreamMarshaller::put_opaque( char *val, unsigned int len )
00144 {
00145 if( !xdr_setpos( _sink, 0 ) )
00146 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00147 if( !xdr_opaque( _sink, val, len ) )
00148 throw Error("Network I/O Error. Could not send opaque data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
00149 unsigned int bytes_written = xdr_getpos( _sink ) ;
00150 if( !bytes_written )
00151 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00152 _out.write( _buf, bytes_written ) ;
00153 }
00154
00155 void
00156 XDRStreamMarshaller::put_int( int val )
00157 {
00158 if( !xdr_setpos( _sink, 0 ) )
00159 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00160 if( !xdr_int( _sink, &val) )
00161 throw Error("Network I/O Error(1). This may be due to a bug in libdap or a\nproblem with the network connection.");
00162 unsigned int bytes_written = xdr_getpos( _sink ) ;
00163 if( !bytes_written )
00164 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00165 _out.write( _buf, bytes_written ) ;
00166 }
00167
00168 void
00169 XDRStreamMarshaller::put_vector( char *val, int num, Vector & )
00170 {
00171 if (!val)
00172 throw InternalErr(__FILE__, __LINE__,
00173 "Buffer pointer is not set.");
00174
00175
00176 put_int( num ) ;
00177
00178 if( !xdr_setpos( _sink, 0 ) )
00179 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00180
00181 if( !xdr_bytes( _sink, (char **)&val,
00182 (unsigned int *) &num,
00183 DODS_MAX_ARRAY) )
00184 {
00185 throw Error("Network I/O Error(2). This may be due to a bug in libdap or a\nproblem with the network connection.");
00186 }
00187
00188 unsigned int bytes_written = xdr_getpos( _sink ) ;
00189 if( !bytes_written )
00190 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00191 _out.write( _buf, bytes_written ) ;
00192 }
00193
00194 void
00195 XDRStreamMarshaller::put_vector( char *val, int num, int width, Vector &vec )
00196 {
00197 if (!val)
00198 throw InternalErr(__FILE__, __LINE__,
00199 "Buffer pointer is not set.");
00200
00201 put_int( num ) ;
00202
00203 if( !xdr_setpos( _sink, 0 ) )
00204 throw Error("Network I/O Error. Could not send byte data - unable to set stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00205
00206 BaseType *var = vec.var() ;
00207 if( !xdr_array( _sink, (char **)&val,
00208 (unsigned int *) & num,
00209 DODS_MAX_ARRAY, width,
00210 XDRUtils::xdr_coder( var->type() ) ) )
00211 {
00212 throw Error("Network I/O Error(2). This may be due to a bug in libdap or a\nproblem with the network connection.");
00213 }
00214
00215 unsigned int bytes_written = xdr_getpos( _sink ) ;
00216 if( !bytes_written )
00217 throw Error("Network I/O Error. Could not send byte data - unable to get stream position.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
00218 _out.write( _buf, bytes_written ) ;
00219 }
00220
00221 void
00222 XDRStreamMarshaller::dump(ostream &strm) const
00223 {
00224 strm << DapIndent::LMarg << "XDRStreamMarshaller::dump - ("
00225 << (void *)this << ")" << endl ;
00226 }
00227