File: dmr-test.cc

package info (click to toggle)
libdap 3.20.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,568 kB
  • sloc: cpp: 50,809; sh: 41,536; xml: 23,511; ansic: 20,030; yacc: 2,508; exp: 1,544; makefile: 990; lex: 309; perl: 52; fortran: 8
file content (485 lines) | stat: -rw-r--r-- 13,207 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002,2003,2013 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

// Test the DMR parser

#include "config.h"

#include <stdint.h>
#include <cstring>

#include <fstream>
#include <memory>

#include "crc.h"

#include <GetOpt.h>

#include "BaseType.h"
#include "Array.h"
#include "D4Enum.h"

#include "DMR.h"
#include "D4Group.h"
#include "D4StreamUnMarshaller.h"
#include "chunked_ostream.h"
#include "chunked_istream.h"

#include "util.h"
#include "InternalErr.h"
#include "Error.h"

#include "D4ResponseBuilder.h"
#include "ConstraintEvaluator.h"

#include "D4ParserSax2.h"
#include "D4TestTypeFactory.h"
#include "TestCommon.h"

#include "D4ConstraintEvaluator.h"
#include "D4FunctionEvaluator.h"
#include "ServerFunctionsList.h"
#include "D4TestFunction.h"
#include "D4RValue.h"

#include "util.h"
#include "mime_util.h"
#include "debug.h"

int test_variable_sleep_interval = 0;   // Used in Test* classes for testing timeouts.

using namespace libdap;

/**
 * Open the named XML file and parse it, assuming that it contains a DMR.
 * @param name The name of the DMR XML file (or '-' for stdin)
 * @param debug True if the debug mode of the parse should be used
 * @param print Once parsed, should the DMR object be printed?
 * @return true if the parse worked, false otherwise
 */
DMR *
test_dap4_parser(const string &name, bool debug, bool print)
{
    D4TestTypeFactory *factory = new D4TestTypeFactory;
    DMR *dataset = new DMR(factory, path_to_filename(name));

    try {
        D4ParserSax2 parser;
        if (name == "-") {
            parser.intern(cin, dataset, debug);
        }
        else {
            fstream in(name.c_str(), ios_base::in);
            parser.intern(in, dataset, debug);
        }
    }
    catch(...) {
        delete factory;
        delete dataset;
        throw;
    }

    cout << "Parse successful" << endl;

    if (print) {
        XMLWriter xml("    ");
        dataset->print_dap4(xml, false);
        cout << xml.get_doc() << endl;
    }

    delete factory;
    dataset->set_factory(0);
    return dataset;
}

/**
 * Should the changing values - meant to mimic the DTS - be used?
 * @param dmr Set for this DMR
 * @param state True to use the DTS-like values, false otherwise
 */
void
set_series_values(DMR *dmr, bool state)
{
	if (state == true)
		dmr->root()->set_read_p(false);

	TestCommon *tc = dynamic_cast<TestCommon*>(dmr->root());
	if (tc)
		tc->set_series_values(state);
	else
		cerr << "Could not cast root group to TestCommon (" << dmr->root()->type_name() << ", " << dmr->root()->name() << ")" << endl;
}

/**
 * Call the parser and then serialize the resulting DMR after applying the
 * constraint. The persistent representation is written to a file. The file
 * is name '<name>_data.bin'.
 *
 * @param dataset
 * @param constraint
 * @param series_values
 * @return The name of the file that hods the response.
 */
string
send_data(DMR *dataset, const string &constraint, const string &function, bool series_values, bool ce_parser_debug)
{
    set_series_values(dataset, series_values);

    // This will be used by the DMR that holds the results of running the functions.
    // It's declared at this scope because we (may) need it for the code beyond the
    // function parse/eval code that immediately follows. jhrg 3/12/14
    D4TestTypeFactory d4_factory;
    unique_ptr<DMR> function_result(new DMR(&d4_factory, "function_results"));

	// The Function Parser
	if (!function.empty()) {
		ServerFunctionsList *sf_list = ServerFunctionsList::TheList();
		ServerFunction *scale = new D4TestFunction;
		sf_list->add_function(scale);

		D4FunctionEvaluator parser(dataset, sf_list);
		if (ce_parser_debug) parser.set_trace_parsing(true);
		bool parse_ok = parser.parse(function);
		if (!parse_ok)
			Error("Function Expression failed to parse.");
		else {
			if (ce_parser_debug) cerr << "Function Parse OK" << endl;

			parser.eval(function_result.get());

			// Now use the results of running the functions for the remainder of the
			// send_data operation.
			dataset = function_result.release();
		}
	}

    D4ResponseBuilder rb;
    rb.set_dataset_name(dataset->name());

    string file_name = dataset->name() + "_data.bin";
    ofstream out(file_name.c_str(), ios::out|ios::trunc|ios::binary);

	if (!constraint.empty()) {
		D4ConstraintEvaluator parser(dataset);
		if (ce_parser_debug)
		    parser.set_trace_parsing(true);
		bool parse_ok = parser.parse(constraint);
		if (!parse_ok)
			throw Error("Constraint Expression failed to parse.");
		else if (ce_parser_debug)
		    cerr << "CE Parse OK" << endl;
	}
    else {
    	dataset->root()->set_send_p(true);
    }

    rb.send_dap(out, *dataset, /*with mime headers*/ true, !constraint.empty());
    out.close();

    return file_name;
}

void
intern_data(DMR *dataset, /*const string &constraint,*/ bool series_values)
{
    set_series_values(dataset, series_values);

    // Mark all variables to be sent in their entirety. No CEs are used
    // when 'interning' variables' data.
    dataset->root()->set_send_p(true);
#if 0
    Crc32 checksum;
#endif
    dataset->root()->intern_data(/*checksum, *dataset, eval*/);
}

DMR *
read_data_plain(const string &file_name, bool debug)
{
    D4BaseTypeFactory *factory = new D4BaseTypeFactory;
    DMR *dmr = new DMR(factory, "Test_data");

    fstream in(file_name.c_str(), ios::in|ios::binary);

    // Gobble up the response's initial set of MIME headers. Normally
    // a client would extract information from these headers.
    remove_mime_header(in);

    chunked_istream cis(in, CHUNK_SIZE);

    // parse the DMR, stopping when the boundary is found.
    try {
        // force chunk read
        // get chunk size
        int chunk_size = cis.read_next_chunk();
        // get chunk
        char chunk[chunk_size];
        cis.read(chunk, chunk_size);
        // parse char * with given size
    	D4ParserSax2 parser;

    	// Mirror the behavior in D4Connect where we are permissive with DAP4
    	// data responses' parsing, as per Hyrax-98 in Jira. jhrg 4/13/16
    	parser.set_strict(false);

    	// '-2' to discard the CRLF pair
        parser.intern(chunk, chunk_size-2, dmr, debug);
    }
    catch(Error &e) {
    	delete factory;
    	delete dmr;
    	cerr << "Exception: " << e.get_error_message() << endl;
    	return 0;
    }
    catch(std::exception &e) {
    	delete factory;
    	delete dmr;
    	cerr << "Exception: " << e.what() << endl;
    	return 0;
    }
    catch(...) {
    	delete factory;
    	delete dmr;
    	cerr << "Exception: unknown error" << endl;
    	return 0;
    }

    D4StreamUnMarshaller um(cis, cis.twiddle_bytes());

    dmr->root()->deserialize(um, *dmr);

    delete factory;
    dmr->set_factory(0);
    return dmr;
}

static void usage()
{
    cerr << "Usage: dmr-test -p|s|t|i <file> [-c <expr>] [-f <function expression>] [-d -x -e]" << endl
            << "p: Parse a file (use \"-\" for stdin; if a ce or a function is passed those are parsed too)" << endl
            << "s: Send: parse and then 'send' a response to a file" << endl
            << "t: Transmit: parse, send and then read the response file" << endl
            << "i: Intern values (ce and function will be ignored by this)" << endl
            << "c: Constraint expression " << endl
            << "f: Function expression" << endl
            << "d: turn on detailed xml parser debugging" << endl
            << "D: turn on detailed ce parser debugging" << endl
            << "x: print the binary object(s) built by the parse, send, trans or intern operations." << endl
            << "e: use sEries values." << endl;
}

int
main(int argc, char *argv[])
{
    GetOpt getopt(argc, argv, "p:s:t:i:c:f:xdDeh?");
    int option_char;
    bool parse = false;
    bool debug = false;
    bool print = false;
    bool send = false;
    bool trans = false;
    bool intern = false;
    bool series_values = false;
    bool constrained = false;
    bool functional = false;
    bool ce_parser_debug = false;
    string name = "";
    string ce = "";
    string function = "";

    // process options

    while ((option_char = getopt()) != -1)
        switch (option_char) {
        case 'p':
            parse = true;
            name = getopt.optarg;
            break;

        case 's':
        	send = true;
        	name = getopt.optarg;
        	break;

        case 't':
        	trans = true;
        	name = getopt.optarg;
        	break;

        case 'i':
        	intern = true;
        	name = getopt.optarg;
        	break;

        case 'c':
        	constrained = true;
        	ce = getopt.optarg;
        	break;

        case 'f':
        	functional = true;
        	function = getopt.optarg;
        	break;

        case 'd':
            debug = true;
            break;

        case 'D':
            ce_parser_debug = true;
            break;

        case 'x':
            print = true;
            break;

        case 'e':
        	series_values = true;
        	break;

        case '?':
        case 'h':
            usage();
            return 0;

        default:
            cerr << "Error: ";
            usage();
            return 1;
        }

    if (! (parse || send || trans || intern)) {
        cerr << "Error: ";
        usage();
        return 1;
    }

    try {
		if (parse) {
			DMR *dmr = test_dap4_parser(name, debug, print);

			// The CE Parser
			if (!ce.empty()) {
				try {
					D4ConstraintEvaluator parser(dmr);
					if (ce_parser_debug) parser.set_trace_parsing(true);
					bool parse_ok = parser.parse(ce);
					if (!parse_ok)
						cout << "CE Parse Failed" << endl;
					else
						cout << "CE Parse OK" << endl;
				}
				catch (Error &e) {
					cerr << "CE Parse error: " << e.get_error_message() << endl;
				}
				catch (...) {
					cerr << "Ce Parse error: Unknown exception thrown by parser" << endl;
				}
			}

			// The Function Parser
			if (!function.empty()) {
				try {
					ServerFunctionsList *sf_list = ServerFunctionsList::TheList();
				    ServerFunction *scale = new D4TestFunction;
				    sf_list->add_function(scale);

					D4FunctionEvaluator parser(dmr, sf_list);
					if (ce_parser_debug) parser.set_trace_parsing(true);
					bool parse_ok = parser.parse(function);
					if (!parse_ok)
						cout << "Function Parse Failed" << endl;
					else
						cout << "Function Parse OK" << endl;
				}
				catch (Error &e) {
					cerr << "Function Parse error: " << e.get_error_message() << endl;
				}
				catch (...) {
					cerr << "Function Parse error: Unknown exception thrown by parser" << endl;
				}
			}

			delete dmr;
		}

		if (send) {
        	DMR *dmr = test_dap4_parser(name, debug, print);

        	string file_name = send_data(dmr, ce, function, series_values, ce_parser_debug);
        	if (print)
        		cout << "Response file: " << file_name << endl;
        	delete dmr;
        }

        if (trans) {
        	DMR *dmr = test_dap4_parser(name, debug, print);
        	string file_name = send_data(dmr, ce, function, series_values, ce_parser_debug);
         	delete dmr;

        	DMR *client = read_data_plain(file_name, debug);

        	if (print) {
        		XMLWriter xml;
        		// received data never have send_p set; don't set 'constrained'
        		client->print_dap4(xml, false /* constrained */);
        		cout << xml.get_doc() << endl;

				cout << "The data:" << endl;
        	}

        	// if trans is used, the data are printed regardless of print's value
    		client->root()->print_val(cout, "", false);
    		cout << endl;

        	delete client;
        }

        if (intern) {
        	DMR *dmr = test_dap4_parser(name, debug, print);
        	intern_data(dmr, /*ce,*/ series_values);

        	if (print) {
        		XMLWriter xml;
        		dmr->print_dap4(xml, false /*constrained*/);
        		cout << xml.get_doc() << endl;

				cout << "The data:" << endl;
        	}

        	// if trans is used, the data are printed regardless of print's value
    		dmr->root()->print_val(cout, /*space*/"", false);
    		cout << endl;

        	delete dmr;
        }
    }
    catch (Error &e) {
        cerr << "Error: " << e.get_error_message() << endl;
        return 1;
    }

    return 0;
}