Receiving messages from a caucus

Caucus members receive messages using TStandardCaucusReceiveMessage objects. A client receives a message by creating a TStandardCaucusReceiveMessage message, streaming out the contents, and destroying the object.

You do not need to read all of the data in a message--you can read just enough to determine that a message is not of interest. However, if you try to read beyond the amount of data contained in the object, this raises an exception.

Each incoming message includes:

The member alias is an alias to the receiving member.

The sender identifier corresponds to the caucus member that originated the message.

The message type identifies the message using an enumerated value:

kJoin Join message; zero bytes. Indicates that the sender joined the caucus.
kLeave Leave message; zero bytes. Indicates that either the sender exited the caucus intentionally or the sender crashed.
kData A message not generated automatically by the standard implementation; a custom message as defined by a caucus member.
kPause Pause message. Indicates that the sender is pausing caucus communication.
kResume Resume message. Confirms that the sender has resumed caucus communication.

One way to set up a caucus member to receive messages is to create a thread that loops continuously, creating TStandardCaucusReceiveMessage objects. This thread blocks until it can create a valid TCaucusReceiveMessage object--once it successfully creates a receive message object, the code can stream the data out and destroy the object.

To receive a message:

  1. Construct a TCaucusReceiveMessage object on the stack.
  2. Stream data out of the object.
  3. Destroy the TCaucusReceiveMessage object.
The next example runs in a loop, testing to make sure that the incoming message is not the receiving member's own leave message. The code evaluates the incoming message type and calls the corresponding member function of a message handler object to process the message. By handling messages in the manner shown here, instead of streaming out the data no matter what the message type, the code can better handle zero-byte messages (such as join
and leave).

NOTE The example code shows the body of Run, a member of TStandardCaucusReadThread. This class is shown for example only; it is not a part of the CommonPoint application system. The variable fMessageHandler is a member of MStandardCaucusMessageHandler, a user-defined class not shown here. For Beta, you need to define your own read thread classes; you can model your own using TStandardCaucusReadThread as an example.

      // Copyright 1995 Taligent, Inc. All rights reserved.
      
      void TStandardCaucusReadThread::Run()
      {
          try
          {
              TCaucusMember::Identifier member = 0;
              TCaucusMember::Identifier me = fCaucusMember->GetIdentity();
              
              TStandardCaucusMember::EMessageType messageType = TStandardCaucusMember::kJoin;
              
              // stop only when the message is my Leave message
              while ( !((messageType == TStandardCaucusMember::kLeave) && (member == me)) )
              {
                  TStandardCaucusReceiveMessage message(fCaucusMember, member, messageType);
                  switch (messageType)
                  {
                  case TStandardCaucusMember::kData:
                      fMessageHandler->HandleReceivedData(message, member);
                  break;
                  case TStandardCaucusMember::kLeave:
                      fMessageHandler->HandleMemberLeft(member);
                  break;
                  case TStandardCaucusMember::kJoin:
                      fMessageHandler->HandleMemberJoin(member);
                  break;
                  case TStandardCaucusMember::kPause:
                      fMessageHandler->HandleReceivedPause(message, member);
                  break;
                  case TStandardCaucusMember::kResume:
                      fMessageHandler->HandleReceivedResume(message, member);
                  break;
                  }
              }
          } catch (const TCaucusException &e)
          {
              // Can only mean the caucus no longer exists
              fMessageHandler->HandleCaucusReadException(e);
          }
      }


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

Generated with WebMaker