In our previous article, we discussed the basic design criteria for a buffer style Open Access (OA) handler that would let us access a currency exchange Web service using conventional RPG op-codes. As promised, this article describes the implementation of that handler and shows how an RPG program uses it.
Just to highlight the impact, or rather the lack of impact, that using OA has on the style of the RPG code, we’ll start by examining a test program that will use our new handler. Let's just jump straight into the code. As you’ll see, it won’t take us long.
Using the Currency Service Handler
Below you’ll see the entire program we’re going to use to test the new handler. Study it for as long as you like, and the only thing that you’ll see that’s any different from a conventional RPG program accessing a keyed disk file is the keyword HANDLER on the F spec. This keyword is how we associate the handler program (CURCONVHND) with all I/O operations for the currency file (CURRRATES).
The DDS for the CURRRATES file is very simple:
A R CURRRATESR
A FROMCNTRY 3A
A TOCNTRY 3A
A RATE 15P 5
A K FROMCNTRY
A K TOCNTRY
Our handler has been written as a program as it makes it a little simpler for you to recreate the programs should you choose to download them and try it for yourself. But it could equally have been a procedure in a service program, in which case the handler keyword might have looked like this:
Handler('MYSERVPGM(CURCONVHND)')
Here’s the source of the test program CURHNDTST:
// Tests an RPG Open Access SOAP Web service handler
FCurrRates IF E K DISK Handler('CURCONVHND')
D from s Like(fromCntry)
D to s Like(toCntry)
/free
from = 'USD';
to = 'CAD';
Chain (from: to) CurrRates;
If %Found(CurrRates);
Dsply ('Conversion rate from ' + fromCntry +
' to ' + toCntry + ' is ' + %Char(rate));
Else;
Dsply ('No record found for ' + from + ' to ' + to );
EndIf;
*InLr = *On;
Return;
/End-free
As you can see, nothing is unusual about this code. Perhaps the style of the CHAIN opcode may not be what you’re familiar with, but this is just vanilla 21st-century RPG. The program sets the key values, performs the CHAIN to the file and then displays one of two messages depending on whether or not the record was found. To test it with different currency codes, you change the source and recompile.
If you download the source from our website, you’ll see that we’ve also supplied a more sophisticated test program that lets you enter multiple currency codes and see the results. We just wanted to keep this example program as simple as possible.
This simple program conveys a powerful lesson—one that’s important to understand when considering the use of OA. That lesson is that there really is no difference between coding an RPG program that will take advantage of an OA handler and one that will use a conventional disk file—or indeed any other kind of file. That’s the whole point of OA.
The Handler Program
Now that you’ve seen the test program, let’s look at the handler that performs the “magic.” Before we do that, please note that we’re not going to describe the mechanics of calling the actual Web service itself. Other articles in this magazine appear on that topic, and you can find many articles on how to use HTTPAPI on Scott Klement’s website so we will leave you to explore that for yourself. If there’s enough interest, we may do a separate article on using this fabulous piece of software at some point in the future. Just let us know. Instead, we’ll focus on the details of the handler and how it interfaces with the RPG runtime.
Referring to Code Sample 1, the first thing you’ll notice at (A) is that we’ve defined an F spec for the currency rates file. But we added the keyword TEMPLATE. Because we’re writing a buffer handler, we need to know the layout of the records. We could have used externally described data structures, but somehow it seemed more natural to use the F spec approach and the keyword LIKEREC to define the key and record structures. You’ll see those in action in just a moment. Note that because we used the TEMPLATE keyword, no attempt will be made to open this file. RPG knows that we simply want to use its details for reference purposes.
At (B), you can see the procedure interface for the program, which is the equivalent to the *ENTRY PLIST for those of you not yet using procedure interfaces. There is only one parameter defined that we’ve called info. Even though it’s only a single parameter, there’s a lot of stuff in there. We’ve used the LIKEDS keyword to define the it. The referenced DS (QrnOpenAccess_T) is supplied by IBM in the source file QRNOPENACC, which is copied into the program at (C).
The record layout and the key structure are defined at (D). Both use the LIKEREC keyword to reference the record layout in the currency rates file. Notice that both structures are defined as based. They are based on pointers that are supplied in the info data structure that was passed to us by the RPG runtime. While you may be unfamiliar with the use of pointers, there’s nothing to worry about here. We’re not going to do any pointer manipulation; we simply use the pointers that RPG passes to us as the basing pointers for the buffers we need to access. That means we can access the data the minute our handler is called.
Notice when defining the currency file record layout, we specified that it was the input layout we wanted. This is one of the things you must get used to when writing OA handlers. The data we output from the handler is seen as input by the user program. In other words, we’re going to have to move values into the input record layout so that the RPG program using the handler can see the data as the result of the CHAIN.
Processing Handler Requests
If you jump down to (E) in the source, you’ll see the beginning of the calculation specifications that handle the I/O requests from RPG. The first test we do is to see if the operation we’re being asked to perform is a CHAIN, which is determined by comparing the operation code (info.rpgOperation) with the RPG-supplied constant QrnOperation_CHAIN. If it is a CHAIN, then we perform the DoChain function. We’ll look at it in a more detail later. Note that the variables rpgOperation and rpgStatus (used later) and the constants such as QrnOperation_CHAIN are all defined for us in the IBM-supplied member we /Copy’ed in at (C) above.
If it’s not a CHAIN request, then we check to see if we’re being asked to process an OPEN (F). It’s important to understand that you’ll receive an open request even if the file is not being manually opened but is being opened by the RPG cycle. Similarly, you must handle CLOSE (G) operations even if there’s no explicit close operation coded.
This particular handler hasn’t been coded to support concurrent usage within the same job (i.e., two programs in the same job should not attempt to have this handler open at the same time). That is the purpose of the inUse flag. We use it to ensure a second open will be rejected as an error. Since it’s highly unlikely that we would have concurrent users of our currency rate handler this doesn’t present a problem in practical terms. In later examples, we’ll share how the handler can easily be coded to allow concurrent usage.
The section of code at (H) is one that will appear in almost all OA handlers. It deals with any operation requests that our handler doesn’t know how to process. How could that happen? Think back to the test program we mentioned before. It declares the currency rate file is being a keyed indexed file. As it happens, RPG would normally consider valid any number of operation codes for such a file—such as SETLL, READE, etc. In the context of this particular handler such operations really don’t make any sense and therefore we’ve made no attempt to support them. But the RPG compiler has no way of knowing this and therefore would happily allow their use. It’s up to us to make sure that at runtime we don’t permit any operations that the handler cannot actually execute. That’s what you’re seeing here. We’re requesting RPG to issue the generic I/O error response by setting the RPG status code (info.rpgStatus). But to aid in debugging the problem, we’re also displaying a message that will appear in the log. There are alternative approaches to this kind of error handling, such as simply sending an escape message back to our caller and letting RPG set the status, and we’ll look at those later in this series.
Performing the CHAIN
Let’s look briefly now at the code in the DoChain subprocedure (I) that we mentioned earlier. As we said, we’re not going into detail here on the Web service portion of the code; we’ll only cover those items related to processing the CHAIN.
Notice that we start by setting the field charRate equal to the not found status (J). If the Web service returns an exchange rate, it will replace this value. By setting the not found status in advance we can simplify the testing of the values returned by the Web service and focus on trapping the value we want. So if there’s an error or timeout when we call the Web service, or there simply is no exchange rate for the requested from and to country pair, we don’t have to perform any elaborate error-handling logic. We can simply rely on the fact that if we don’t get the answer we want then the status will be “not found.” In a real handler of this kind, we’d probably want to report failures in the Web service so they can be corrected, but for demonstration purposes this approach works well.
The part of the subprocedure that affects their interaction with RPG can be seen at (K). Having invoked the Web service and gotten a response back, we can now look at the charRate field and see if a value has been placed there. If it has, then we simply convert it from the character representation in which the Web service supplies it into the packed decimal 15,5 format that must be placed into the rate field in the input buffer (currRatesD.rate). This is the input field that will be received by the RPG program. Since we’re now building the input record for the RPG program we also need to copy the key values fromCntry and toCntry we used in the Web service lookup from their key structure area into the record input buffer. Once the buffer has been populated, we can set the found indicator info.found. This is a direct representation of the %Found value associated with this particular file. If we didn’t retrieve a value, then we must set the indicator off before we finally return back to our caller.
That’s really all there is to it. It’s not that complicated to write a handler of this style in part because we can restrict the operations that can be performed to a simple CHAIN. In this context, that’s really the only operation that makes sense.
What’s Missing?
If you were implementing this handler for production use, you include a few other considerations.
Take for example the value returned by the Web service. What if there were problems with the service such that a non-numeric response was returned? The current code would blow up with an exception on the %DECH BIF. We could trap that with a MONITOR op-code but what then? The minimal answer would be to ensure that the not found status is set and a message is written to the log.
As noted in the text a “real” handler should also differentiate between “not found” due to the Web service informing us that there’s no conversion rate for the selected currencies and “not found” due to a failure to connect to the service. The former is an expected error; the latter is clearly not.
What if we wanted to support SETLL and READE so that we could retrieve all rates for a particular currency? If the Web service itself doesn’t support this functionality, how would you implement it? Something to think about.
Next Time
In the next article in this series, we’ll return to the topic of IFS handlers and demonstrate a simple handler for reading IFS files. Until then, have fun with OA and if you have any questions or suggestions for future articles in the series, please let us know.