Chapter 3. Dynamic Message Patterns

This chapter describes how to provide dynamic message pattern information to the ToolTalk service. The ToolTalk service uses message patterns to determine message recipients. After receiving a message, the ToolTalk service compares the message to all current message patterns to find a matching pattern. Once a match is made, the message is delivered to the application listed in the message pattern.

You can provide message pattern information to the ToolTalk service either dynamic or static methods,or both. The method you choose depends on the type of messages you want to receive.

Regardless of the method you choose to provide message patterns to the ToolTalk service, you will want to update these patterns with each current session and file information so that you receive all messages that refer to the session or file in which you are interested.

Message Pattern Attributes

The attributes in your message pattern specify the type of messages you want to receive and, to some extent, the number of messages you receive. You can supply multiple values for each attribute you add to a pattern. However, some attributes are set and have only one value.

Table 3-1 provides a complete list of attributes you can put in your message patterns.

Table 3-1. ToolTalk Message Pattern Attributes

Pattern Attributes

Value

Description

Category

TT_OBSERVE, TT_HANDLE,

Declares whether you want to perform the operation listed in a message or only view a message.

 

Scope

TT_SESSION, TT_FILE, TT_BOTH, TT_FILE_IN_SESS ION

Declares interest in messages about a session or a file, or both; Join a session or file after the message pattern is registered to update the sessid and filename.

 

 

Arguments

arguments or results

Declares the arguments for the operation in which you are interested.

 

Class

TT_NOTICE, TT_REQUEST

Declares whether you want to receive notices or requests, or both.

 

File

char *pathname

Declares the files in which you are interested.

 

Object

char *objid

Declares what objects in which you are interested.

 

Operation

char *opname

Declares what operations in which you are interested.

 

Otype

char *otype

Declares the type of objects in which you are interested.

 

address

TT_PROCEDURE, TT_OBJECT, TT_HANDLER, TT_OTYPE

Declares what type of address in which you are interested.

 

 

address

TT_PROCEDURE, TT_OBJECT, TT_HANDLER, TT_OTYPE

Declares what type of address in which you are interested.

 

 

disposition

TT_DISCARD, TT_QUEUE, TT_START

Instructs the ToolTalk service how to handle undeliverable messages to your application

 

.

sender

char *procid

Declares the sender in which you are interested.

 

sender_ptype

char *ptype

Declares the type of sending process in which you are interested.

 

session

char *sessid

Declares the session in which you are interested.

 

state

TT_CREATED, TT_SENT, TT_HANDLED, TT_FAILED, TT_QUEUED, TT_STARTED, TT_REJECTED

Declares the state of the message in which you are interested.

 

 

 

 

 

All your message patterns must minimally specify:

  • Scope — Whether the application is interested in messages about a particular session or file.

  • Use TT_SESSION to receive messages from other processes in your session.

  • Use TT_FILE to receive messages about the file you have joined.

  • Use TT_FILE_IN_SESSION to receive messages for the file you have joined while in this session.


    Note: Messages that have a TT_BOTH scope will match your pattern if it has either TT_FILE or TT_SESSION.


  • Category — Whether the application wants to perform operations listed in messages or only view messages.

  • Use TT_OBSERVE if you only want to view messages.

  • Use TT_HANDLE to if you want to perform operations listed in the message.

The ToolTalk service compares message attributes to pattern attributes as follows:

  • If no pattern attribute is specified, the ToolTalk service counts the message attribute as matched. The fewer pattern attributes you specify, the more messages you become eligible to receive.

  • If there are multiple values specified for a pattern attribute, one of the values must match the message attribute value. If no value matches, the ToolTalk service will not consider your application as a receiver.

Defining Dynamic Messages

The dynamic method provides message pattern information while your application is running. You create a message pattern and register it with the ToolTalk service. You can add callback routines to dynamic message patterns that the ToolTalk service will call when it matches a message to the pattern.

To create and register a dynamic message pattern, you allocate a new pattern object, fill in the proper information, and register it. When you are done with the pattern (that is, when you are no longer interested in messages that match it), either unregister or destroy the pattern. You can register and unregister dynamic message patterns as needed.

The ToolTalk functions used to create, register, and unregister dynamic message patterns are listed in Table 3-2.

Table 3-2. ToolTalk Dynamic Message Pattern Attributes

Return Type

ToolTalk Function

Tt_pattern

tt_pattern_create(void)

Tt_status

tt_pattern_arg_add(Tt_pattern p, Tt_mode n, const char *vtype, const char *value)

Tt_status

tt_pattern_barg_add(Tt_pattern m, Tt_mode n, const char *vtype, const unsigned char *value, int len)

Tt_status

tt_pattern_iarg_add(Tt_pattern m, Tt_mode n, const char *vtype, int value)

Tt_status

tt_pattern_address_add(Tt_pattern p, Tt_address d)

Tt_status

tt_pattern_callback_add(Tt_pattern m, Tt_message_callback f)

Tt_status

tt_pattern_category_set(Tt_pattern p, Tt_category c)

Tt_status

tt_pattern_class_add(Tt_pattern p, Tt_class c)

Tt_status

tt_pattern_disposition_add(Tt_pattern p, Tt_disposition r)

Tt_status

tt_pattern_file_add(Tt_pattern p, const char *file)

Tt_status

tt_pattern_object_add(Tt_pattern p, const char *objid)

Tt_status

tt_pattern_op_add(Tt_pattern p, const char *opname)

Tt_status

tt_pattern_opnum_add(Tt_pattern p, int opnum)

Tt_status

tt_pattern_otype_add(Tt_pattern p, const char *otype)

Tt_status

tt_pattern_scope_add(Tt_pattern p, Tt_scope s)

Tt_status

tt_pattern_sender_add(Tt_pattern p, const char *procid)

Tt_status

tt_pattern_sender_ptype_add(Tt_pattern p, const char *ptid)

Tt_status

tt_pattern_session_add(Tt_pattern p, const char *sessid)

Tt_status

tt_pattern_state_add(Tt_pattern p, Tt_state s)

Tt_status

tt_pattern_user_set(Tt_pattern p, int key, void *v)

Tt_status

tt_pattern_register(Tt_pattern p)

Tt_status

tt_pattern_unregister(Tt_pattern p)

Tt_status

tt_pattern_destroy(Tt_pattern p)


Creating a Message Pattern

To create message patterns, use the tt_pattern_create() function. You can use this funtion to get a handle or opaque pointer to a new pattern object, and then use this handle on succeeding calls to reference the pattern.

To fill in pattern information, use the tt_pattern_<attribute>_add() and tt_pattern_<attribute>_set() calls. You can supply multiple values for each attribute you add to a pattern. The pattern attribute matches a message attribute if any of the values in the pattern match the value in the message. If no value is specified for an attribute, the ToolTalk service asumes that you want any value to match. See Table 3-1 for a complete list of pattern attributes.


Note: Some attributes are set and, therefore, can only have one value.


Adding a Message Pattern Callback

To add a callback routine to your pattern, use the tt_pattern_callback_add() function.

When the ToolTalk service matches a message, it automatically calls your callback routine to examine the message and take appropriate actions. When a message that matches a pattern with a callback is delivered to you, it is processed through the callback routine. When the routine is finished, it returns TT_CALLBACK_PROCESSED; you can then use tt_message_destroy() to destroy the message, which frees the storage used by the message, as illustrated in the following code sample.

Tt_callback action
sample_msg_callback(Tt_message m, Tt_pattern p)
{
	... process the msg ...

	tt_message_destroy(m);
	return TT_CALLBACK_PROCESSED;
}

Registering a Message Pattern

To register the completed pattern, use the tt_pattern_register() function. After you register your pattern, you join the sessions or files of interest.

The following code sample creates and registers a pattern.

/*
 * Create and register a pattern so ToolTalk knows we are 
 * interested in “ttsample1_value” messages within the 
 * session we join.
*/

pat = tt_pattern_create();
tt_pattern_category_set(pat, TT_OBSERVE);
tt_pattern_scope_add(pat, TT_SESSION);
tt_pattern_op_add(pat, “ttsample1_value”);
tt_pattern_register(pat);

Deleting and Unregistering a Message Pattern

If delivered messages that matched the deleted pattern have not been retrieved by your application (for example, the messages might be queued), the ToolTalk service does not destroy these messages.

To delete a message pattern, use the tt_pattern_destroy() function. This function first unregisters the pattern and then destroys the pattern object.

To stop receiving messages that match a message pattern without destroying the pattern object, use the tt_pattern_unregister() to unregister the pattern.

The ToolTalk service will automatically unregister and destroy all message pattern objects when you call tt_close().

Updating Message Patterns with the Current Session or File

To update your message patterns with the session or file in which you are currently interested, join the session or file.

Joining the Default Session

When you join a session or file, the ToolTalk service updates your message pattern with the sessid. For example, if you have declared a ptype or registered a message pattern that specifies TT_SESSION or TT_FILE_IN_SESSION, use tt_session_join() join the default sessionThe following code sample shows how to join the default session.

/*
 * Join the default session
 */

tt_session_join(tt_default_session());

Table 3-3 lists the ToolTalk functions you use to join the session in which you are interested.

Table 3-3. ToolTalk Functions for Joining Default Sessions

Return Type

ToolTalk Function

char *

tt_default_session(void)

Tt_status

tt_default_session_set(const char *sessid)

char *

tt_initial_session(void)

Tt_status

tt_session_join(const char *sessid)

Tt_status

tt_session_quit(const char *sessid)

Once your patterns are updated, you will begin to receive messages scoped to the session you joined.


Note: If you had previously joined a session and then registered a ptype or a new message pattern, you must again join the same session or a new session to update your pattern before you will receive messages that match your new pattern.

When you no longer want to receive messages that reference the default session, use the tt_session_quit() function. This function removes the sessid from your session–scoped message patterns.

Joining Files of Interest

When you join a file, the ToolTalk service automatically adds the name of the file to your file-scoped message patterns. For example, if you have declared a process type or registered a message pattern that specifies TT_FILE or TT_FILE_IN_SESSION, use the tt_file_join() function to join files of interested. Table 3-4 lists the ToolTalk functions you use to express your interest in specific files.

Table 3-4. ToolTalk Functions for Joining Files of Interest

Return Type

ToolTalk Function

char *

tt_default_file(void)

Tt_status

tt_default_file_set(const char *docid)

Tt_status

tt_file_join(const char *filepath)

Tt_status

tt_file_quit(const char *filepath)

When you no longer want to receive messages that reference the file, use the tt_file_quit() function to remove the file name from your file–scoped message patterns.