Example: Passing an Array
In this example, the client creates an array of values and passes the array to the server. The server processes the array and sends back an object representing an array to the client where it is printed, as shown in
Figure 3-2: Passing an Array. The returned object must be explicitly cast to the desired data type (in this example, the object is cast to a floating-point array).
Data is passed from client to server, and the server returns a data object, which is then printed by the client.
Client application passes an array to the server, retrieves a result, and prints it
import com.visualnumerics.jwave.JWaveExecute;
import com.visualnumerics.jwave.JWaveConnection;
public class PassArray {
public static void main(String[] args) {
JWaveExecute command = null;
// Create a simple array of data values.
float arr[] = new float[10];
for (int k=0; k<arr.length; k++) {
arr[k]=k;
}
try {
// Pass the array parameter to the server to use with
// the PASSARR JWAVE wrapper function.
command = new JWaveExecute(”PASSARR”);
command.setParam(”ARRAY”, arr);
command.execute();
// Get the data Object returned from the server and cast
// to float array.
float d[]= (float[]) command.getReturnData(”DATA”);
//Print the returned array.
for (int j=0; j<d.length; j++) {
System.out.println(d[j]);
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
if (command != null) {
JWaveConnection c = command.getConnection();
if (c != null) c.shutdown();
}
}
}
JWAVE wrapper function receives the array, changes it, and returns it to the client
FUNCTION PASSARR, client_data
; Unpack parameters and data
arr = GETPARAM(client_data, ’ARRAY’, /Value, Default=[1.,2.,3.])
; change the array
mydata = arr * 1.5
; return the changed array and ensure FLOAT data type
RETURN, FLOAT(mydata)
END
When this Java program is executed, the following output is printed on the client:
% java PassArray
0.0
1.5
3.0
4.5
6.0
7.5
9.0
10.5
12.0
13.5
%