/** Copyright 2007 Haiku, Inc. All rights reserved.* Distributed under the terms of the MIT License.** Authors:* Niels Sascha Reedijk, niels.reedijk@gmail.com*//*!\page app_messaging Messaging FoundationsOne of the foundations of the Haiku API is the messaging system. Thisframework is the basis for the efficient multithreaded Haiku applications,because it solves one of the fundamental issues of multithreading: itallows you to easily and securely communicate between threads. Theframework allows inter-application messaging as well asintra-application messaging, and it will always use the most effectivemechanism for the communication automatically.This page will introduce you to the subject of messaging. It is meant as abroad overview to the classes, rather than a tutorial. If you are lookingfor effective messaging techniques or a tutorial on messaging, have a lookat the developer section of the Haiku website.<b>Table of contents</b>- Overview of the Messaging Classes- Receiving and Handling Messages- Sending messages\section app_messaging_overview Overview of the Messaging Classes\subsection app_messaging_overview_bmessage BMessageThe BMessage class is the class that is in the center of all the messengeroperations, because it represents a message. A message is nothing more thanan object that contains:- The \c what member, an \c uint32 that determines the type of message.Some constants are defined by the Haiku API, for example B_MOUSE_DOWN orB_QUIT_REQUESTED.- Zero or more data objects. BMessage is a powerful data container thatkeeps track of different sorts of data. BMessage provides many convenientAdd*() methods, for example BMessage::AddBool(). With the correspondingFind*() method (in this example,\link BMessage::FindBool(const char *, int32, bool *) const FindBool() \endlink)you can retrieve the data.BMessage itself is generic, its syntax and semantics are determined by thecontext. The Haiku API defines several messages and their required datamembers. Several applications provide a scripting interface with definedmessage syntax. You can do the same for your application.\subsection app_messaging_overview_blooper BLooperObjects of the BLooper type are objects that run message loops. Everyobject runs in its own thread. The BLooper objects continually check forincoming messages. To process the messages, the looper looks for messagehandlers that handle the messages within the thread's context. Messagehandling within a looper is synchronous.BLooper inherits BHandler, the base class for message handling. However, itis possible to chain additional handlers to the object. For example, if youhave an application that understands different networking protocols, andyou support extensions that understand the base protocol, these extensionscan provide handlers that you can chain in your general message parserthread. See AddHandler() and SetPreferredHandler() for information onhandlers.Messages can be posted to the looper by using the object's PostMessage()method. This method puts the message in the BMessageQueue of the looper.Since PostMessage() is asynchronous, the message might not be handledimmediately. See \ref app_messaging_overview_bmessenger "BMessenger"for a synchronous implementation.Loopers can have a generic filter that discards messages based onuser-definable characteristics. The BMessageFilter class provides thefoundation for the qualifying of messages. See AddCommonFilterList() andSetCommonFilterList() for more information.To get the most out of the functionality of BLooper, it is usuallysubclassed to create a self-contained event 'machine'. Most of the time,these subclasses also perform the message handling, which is possibledue to the fact that it is also a subclass of BHandler.In the Haiku API, there are two major classes that inherit BLooper:the base application class, BApplication, and the window class, BWindow.Because they inherit BLooper, each application and each window has itsown message loop. This makes every window quick and responsive. To keepyour applications running smoothly, it is advisable to make sure thatevent handling that requires more processing power, is done within its ownBLooper context. Networking usually qualifies as a candidate for its ownthread.\subsection app_messaging_overview_bhandler BHandlerObjects of the BHandler type are associated to BLoopers. When they arecreated, they should be passed to the BLooper::AddHandler() method of thelooper they want to handle messages for. They can then either be set aspreferred handlers (by chaining them with BLooper::SetPreferredHandler()),or they can be added to other BHandlers with the SetNextHandler() method.The magic of the class happens in the MessageReceived() method. In yoursubclasses you override this method, to check the incoming BMessage.Usually, you check the \c what member of the message in a switch statement.If your handler cannot handle the object, it will pass the message on tothe parent class.\warning Don't forget to actually call the MessageReceived() method of thebase class. Failing to do this will mean that the message chain willnot completely be followed, which can lead to unhandled messages. Theremight be some internal system messages that the Haiku API classeshandle, and not actually handling these messages could lead toinconsistent internal behavior.\subsection app_messaging_overview_bmessenger BMessengerBMessenger objects can send messages to both local and remote targets. Forlocal targets, a BMessenger provides an advantage over directly callingthe BLooper::PostMessage() method: some variants of theBMessenger::SendMessage() methods allow for synchronous replies. So, thecall will actually verify the handling thread processes the message, andreply to the sender.The other feature of BMessenger is that it is able to be constructed withthe signature of another application as argument. This allows the messengerto pass messages to other applications. It facilitates inter-applicationcommunication.\subsection app_messaging-overview-other Other messaging classesThere are several convenience classes supplied with the application kit,which can make your life easier in some specific cases.- BInvoker binds together a message and a target. By callingBInvoker::Invoke(), the message will be sent. This class is inherited bythe controls in the interface kit, such as BButton.- A BMessageRunner object will send messages to a specified target withspecified intervals in between.- BMessageQueue is a class that is also internally used by BLooper. Itprovides a queue of messages, with convenience functions of managingthis queue.- BMessageFilter is the base class of the filters. Filters can be appliedto BLoopers to filter all incoming messages, or to BHandlers to filtermessages that could be handled by that object. The filter object can besubclassed and extended by overriding the \link BMessageFilter::Filter()Filter() \endlink method.\section app-messaging-receiving Receiving MessagesTo do...\section app-messaging-sending Sending MessagesTo do...*/