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 9-1.
Table 9-1 Functions for Creating, Updating, and Deleting Message Patterns
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 assumes that you want any value to match. Some attributes are set and, therefore, can only have one value.
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 and the API objects involved in the operation are freed. 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 reply msg ...
tt_message_destroy(m);
return TT_CALLBACK_PROCESSED;
}
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);
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.
/*
* Join the default session
*/
tt_session_join(tt_default_session());
Table 9-2 lists the ToolTalk functions you use to join the session in which you are interested.
Table 9-2 ToolTalk Functions for Joining Default Sessions
Once your patterns are updated, you will begin to receive messages scoped to the session you joined.
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.
tt_default_session_set(new_session_identifier);
tt_open();
tt_session_join(new_session);
In order to effectively use multiple sessions, you must store the session ids of the sessions in which you are interested in order to pass these identifiers to tt_default_session_set prior to opening a new session with tt_open; that is, you need to place the values (which ttsession stores in the environment variable _TT_SESSION) in a file on the system so that other ToolTalk clients can access the value of a session id contained in that file and use it to open the non-default session. For example, you can store the session ids in a "well-known" file and then send a file-scoped message (indicating this file) to all clients which have registered an appropriate pattern. The client will then know to open the scoped-to file, read one or more session ids from it, and use these session ids (with tt_open) to open a non-default session. An alternative method is advertising the session ids by means of, for example, a name service or a third-party database.
Table 9-3 ToolTalk Functions for Joining Files of Interest
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.