Haiku OS Messaging

Introduction

Haiku OS is an open-source operating system designed for personal computing. One of its key features is its unique messaging passing system, which allows different parts of the operating system to communicate with each other efficiently.

What is Messaging Passing?

Messaging passing is a communication mechanism used in operating systems to allow different processes or threads to exchange data and synchronize their activities. Instead of directly accessing each other's memory, processes communicate by sending and receiving messages through a messaging system provided by the operating system.

Haiku OS application Kit provides a message-passing system that lets the application send messages to and receives messages from other applications (Including the servers and apps), and from other threads in the own application.

The primary messaging classes are:

1 BMessage

  • The BMessage class is the fundamental messaging class in Haiku OS.
  • It represents a message that can be sent from one process to another.

Key Features:

  • Supports various data types, including integers, floating-point numbers, strings, and pointers.
  • Allows for the creation of custom messages with user-defined data fields.

Example:

// Creating a message
BMessage message(MSG_HELLO);
message.AddString("name", "Haiku");

// Sending the message
messenger.SendMessage(&message);

2 BHandler

Defines hook functions that are called to handle in-coming messages.

Description:

  • BHandler is a fundamental class in Haiku OS that allows objects to receive and handle messages.
  • It provides a mechanism for objects to register as message handlers and process incoming messages.

Key Features:

  • Allows objects to register as message handlers and associate themselves with specific message types using the AddHandler() method.
  • Provides a virtual function MessageReceived() that is called whenever a registered message is received by the handler.
  • Supports the handling of system messages and user-defined messages.

Example:

class MyHandler : public BHandler {
public:
    MyHandler() : BHandler() {}

    void MessageReceived(BMessage* message) {
        switch (message->what) {
            case MSG_HELLO:
                // Handle hello message
                break;
            default:
                BHandler::MessageReceived(message);
        }
    }
};

3 BLooper

Runs a loop that receives in-coming messages and figures out which BHandler should handle them.

Description:

  • BLooper is a class in Haiku OS that manages a set of message handlers and dispatches messages to them.
  • It provides a central message loop for handling and dispatching messages to registered handlers.

Key Features:

  • Allows objects to register as message handlers using the AddHandler() method.
  • Provides a message loop that continuously waits for incoming messages and dispatches them to the appropriate handler.
  • Supports the creation of custom looper threads for handling messages asynchronously.

Example:

class MyLooper : public BLooper {
public:
    MyLooper() : BLooper() {}

    void MessageReceived(BMessage* message) {
        switch (message->what) {
            case MSG_HELLO:
                // Handle hello message
                break;
            default:
                BLooper::MessageReceived(message);
        }
    }
};

4 BMessenger

Represents a message's destination (a combination of BLooper and BHandler), whether it's local or remote. The object is most useful for sending messages to other applications—you don't need it for local calls.

Description:

  • The BMessenger class is used to send messages to specific handlers.
  • It provides a convenient way to target message delivery to a specific handler or looper.

Key Features:

  • Allows processes to specify the target handler or looper for message delivery.
  • Provides methods for sending messages asynchronously and synchronously.

Example:

// Creating a messenger
BMessenger messenger(&targetHandler);

// Sending a message
messenger.SendMessage(MSG_HELLO);

5 BMessageRunner

Lets you send the same message over and over, at regular intervals.

Description:

  • The BMessageRunner class is used to send messages at regular intervals.
  • It allows processes to schedule the sending of messages in a timed manner.

Key Features:

  • Supports the automatic sending of messages at specified intervals.
  • Provides control over the timing and frequency of message delivery.

Example:

// Creating a message runner
BMessageRunner* messageRunner = new BMessageRunner(
                                    BMessenger(&targetHandler),
                                    new BMessage(MSG_UPDATE),
                                    1000000); // 1 second interval

 

The other messaging classes are:

BMessageQueue
Is a FIFO that holds a BLooper's in-coming messages.

BMessageFilter
Is a device that can examine and (potentially) reject or re-route in-coming messages.

BInvoker
Is a convenience class that lets you treat a message and its target (the BHandler that will handle the message) as a single object.