ToolTalk User's Guide

8 Sending Messages


Contents of Chapter:
8.1 How the ToolTalk Service Routes Messages
Sending Notices
Sending Requests
Changes in State of Sent Message
8.2 Message Attributes
Address Attribute
Scope Attributes
Serialization of Structured Data
8.3 ToolTalk Message Delivery Algorithm
Process-Oriented Message Delivery
Object-Oriented Message Delivery
Otype Addressing
8.4 Modifying Applications to Send ToolTalk Messages
Creating Messages
Adding Message Callbacks
Sending a Message
8.5 Examples
This chapter explains how messages are routed, and describes the ToolTalk message attributes and algorithm. It also describes how to create messages, fill in message contents, attach callbacks to requests, and send messages.

8.1 How the ToolTalk Service Routes Messages

Applications can send two classes of ToolTalk messages, notices and requests. A notice is informational, a way for an application to announce an event. Applications that receive a notice absorb the message without returning results to the sender. A request is a call for an action, with the results of the action recorded in the message, and the message returned to the sender as a reply.

Sending Notices

When you send an informational message, the notice takes a one-way trip, as shown in Figure 8-1.

Figure 8-1 Notice Routing

The sending process creates a message, fills in attribute values, and sends it. The ToolTalk service matches message and pattern attribute values, then gives a copy of the message to one handler and to all matching observers. File-scoped messages are automatically transferred across session boundaries to processes that have declared interest in the file.

Sending Requests

When you send a message that is a request, the request takes a round-trip from sender to handler and back; copies of the message take a one-way trip to interested observers. Figure 8-2 illustrates the request routing procedure.

Figure 8-2 Request Routing

The ToolTalk service delivers a request to only one handler. The handler adds results to the message and sends it back. Other processes can observe a request before or after it is handled, or at both times; observers absorb a request without sending it back.

Changes in State of Sent Message

To allow you to track the progress of a request you sent, you will receive a message every time the request changes state. You will receive these state change messages even if no patterns have been registered, or no message callbacks have been specified.

8.2 Message Attributes

ToolTalk messages contain attributes that store message information and provide delivery information to the ToolTalk service. This delivery information is used to route the messages to the appropriate receivers.

ToolTalk messages are simple structures that contain attributes for address, subject (such as operation and arguments), and delivery information (such as class and scope.) Each message contains attributes fromTable 8-1.

Table 8-1 ToolTalk Message Attributes

Address Attribute

Messages addressed to other applications can be addressed to a particular process or to any process that has registered a pattern that matches your message. When you address a message to a process, you need to know the process identifier (procid) of the other application. However, processes do not usually know each other's procid; more often, a sender does not care which process performs an operation (request message) or learns of an event (notice message).

Scope Attributes

Applications that use the ToolTalk service to communicate usually have something in common - the applications are running in the same session, or they are interested in the same file or data. To register this interest, applications join sessions or files (or both) with the ToolTalk service. This file and session information is used by the ToolTalk service with the message patterns to determine which applications should receive a message.


Note: The scope attributes are restricted to NFS and UFS files systems; file scoping does not work across file systems (for example, a tmpfs file system.)

File Scope

When a message is scoped to a file, only those applications that have joined the file (and match the remaining attributes) will receive the message. Applications that share interest in a file do not have to be running in the same session.

File-based Scoping in Patterns
Table 8-2 describes the types of scopes that use files which you can use to scope messages with patterns.

Table 8-2 Scoping a Message with Patterns to a File

To scope to the union of TT_FILE_IN_SESSION and TT_SESSION, add both scopes to the same pattern, as shown in Code Example 8-1.

Code Example 8-1 Scoping to Union of TT_FILE_IN_SESSION and TT_SESSION


tt_open(); Tt_pattern pat = tt_create_pattern(); tt_pattern_scope_add(pat, TT_FILE_IN_SESSION); tt_pattern_scope_add(pat, TT_SESSION); tt_pattern_file_add(pat, file); tt_pattern_session_add(pat, tt_default_session()); tt_pattern_register(pat);
File-based Scoping in Messages
Messages have the same types of file-based scoping mechanisms as patterns. Table 8-3 describes these scopes.

Table 8-3 Scoping Mechanisms for Messages

When a message is scoped to TT_FILE or TT_BOTH, the ToolTalk client library checks the database server for all sessions that have clients that are interested in the file and sends the message to all of the interested ToolTalk sessions. The ToolTalk sessions then match the messages to the appropriate clients. The message sender is not required to explicitly call to tt_file_join.

If a message that is scoped to TT_FILE_IN_SESSION or TT_SESSION contains a file, the database server is not contacted and the message is sent only to clients that are scoped to the message's session.

Session Scope

When a message is scoped to a session, only those applications that have connected to that session are considered as potential recipients.

Code Example 8-2 Setting a Session

Create message
Tt_message m= tt_message_create();

Add scope to message
tt_message_scope_set(m, TT_SESSION);

Add file attribute that does not
affect message scope
tt_message_file_set(m, file);

File-In-Session Scope

Applications can be very specific about the distribution of a message by specifying TT_FILE_IN_SESSION for the message scope. Only those applications that have joined both the file and the session indicated are considered potential recipients.

Applications can also scope a message to every client that has registered interest in the message's session by specifying TT_SESSION with tt_message_file_set for the message scope. When the message is received by a client whose pattern matches, the receiving client can get the file name by calling tt_message_file.

Code Example 8-3 Setting a File

Create message
Tt_message m= tt_message_create();

Add scope to message
tt_message_scope_set(m, TT_FILE_IN_SESSION);

Add file to message scope
tt_message_file_set(m, file);

Serialization of Structured Data

The ToolTalk service supports three types of data for message arguments: integers, null-terminated strings, and byte strings.

To send any other data type in a ToolTalk message, the client must serialize the data into a string or byte string and then deserialize it on receipt. The new XDR argument API calls provided with the ToolTalk service now handles these serialization and deserialization functions. The client only needs to provide an XDR routine and a pointer to the data. After serializing the data into the internal buffer, the ToolTalk service treats the data in the same manner as it treats a byte stream.

8.3 ToolTalk Message Delivery Algorithm

To help you further understand how the ToolTalk service determines message recipients, this section describes the creation and delivery of both process-oriented messages and object-oriented messages.

Process-Oriented Message Delivery

For some process-oriented messages, the sending application knows the ptype or the procid of the process that should handle the message. For other messages, the ToolTalk service can determine the handler from the operation and arguments of the message.

  1. Initialize.

    The sender obtains a message handle and fills in the address, scope, and class attributes.

    The sender fills in the operation and arguments attributes.

    If the sender has declared only one ptype, the ToolTalk service fills in sender_ptype by default; otherwise, the sender must fill it in.

    If the scope is TT_FILE, the file name must be filled in or defaulted. If the scope is TT_SESSION, the session name must be filled in or defaulted. If the scope is TT_BOTH or TT_FILE_IN_SESSION, both the file name and session name must be filled in or defaulted.


    Note: The set of patterns checked for delivery depends on the scope of the message. If the scope is TT_SESSION, only patterns for processes in the same session are checked. If the scope is TT_FILE, patterns for all processes observing the file are checked. If the scope is TT_FILE_IN_SESSION or TT_BOTH, both sets of processes are checked.

    The sender may fill in the handler_ptype if known. However, this greatly reduces flexibility because it does not allow processes of one ptype to substitute for another. Also, the disposition attribute must be specified by the sender in this case.

  2. Dispatch to handler.

    The ToolTalk service compares the address, scope, message class, operation, and argument modes and types to all signatures in the Handle section of each ptype.

    Only one ptype will usually contain a message pattern that matches the operation and arguments and specifies a handle. If a handler ptype is found, then the ToolTalk service fills in opnum, handler_ptype, and disposition from the ptype message pattern.

    If the address is TT_HANDLER, the ToolTalk service looks for the specified procid and adds the message to the handler's message queue. TT_HANDLER messages cannot be observed because no pattern matching is done.

  3. Dispatch to observers.

    The ToolTalk service compares the scope, class, operation, and argument types to all message patterns in the Observe section of each ptype.

    For all observe signatures that match the message and specify TT_QUEUE or TT_START, the ToolTalk service attaches a record (called an "observe promise") to the message that specifies the ptype and the queue or start options. The ToolTalk service then adds the ptype to its internal ObserverPtypeList.

  4. Deliver to handler.

    If a running process has a registered handler message pattern that matches the message, the ToolTalk service delivers the message to the process; otherwise, the ToolTalk service honors the disposition (start or queue) options.

    If more than one process has registered a dynamic pattern that matches the handler information, the more specific pattern (determined by counting the number of non-wildcard matches) is given preference. If two patterns are equally specific, the choice of handler is arbitrary.

  5. Deliver to observers.

    The ToolTalk service delivers the message to all running processes that have registered Observer patterns that match the message. As each delivery is made, the ToolTalk service checks off any observe promise for the ptype of the observer. After this process is completed and there are observe promises left unfulfilled, the ToolTalk service honors the start and queue options in the promises.

Example

In this example, a debugger uses an editor to display the source around a breakpoint through ToolTalk messages.

The editor has the following Handle pattern in its ptype:


(HandlerPtype: TextEditor; Op: ShowLine; Scope: TT_SESSION; Session: my_session_id; File: /home/butterfly/astrid/src/ebe.c)
  1. When the debugger reaches a breakpoint, it sends a message that contains the op (ShowLine), argument (the line number), file (the file name), session (the current session id), and scope (TT_SESSION) attributes.

  2. The ToolTalk service matches this message against all registered patterns and finds the pattern registered by the editor.

  3. The ToolTalk service delivers the message to the editor.

  4. The editor then scrolls to the line indicated in the argument.

Object-Oriented Message Delivery

Many messages handled by the ToolTalk service are directed at objects but are actually delivered to the process that manages the object.The message signatures in an otype, which include the ptype of the process that can handle each specific message, help the ToolTalk service determine process to which it should deliver an object-oriented message.

  1. Initialize.

    The sender fills in the class, operation, arguments, and the target objid attributes.

    The sender attribute is automatically filled in by the ToolTalk service. The sender can either fill in the sender_ptype and session attributes or allow the ToolTalk service to fill in the default values.

    If the scope is TT_FILE, the file name must be filled in or defaulted. If the scope is TT_SESSION, the session name must be filled in or defaulted. If the scope is TT_BOTH or TT_FILE_IN_SESSION, both the file name and session name must be filled in or defaulted.


    Note: The set of patterns checked for delivery depends on the scope of the message. If the scope is TT_SESSION, only patterns for processes in the same session are checked. If the scope is TT_FILE, patterns for all processes observing the file are checked. If the scope is TT_FILE_IN_SESSION or TT_BOTH, both sets of processes are checked.

  2. Resolve.

    The ToolTalk service looks up the objid in the ToolTalk database and fills in the otype and file attributes.

  3. Dispatch to handler.

    The ToolTalk service searches through the otype definitions for Handler message patterns that match the message's operation and arguments attributes. When a match is found, the ToolTalk service fills in scope, opnum, handler_ptype, and disposition from the otype message pattern.

  4. Dispatch to object-oriented observers.

    The ToolTalk service compares the message's class, operation, and argument attributes against all Observe message patterns of the otype. When a match is found, if the message pattern specifies TT_QUEUE or TT_START, the ToolTalk service attaches a record (called an "observe promise") to the message that specifies the ptype and the queue or start options.

  5. Dispatch to procedural observers.

    The ToolTalk service continues to match the message's class, operation, and argument attributes against all Observe message patterns of all ptypes. When a match is found, if the signature specifies TT_QUEUE or TT_START, the ToolTalk service attaches an observe promise record to the message, specifying the ptype and the queue or start options.

  6. Deliver to handler.

    If a running process has a registered Handler pattern that matches the message, the ToolTalk service delivers the message to the process; otherwise, the ToolTalk service honors the disposition (queue or start) options.

    If more than one process has registered a dynamic pattern that matches the handler information, the more specific pattern (determined by counting the number of non-wildcard matches) is given preference. If two patterns are equally specific, the choice of handler is arbitrary.

  7. Deliver to observers.

    The ToolTalk service delivers the message to all running processes that have registered Observer patterns that match the message. As each delivery is made, the ToolTalk service checks off any observe promise for the ptype of the observer. After this process is completed and there are observe promises left unfulfilled, the ToolTalk service honors the disposition (queue or start) options in the promises.

Example

In this example, a hypothetical spreadsheet application named FinnogaCalc is integrated with the ToolTalk service.

  1. FinnogaCalc starts and registers with the ToolTalk service by declaring its ptype, FinnogaCalc, and joining its default session.

  2. FinnogaCalc loads a worksheet, hatsize.wks, and tells the ToolTalk service it is observing the worksheet by joining the worksheet file.

  3. A second instance of FinnogaCalc (called FinnogaCalc2) starts, loads a worksheet, wardrobe.wks, and registers with the ToolTalk service in the same way.

  4. The user assigns the value of cell B2 in hatsize.wks to also appear in cell C14 of wardrobe.wks.

  5. So that FinnogaCalc can send the value to FinnogaCalc2, FinnogaCalc2 creates an object spec for cell C14 by calling a ToolTalk function. This object is identified by an objid.

  6. FinnogaCalc2 then gives this objid to FinnogaCalc (for example, through the clipboard).

  7. FinnogaCalc remembers that its cell B2 should appear in the object identified by this objid and sends a message that contains the value.

  8. ToolTalk routes the message. To deliver the message, the ToolTalk service:

    a. Examines the spec associated with the objid and finds that the type of the objid is FinnogaCalc_cell and that the corresponding object is in the file wardrobe.wks.

    b. Consults the otype definition for FinnogaCalc_cell. From the otype, the ToolTalk service determines that this message is observed by processes of ptype FinnogaCalc and that the scope of the message should be TT_FILE.

    c. Matches the message against registered patterns and locates all processes of this ptype that are observing the proper file. FinnogaCalc2 matches, but FinnogaCalc does not.

    d. Delivers the message to FinnogaCalc2.

  9. FinnogaCalc2 recognizes that the message contains an object that corresponds to cell C14. FinnogaCalc2 updates the value in wardrobe.wks and displays the new value.

Otype Addressing

Sometimes you may need to send an object-oriented message without knowing the objid. To handle these cases, the ToolTalk service provides otype addressing. This addressing mode requires the sender to specify the operation, arguments, scope, and otype. The ToolTalk service looks in the specified otype definition for a message pattern that matches the message's operation and arguments to locate handling and observing processes. The dispatch and delivery then proceed as in messages to specific objects.

8.4 Modifying Applications to Send ToolTalk Messages

To send ToolTalk messages, your application must perform several operations: it must be able to create and complete ToolTalk messages; it must be able to add message callback routines; and it must be able to send the completed message.

Creating Messages

The ToolTalk service provides three methods to create and complete messages:

  1. General-purpose function
  2. Process-oriented notice and request functions
  3. Object-oriented notice and request functions
The process- and object-oriented notice and request functions make message creation simpler for the common cases. They are functionally identical to strings of other tt_message_create() and tt_message_<attribute>_set() calls, but are easier to write and read. Table 8-4 and Table 8-5 list the ToolTalk functions that are used to create and complete message

Table 8-4 Functions Used to Create Messages

Table 8-5 Functions Used to Complete Messages

Using the General-Purpose Function to Create ToolTalk Messages

You can use the general-purpose function tt_message_create() to create and complete ToolTalk messages. If you create a process- or object-oriented message with tt_message_create(), use the tt_message_<attribute>_set() calls to set the attributes.

Class
Address
Scope
Fill in the scope of the message delivery. Potential recipients could be joined to:

Depending on the scope, the ToolTalk service will add the default session or file, or both to the message.

Op
Fill in the operation that describes the notification or request that you are making. To determine the operation name, consult the ptype definition for the target recipient or the message protocol definition.

Args
Fill in any arguments specific to the operation. Use the function that best suits your argument's data type:

For each argument you add (regardless of the value type), specify:

           tt_message_iarg_add(m, "Tt_status", (int) TT_OK);

The ToolTalk service sends the value as an integer but the "Tt_status" vtype tells the recipient what the value means.


Note: It is very important that senders and receivers define particular vtype names so that a receiver does not attempt to retrieve a value that was stored in another fashion; for example, a value stored as an integer but retrieved as a string.

Creating Process-Oriented Messages

You can easily create process-oriented notices and requests. To get a handle or opaque pointer to a new message object for a procedural notice or request, use the tt_pnotice_create or tt_prequest_create function. You can then use this handle on succeeding calls to reference the message.

When you create a message with tt_pnotice_create or tt_prequest_create, you must supply the following two attributes as arguments:

  1. Scope
    Fill in the scope of the message delivery. Potential recipients could be joined to:

    Depending on the scope, the ToolTalk service fills in the default session or file (or both).

  2. Op
    Fill in the operation that describes the notice or request you are making. To determine the operation name, consult the ptype definition for the target process or other protocol definition.

You use the tt_message_<attribute>_set calls to complete other message attributes such as operation arguments.

Creating and Completing Object-Oriented Messages

You can easily create object-oriented notices and requests. To get a handle or opaque pointer to a new message object for a object-oriented notice or request, use the tt_onotice_create or tt_orequest_create function. You can then use this handle on succeeding calls to reference the message.

When you create a message with tt_onotice_create or tt_orequest_create, you must supply the following two attributes as arguments:

  1. Objid
    Fill in the unique object identifier.

  2. Op
    Fill in the operation that describes the notice or request you are making. To determine the operation name, consult the ptype definition for the target process or other protocol definition.

You use the tt_message_<attribute>_set calls to complete other message attributes such as operation arguments.

Adding Message Callbacks

When a request contains a message callback routine, the callback routine is automatically called when the reply is received to examine the results of the reply and take appropriate actions.


Note: Callbacks are called in reverse order of registration (for example, the most recently added callback is called first).

You use tt_message_callback_add to add the callback routine to your request. When the reply comes back and the reply message has been processed through the callback routine, the reply message must be destroyed before the callback function returns TT_CALLBACK_PROCESSED. To destroy the reply message, use tt_message_destroy, as illustrated in Figure 8-3.

Figure 8-3 Destroying a Message


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 is a callback routine, cntl_msg_callback, that examines the state field of the reply and takes action if the state is started, handled, or failed.


/* * Default callback for all the ToolTalk messages we send. */ Tt_callback_action cntl_msg_callback(m, p) Tt_message m; Tt_pattern p; { int mark; char msg[255]; char *errstr; mark = tt_mark(); switch (tt_message_state(m)) { case TT_STARTED: xv_set(cntl_ui_base_window, FRAME_LEFT_FOOTER, "Starting editor...", NULL); break; case TT_HANDLED: xv_set(cntl_ui_base_window, FRAME_LEFT_FOOTER, "", NULL); break; case TT_FAILED: errstr = tt_message_status_string(m); if (tt_pointer_error(errstr) == TT_OK && errstr) { sprintf(msg,"%s failed: %s", tt_message_op(m), errstr); } else if (tt_message_status(m) == TT_ERR_NO_MATCH) { sprintf(msg,"%s failed: Couldn't contact editor", tt_message_op(m), tt_status_message(tt_message_status(m))); } else { sprintf(msg,"%s failed: %s", tt_message_op(m), tt_status_message(tt_message_status(m))); } xv_set(cntl_ui_base_window, FRAME_LEFT_FOOTER, msg, NULL); break; default: break; } /* * no further action required for this message. Destroy it * and return TT_CALLBACK_PROCESSED so no other callbacks will * be run for the message. */ tt_message_destroy(m); tt_release(mark); return TT_CALLBACK_PROCESSED; }
You can also add callbacks to static patterns by attaching a callback to the opnum of a signature in a ptype. When a message is delivered because it matched a static pattern with an opnum, the ToolTalk service checks for any callbacks attached to the opnum and runs them.

Sending a Message

When you have completed your message, use tt_message_send to send it.

If the ToolTalk service returns TT_WRN_STALE_OBJID, it has found a forwarding pointer in the ToolTalk database that indicates the object mentioned in the message has been moved. However, the ToolTalk service will send the message with the new objid. You can then use tt_message_object to retrieve the new objid from the message and put it into your internal data structure.

If you will not need the message in the future (for example, if the message was a notice), you can use tt_message_destroy to delete the message and free storage space.


Note: If you are expecting a reply to the message, do not destroy the message until you have handled the reply.

8.5 Examples

Figure 8-4 illustrates how to create and send a pnotice.

Figure 8-4 Creating and Sending a Pnotice


/* * Create and send a ToolTalk notice message * ttsample1_value(in int <new value) */ msg_out = tt_pnotice_create(TT_SESSION, "ttsample1_value"); tt_message_arg_add(msg_out, TT_IN, "integer", NULL); tt_message_arg_ival_set(msg_out, 0, (int)xv_get(slider, PANEL_VALUE)); tt_message_send(msg_out); /* * Since this message is a notice, we don't expect a reply, so * there's no reason to keep a handle for the message. */ tt_message_destroy(msg_out);
Figure 8-5 illustrates how an orequest is created and sent when the callback routine for cntl_ui_hilite_button is called.

Figure 8-5 Creating and Sending an Orequest


/* * Notify callback function for `cntl_ui_hilite_button'. */ void cntl_ui_hilite_button_handler(item, event) Panel_item item; Event *event; { Tt_message msg; if (cntl_objid == (char *)0) { xv_set(cntl_ui_base_window, FRAME_LEFT_FOOTER, "No object id selected", NULL); return; } msg = tt_orequest_create(cntl_objid, "hilite_obj"); tt_message_arg_add(msg, TT_IN, "string", cntl_objid); tt_message_callback_add(msg, cntl_msg_callback); tt_message_send(msg); }



Generated with CERN WebMaker