Batching

Batching is a sample program that shows how several notifications can be sent together as a batch. It modifies TReceiver to enable receiving the batched notifications. This can reduce communications traffic between a sender and receiver, and allow receivers to optimize their responses to notification.

This sample is an extension of CustomNotification.

Running the sample

Execute BatchingSApp. As the notifications are sent and received, they are batched for the receiver. The receiver extracts the old value from the first notification in the batch, discards the rest, and displays the old and current values to the shell window.

Files and classes

TSender is defined in Sender.h and Sender.C. It is unchanged from the sender in CustomNotification.

The main function in Main.C brackets a series of operations on TSender with calls to BeginNotification and EndNotification. This is possible because TSender derives from MNotifier, and so provides this protocol. TSender could do the bracketing itself instead. For example a Document framework command object could bracket a series of operations on a model in this way.

TReceiver is defined in Receiver.h and Receiver.C. It has been modified to handle batched notifications as follows:

1) It defines which state to use while processing batched notifications. In this sample, the state is a long. It initializes this state to a default value in its constructor.

2) It sets up its connection with a default handler that handles TBatchNotifications, in addition to handlers for the other notifications.

3) It enables batched notification before calling Connect on the connection. This causes all notifications it receives to be TBatchNotifications. These will contain the original notifications.

4) It implements the "real" handlers to operate on the state rather than take immediate action. Here the operation consists of setting the state value, if it was not previously set while processing the current TBatchNotification.

5) It implements the handler for TBatchNotification to process the notification on three steps. First, it distributes the separate notifications contained in the TBatchNotification instance back through the connection. Next, it uses the state that the other handlers operated on. Finally, it resets the state.

TLongNotification is defined in LongNotification.h and LongNotification.C. It is the same as the version found in CustomNotification.

Notes:

It only makes sense to use batch notifications when operations on the sender occur in meaningful batches that a receiver can usefully optimize for.

Notifications sent by the sender outside of a Begin/EndNotification pair are received as TBatchNotifications by receivers that have enabled batch notifications. Conversely, a receiver which has not enabled batch notifications will never receive a batch notification, but only individual notifications, whether or not they were sent as batched.

This sample relies on the sender never sending a notification with a value of kUnusedState so that it can use this value to indicate an empty cache. This is, of course, unsafe except when the sender has agreed not to send this value. This technique was used for convenience; a separate flag could have been used instead.

It is also possible to iterate through the notifications contained in the TBatchNotification instance directly (without dispatching them back through the connection) by calling CreateNotificationIterator on the TBatchNotification instance. This iterator returns a TNotificationInterestPair that you can use to retrieve the notification and interest used to send it. In this sample, for instance, HandleBatchNotification could have created the iterator, extracted the first notification, retrieved the value it contained, and then ignored the remaining notifications.


Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.