I want to give you a practical example of using a call back along with a couple of procedures that might be of use when displaying lists of information on a Web page using CGIDEV2.
But let's get started with the concept of call back. Call back is where a program issues a call to a procedure that, in turn, issues a call back to a procedure in the calling program. Clear as mud, right? Figure 1 should make it a little easier to understand. Your program issues a call to a standard procedure in a service program. But there is a little bit of logic that must be performed by this standard procedure that may be different on every call, depending on the caller. Instead of coding a lot of complex if/else logic, the standard procedure will call a procedure back in the calling program. Of course, the "call back" procedure does not have to be back in the calling program just as long as it is accessible as a bound call (i.e. it can be in another module or service program).
Figure 1: Call back
Usually, the call back technique is used at a fairly low level of programming, such as using the C functions qsort and bsearch, both of which are extremely well documented in the Redbook "Who Knew You Could Do That with RPG IV?". But you do not need to go to a very low level to make use of the technique. Let's look at an example.
An ExampleFigure 2 shows a web page that displays a list of products. Above the list are a couple of options where the user can select whether the list should be displayed by row or by column and how many columns should be displayed. They simply select the two radio buttons and click the change button. All of the logic for handling and processing the selection is written in a couple of subprocedures: all you have to do is write a procedure that places the information for a product and follow a couple of rules with your HTML.
Figure 2: List manipulation on a Web page
Figure 3 shows the code for the called program. The main points to note are:
There are a few rules that you must follow when defining the HTML (shown in Figure 4) for the list to be displayed. Remember, this list is going to be written by a standard routine.
The HTML must contain seven sections that are written by the FormatList procedure:
HeaderColumn and DataColumn are the two sections that require all the work. The rest of the sections will, for the most part, remain the same unless you need to customize any of the table or row attributes.
Figure 5 shows the prototypes for the two standard procedures and the call back procedure.
D FormatSelect PR ExtProc('FORMATSELECT') D ProgramName 10A Const D AllColumns 10I 0 Const Options(*NoPass) D FormatList PR ExtProc('FORMATLIST') D NumberOfRows 10I 0 Const D CallProc * ProcPtr Const D ColumnWidth 10I 0 Const Options(*NoPass) D SetColumnData PR D ForColumn 10I 0 Const
Figure 5: Prototypes for the procedures
These are the main points to consider:
And the restAnd then there is the code for the standard procedures and HTML documents. But one of the great things about ILE is that you do not care about the code as long as it works – just as you know that all those CGIDEV2 procedures work. So all you have to do is download the code and install it .
Now you have an idea of how call backs can be used and you have added another tool to your programming tool belt.
And you have the basis of a little utility that allows your users to customize lists the way they want.
About the author: Paul Tuohy is CEO of ComCon, an iSeries consulting company. He is the author of Re-Engineering RPG Legacy Applications and is one of the quoted industry experts in the IBM Redbook "Who Knew You Could Do That With RPG IV?"