Figure 13.1: Glish Wrapper for FFT Client
Suppose we want to create an ``FFT server": a Glish client that when sent a numerically-valued fft event computes the FFT of the array of data and returns the result as an answer event. The result consists of a record with two fields, real and imag, arrays of the real and imaginary parts of the Fourier transform.
Assume we have a function fft available for doing the actual transformation and want to ``wrap" a Glish client interface around this function. Figure 13.1 shows how we would do so.
First we create a Client object using the idiom discussed in § 13.5. We then enter the event-loop, blocking until a new event is ready (NextEvent returns a nil pointer when the client should terminate).
If the event's name is fft then we extract the event's value, convert it to double if it is not already, and extract its length into num. We then use DoublePtr to get a pointer to the actual array of double-precision elements. In order to call fft we need to also pass it arrays where it should put its results, so we create real and imag. After computing the FFT we create in r a Glish record value to hold the two arrays, and assign them to r's real and imag fields. We then send this aggregate value as a Glish event with the name answer. Now that we're done with r we Unref() it to reclaim its memory. This will automatically result in real and imag's memory being reclaimed too. We don't need to Unref() the GlishEvent pointed to by e because the next call to NextEvent automatically does so.
Finally, if the event wasn't fft then we inform the Client library that we don't recognize this particular event.
To compile this example we use:
C++ -I$GLISHROOT/include -c fft_server.ccwhere C++ is the local C++ compiler (typically g++ or CC) and $GLISHROOT is the path to the Glish installation directory (see § 14.7).
We then link the example using:
C++ -o fft_server fft_server.o \ -L$GLISHROOT/lib/$ARCH -lglish -lsos -lmwhere $ARCH indicates the local architecture (again, see § 14.7). On some platforms, other libraries may be required for things like sockets, for example under Solaris -lsocket and -lnsl are also required.