1.4 Callback Chaining

A further enhancement to the callback idiom is the concept of callback chaining. This is particularly useful in the context of layered protocol stacks, such as those conforming to the OSI protocol model. An example of such a layered protocol stack is shown in Figure 1.1.

Figure 1.1: Callback Chaining for Protocol Stacks
\includegraphics[width=\textwidth]{introduction.figs/ProtocolStackCallbackChain.eps}

The example illustrates a request for remote data via the protocol stack. As the request is passed down the protocol stack, the request data is modified according to the protocol requirements - implementing packetisation, encryption, framing etc. At the same time, it is also possible to build up a callback chain for handing the response.

The presence of a callback chain for the transaction then allows the response data to be passed back up the stack using the callback mechanism. Within each callback method the received data may then be processed according to the layered protocol requirements. This is the reason that the simplified onCallback method returns a data Object - the returned value of each callback can be passed directly into the next callback in the chain for further processing.

While conceptually elegant, the process of dynamically constructing and then executing callback chains is a non-trivial exercise. Fortunately the Reaction framework provides a simple mechanism for managing this process called deferred events. These will be discussed in more detail in Chapter 5.