Java and SNA: A case study
By Matt MacKinnon, Adam King, David Kaminsky2003-05-24
Classic Transaction #1: The Pipe
To introduce the API, we'll start by examining the first classic transaction: the Pipe. We present the example by interleaving code with commentary.
We based our transaction code on that given in CPI-C Programming in C, an excellent reference written by John Q. Walker and Peter J. Schwaller. For further information on CPI-C, we recommend the book highly.
First, we import the CPIC package, our Java/CPI-C bindings. A CPIC object is the interface to our LU6.2 code. It contains many constants defined in CPI-C, such as the length of a conversation ID, along with methods that are passed through to the native CPI-C calls.
The programmer need only declare one CPIC object per class. Java will load the dynamic link library (DLL) containing the native methods when that CPIC object is instantiated.
|
Each type of parameter has its own class, and each of these classes has associated constants defined as class variables. For example, the CPICReturnCode class has the success return code, CM_OK, defined in it.
There were two major reasons for having a class for each type of parameter. Most importantly, because Java passes all parameters by value, there was no way to return data in simple types, such as integer. If we pass an object as a parameter to a method, the method can set a variable in that object, thus returning data to the caller. The second reason to use objects is to encapsulate constants within the objects that understand those constants--a standard information-hiding technique.
|
The CPI-C send function expects a C-language buffer--that is, allocated space of no specific type. Unlike C, Java has no facility to allocate untyped memory; besides primitives, everything in Java is an object. So whatever the program wants to send must be converted from its object type into an C-style array of bytes. In fact, for each call, the CPIC library must convert from the Java types to a C type.
Java does provide methods that facilitate such conversions. For example, Java can convert a String into a Java array of bytes. While an array of bytes is an object in Java, Java allows you to extract the data--the bytes--from an array of bytes with a native method.
So the programmer must convert his application data into an array of bytes; our code does not do automatic marshalling. While the conversion is not onerous, as we demonstrate below, we are considering augmenting our code to provide this function.
|
Like buffer processing, the CPI-C native calls expect symbolic destination names to be C-strings, not Java Strings. Our tool kit automatically converts them from Java Strings to a C-strings as necessary. In general, automatic conversion is possible when the tool kit expects a specific Java type.
The conversation ID is a Java array of bytes that is converted automatically by our tool kit to a C array of bytes--that is, a simple block of bytes.
|
Now the programmer is ready to make the CPIC calls almost exactly as he would in C. The only noteworthy differences are that he prefaces every CPIC call with the name of the CPIC object and that none of the parameters are prefixed by the pass by reference (&) symbol.
|
That's the end of the client, so we'll turn our attention to the server. The server initializes itself, accepts a conversation, receives some data, and prints some diagnostic information. The code is similar to the client code, so we'll omit discussion of some of the redundant details.
As in the client, we instantiate classes to hold the CPI-C parameters, many of which have only an integer as instance data; recall that by using objects, we can mimic call by reference. We also allocate a byte array to hold the received data.
|
The CPI-C receive call returns a Java array of bytes, while the Pipe transaction expects a String. The programmer can translate the array of bytes into a String by using the String class-constructor that takes an array of bytes as an argument.
|
Tutorial Pages:
» Java and SNA: A case study
» The Java/CPI-C API
» Classic Transaction #1: The Pipe
» Classic Transaction #5: The Database Update
» Native Calls
» Diversion: Native Calls and Applets
» Performance
» CPIC Calls That Have Been Implemented
» Conclusions
» Footnotes
First published by IBM DeveloperWorks
