| 4 Getting ready to write your client |
An OPeNDAP-enabled client application creates a connection to the
remote server using the Connect class, and then issues
requests to the remote server through the Connect class
methods. Please refer to the Geturl.java and
geturl.cc sources as examples of command-line
based DAP client applications written in Java and C++ respectively.
Most of the software is boilerplate. Following are sections of the Geturl.java client application, later a description of the same example in C++ will be provided. Again, please refer to the complete source listings referenced above.
DConnect url = null;
try {
url = new DConnect(nextURL, accept_deflate);
}
This code snippet instantiates a new instance of the DConnect class, passing the URL referencing the remote data server, and a boolean flag indicating that the client can accept responses from the server which are compressed.
if (get_data) {
if ((cexpr==false) && (nextURL.indexOf('?') == -1)) {
System.err.println("Must supply a constraint expression with -D.");
continue;
}
for (int j=0; j<times; j++) {
try {
StatusUI ui = null;
if (gui)
ui = new StatusWindow(nextURL);
DataDDS dds = url.getData(expr, ui);
processData(url, dds, verbose, dump_data, accept_deflate);
}
catch (DODSException e) {
System.err.println(e);
System.exit(1);
}
catch (java.io.FileNotFoundException e) {
System.err.println(e)
System.exit(1);
}
catch (Exception e) {
System.err.println(e);
e.printStackTrace();
System.exit(1);
}
}
}
This compound statement block initiaties a data request to the remote
server. The DConnect method getData forms the data
request to the remote server by appending the string, expr,
containing the constraint-expression, onto the URL used in creating
the initial DConnect to the remote site. The parameter,
ui, provides an optional StatusWindow object to provide
the status of the current request to the client.
catch (DODSException e) {
System.err.println(e);
System.exit(1);
}
catch (java.io.FileNotFoundException e) {
System.err.println(e);
System.exit(1);
}
catch (Exception e) {
System.err.println(e);
e.printStackTrace();
System.exit(1);
}
Completing the try block is a series of catch blocks that catch exceptions thrown by the DAP library code, and Java input/output and general exceptions.
The C++ toolkit provides similar functionality as the Java toolkit though the parameters to the individual Connect methods may vary. The C++ client application geturl.cc uses the similar C++ toolkit classes as the Java toolkit to implement the Geturl client application:
string name = argv[i]; Connect url(name, trace, accept_deflate);
This code fragment declares an instance of the Connect class, passing the URL referencing the remote data server, and a boolean flag indicating that the client can accept responses from the server that are compressed.
else if (get_data) {
if (expr.empty() && name.find('?') == string::npos)
expr = "";
for (int j = 0; j < times; ++j) {
DataDDS dds;
try {
DBG(cerr << "URL: " << url->URL(false) << endl);
DBG(cerr << "CE: " << expr << endl);
url->request_data(dds, expr);
if (verbose)
fprintf( stderr, "Server version: %s\n",
url->get_version().c_str() ) ;
print_data(dds, print_rows);
}
catch (Error &e) {
e.display_message();
continue;
}
}
}
This compound statement block initiaties a data request from the remote
server. The Connect method request_data forms the data request
to the remote server by appending the string, expr, containing the
constraint-expression, onto the URL used in creating the initial
Connect to the remote site.
catch (Error &e) {
e.display_message();
continue
}
Completing the try block is a catch block that picks up exceptions thrown by the DAP library code. The DAP C++ library throws several types of exceptions, the most common of which are Error and InternalErr. All of the exceptions are either instances of Error or are specializations of it, so catching just Error will get everything.
| 4 Getting ready to write your client |