49 char *XDRStreamMarshaller::_buf = 0;
51 #define XDR_DAP_BUFF_SIZE 256
61 XDRStreamMarshaller::XDRStreamMarshaller(ostream &out,
bool checksum,
bool write_data) :
62 _sink(0), _out(out), _md5(0), _write_data(write_data), _checksum_ctx_valid(false)
67 throw Error(
"Failed to allocate memory for data serialization.");
73 _md5 =
reinterpret_cast<MD5_CTX*
>(
new char[
sizeof(MD5_CTX)]);
77 XDRStreamMarshaller::XDRStreamMarshaller() :
80 throw InternalErr(__FILE__, __LINE__,
"Default constructor not implemented.");
83 XDRStreamMarshaller::XDRStreamMarshaller(
const XDRStreamMarshaller &m) :
84 Marshaller(m), _sink(0), _out(cout)
86 throw InternalErr(__FILE__, __LINE__,
"Copy constructor not implemented.");
90 XDRStreamMarshaller::operator=(
const XDRStreamMarshaller &)
92 throw InternalErr(__FILE__, __LINE__,
"Copy operator not implemented.");
114 throw InternalErr( __FILE__, __LINE__,
"checksum_init() called by checksum is not enabled.");
116 if (MD5_Init(_md5) == 0)
117 throw Error(
"Error initializing the checksum buffer.");
119 _checksum_ctx_valid =
true;
130 throw InternalErr( __FILE__, __LINE__,
"checksum_init() called by checksum is not enabled.");
132 if (!_checksum_ctx_valid)
133 throw InternalErr( __FILE__, __LINE__,
"Invalid checksum context.");
138 _checksum_ctx_valid =
false;
140 vector<unsigned char> md(MD5_DIGEST_LENGTH);
141 if (MD5_Final(&md[0], _md5) == 0)
142 throw Error(
"Error computing the checksum.");
145 oss.setf ( ios::hex, ios::basefield );
146 for (
int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
147 oss << setfill(
'0') << setw(2) << (
unsigned int)md[i];
156 throw InternalErr( __FILE__, __LINE__,
"checksum_init() called by checksum is not enabled.");
158 if (!_checksum_ctx_valid)
159 throw InternalErr( __FILE__, __LINE__,
"Invalid checksum context.");
161 if (MD5_Update(_md5, data, len) == 0) {
162 _checksum_ctx_valid =
false;
163 throw Error(
"Error computing the checksum.");
173 DBG( std::cerr <<
"put_byte: " << val << std::endl );
175 if (!xdr_setpos( _sink, 0 ))
176 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.");
178 if (!xdr_char(_sink, (
char *) &val))
179 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.");
181 unsigned int bytes_written = xdr_getpos( _sink );
183 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.");
185 _out.write(_buf, bytes_written);
195 if (!xdr_setpos( _sink, 0 ))
196 throw Error(
"Network I/O Error. Could not send int 16 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.");
199 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.");
201 unsigned int bytes_written = xdr_getpos( _sink );
203 throw Error(
"Network I/O Error. Could not send int 16 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.");
205 _out.write(_buf, bytes_written);
215 if (!xdr_setpos( _sink, 0 ))
216 throw Error(
"Network I/O Error. Could not send int 32 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.");
219 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.");
221 unsigned int bytes_written = xdr_getpos( _sink );
223 throw Error(
"Network I/O Error. Could not send int 32 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.");
225 _out.write(_buf, bytes_written);
235 if (!xdr_setpos( _sink, 0 ))
236 throw Error(
"Network I/O Error. Could not send float 32 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.");
238 if (!xdr_float(_sink, &val))
239 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.");
241 unsigned int bytes_written = xdr_getpos( _sink );
243 throw Error(
"Network I/O Error. Could not send float 32 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.");
245 _out.write(_buf, bytes_written);
255 if (!xdr_setpos( _sink, 0 ))
256 throw Error(
"Network I/O Error. Could not send float 64 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.");
258 if (!xdr_double(_sink, &val))
259 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.");
261 unsigned int bytes_written = xdr_getpos( _sink );
263 throw Error(
"Network I/O Error. Could not send float 64 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.");
265 _out.write(_buf, bytes_written);
275 if (!xdr_setpos( _sink, 0 ))
276 throw Error(
"Network I/O Error. Could not send uint 16 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.");
279 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.");
281 unsigned int bytes_written = xdr_getpos( _sink );
283 throw Error(
"Network I/O Error. Could not send uint 16 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.");
285 _out.write(_buf, bytes_written);
295 if (!xdr_setpos( _sink, 0 ))
296 throw Error(
"Network I/O Error. Could not send uint 32 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.");
299 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.");
301 unsigned int bytes_written = xdr_getpos( _sink );
303 throw Error(
"Network I/O Error. Could not send uint 32 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.");
305 _out.write(_buf, bytes_written);
315 int size = val.length() + 8;
318 char *str_buf = (
char *) malloc(size);
321 throw Error(
"Failed to allocate memory for string data serialization.");
324 XDR *str_sink =
new XDR;
325 xdrmem_create(str_sink, str_buf, size, XDR_ENCODE);
327 if (!xdr_setpos( str_sink, 0 )) {
330 throw Error(
"Network I/O Error. Could not send string 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.");
333 const char *out_tmp = val.c_str();
334 if (!xdr_string(str_sink, (
char **) &out_tmp, size)) {
337 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.");
340 unsigned int bytes_written = xdr_getpos( str_sink );
341 if (!bytes_written) {
344 throw Error(
"Network I/O Error. Could not send string 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.");
347 _out.write(str_buf, bytes_written);
366 throw Error(
"Network I/O Error. Could not send opaque data - length of opaque data larger than allowed");
368 if (!xdr_setpos( _sink, 0 ))
369 throw Error(
"Network I/O Error. Could not send opaque 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.");
371 if (!xdr_opaque(_sink, val, len))
372 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.");
374 unsigned int bytes_written = xdr_getpos( _sink );
376 throw Error(
"Network I/O Error. Could not send opaque 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.");
378 _out.write(_buf, bytes_written);
389 if (!xdr_setpos( _sink, 0 ))
390 throw Error(
"Network I/O Error. Could not send int 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.");
392 if (!xdr_int(_sink, &val))
393 throw Error(
"Network I/O Error(1). Could not send int data.\nThis may be due to a bug in libdap or a\nproblem with the network connection.");
395 unsigned int bytes_written = xdr_getpos( _sink );
397 throw Error(
"Network I/O Error. Could not send int 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.");
399 _out.write(_buf, bytes_written);
406 throw InternalErr(__FILE__, __LINE__,
"Could not send byte vector data. Buffer pointer is not set.");
416 unsigned int add_to = 8;
418 char *byte_buf = (
char *) malloc(num + add_to);
420 throw Error(
"Failed to allocate memory for byte vector data serialization.");
423 XDR *byte_sink =
new XDR;
424 xdrmem_create(byte_sink, byte_buf, num + add_to, XDR_ENCODE);
426 if (!xdr_setpos( byte_sink, 0 )) {
429 throw Error(
"Network I/O Error. Could not send byte vector 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.");
432 if (!xdr_bytes(byte_sink, (
char **) &val, (
unsigned int *) &num, num + add_to)) {
435 throw Error(
"Network I/O Error(2). Could not send byte vector data.\nThis may be due to a bug in libdap or a\nproblem with the network connection.");
438 unsigned int bytes_written = xdr_getpos( byte_sink );
439 if (!bytes_written) {
442 throw Error(
"Network I/O Error. Could not send byte vector 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.");
445 _out.write(byte_buf, bytes_written);
455 throw InternalErr(__FILE__, __LINE__,
"Buffer pointer is not set.");
464 int use_width = width;
470 int size = (num * use_width) + 4;
473 char *vec_buf = (
char *) malloc(size);
476 throw Error(
"Failed to allocate memory for vector data serialization.");
479 XDR *vec_sink =
new XDR;
480 xdrmem_create(vec_sink, vec_buf, size, XDR_ENCODE);
483 if (!xdr_setpos( vec_sink, 0 )) {
486 throw Error(
"Network I/O Error. Could not send vector 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.");
492 if (!xdr_array(vec_sink, (
char **) &val, (
unsigned int *) &num, size, width,
XDRUtils::xdr_coder(var->
type()))) {
495 throw Error(
"Network I/O Error(2). Could not send vector data.\nThis may be due to a bug in libdap or a\nproblem with the network connection.");
499 unsigned int bytes_written = xdr_getpos( vec_sink );
500 if (!bytes_written) {
503 throw Error(
"Network I/O Error. Could not send vector 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.");
507 _out.write(vec_buf, bytes_written);
516 strm <<
DapIndent::LMarg <<
"XDRStreamMarshaller::dump - (" << (
void *)
this <<
")" << endl;
virtual ~XDRStreamMarshaller()
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
Holds a one-dimensional collection of DAP2 data types.
virtual void put_int16(dods_int16 val)
virtual void reset_checksum()
Type type() const
Returns the type of the class instance.
virtual void put_uint16(dods_uint16 val)
virtual void put_float64(dods_float64 val)
const int XDR_DAP_BUFF_SIZE
A class for software fault reporting.
virtual void put_byte(dods_byte val)
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
virtual string get_checksum()
virtual void put_int(int val)
virtual void put_float32(dods_float32 val)
virtual void put_opaque(char *val, unsigned int len)
virtual void put_str(const string &val)
void delete_xdrstdio(XDR *xdr)
static ostream & LMarg(ostream &strm)
virtual void checksum_update(const void *data, unsigned long len)
The basic data type for the DODS DAP types.
abstract base class used to marshal/serialize dap data objects
virtual void put_vector(char *val, int num, Vector &vec)
static xdrproc_t xdr_coder(const Type &t)
Returns a function used to encode elements of an array.
A class for error processing.
virtual void put_int32(dods_int32 val)
virtual void put_uint32(dods_uint32 val)
virtual void put_url(const string &val)