xref: /haiku/docs/user/app/_app_messaging.dox (revision 46b7da1f4f40f7157d74fc7fb26ff9ec7f2416f2)
1575d819aSNiels Sascha Reedijk/*
2820dca4dSJohn Scipione * Copyright 2007 Haiku, Inc. All rights reserved.
3575d819aSNiels Sascha Reedijk * Distributed under the terms of the MIT License.
4575d819aSNiels Sascha Reedijk *
5575d819aSNiels Sascha Reedijk * Authors:
6575d819aSNiels Sascha Reedijk *		Niels Sascha Reedijk, niels.reedijk@gmail.com
7575d819aSNiels Sascha Reedijk */
8575d819aSNiels Sascha Reedijk
9575d819aSNiels Sascha Reedijk/*!
10575d819aSNiels Sascha Reedijk	\page app_messaging Messaging Foundations
11575d819aSNiels Sascha Reedijk
12575d819aSNiels Sascha Reedijk	One of the foundations of the Haiku API is the messaging system. This
13575d819aSNiels Sascha Reedijk	framework is the basis for the efficient multithreaded Haiku applications,
14575d819aSNiels Sascha Reedijk	because it solves one of the fundamental issues of multithreading: it
154f6e4816SNiels Sascha Reedijk	allows you to easily and securely communicate between threads. The
164f6e4816SNiels Sascha Reedijk	framework allows inter-application messaging as well as
174f6e4816SNiels Sascha Reedijk	intra-application messaging, and it will always use the most effective
184f6e4816SNiels Sascha Reedijk	mechanism for the communication automatically.
19575d819aSNiels Sascha Reedijk
20575d819aSNiels Sascha Reedijk	This page will introduce you to the subject of messaging. It is meant as a
21575d819aSNiels Sascha Reedijk	broad overview to the classes, rather than a tutorial. If you are looking
22575d819aSNiels Sascha Reedijk	for effective messaging techniques or a tutorial on messaging, have a look
23575d819aSNiels Sascha Reedijk	at the developer section of the Haiku website.
24575d819aSNiels Sascha Reedijk
25575d819aSNiels Sascha Reedijk	<b>Table of contents</b>
26575d819aSNiels Sascha Reedijk	- Overview of the Messaging Classes
27575d819aSNiels Sascha Reedijk	- Receiving and Handling Messages
28575d819aSNiels Sascha Reedijk	- Sending messages
29575d819aSNiels Sascha Reedijk
30575d819aSNiels Sascha Reedijk	\section app_messaging_overview Overview of the Messaging Classes
31575d819aSNiels Sascha Reedijk
32575d819aSNiels Sascha Reedijk	\subsection app_messaging_overview_bmessage BMessage
33575d819aSNiels Sascha Reedijk
34575d819aSNiels Sascha Reedijk	The BMessage class is the class that is in the center of all the messenger
35575d819aSNiels Sascha Reedijk	operations, because it represents a message. A message is nothing more than
36575d819aSNiels Sascha Reedijk	an object that contains:
37575d819aSNiels Sascha Reedijk	- The \c what member, an \c uint32 that determines the type of message.
38575d819aSNiels Sascha Reedijk	  Some constants are defined by the Haiku API, for example B_MOUSE_DOWN or
39575d819aSNiels Sascha Reedijk	  B_QUIT_REQUESTED.
40575d819aSNiels Sascha Reedijk	- Zero or more data objects. BMessage is a powerful data container that
41575d819aSNiels Sascha Reedijk	  keeps track of different sorts of data. BMessage provides many convenient
42575d819aSNiels Sascha Reedijk	  Add*() methods, for example BMessage::AddBool(). With the corresponding
434f6e4816SNiels Sascha Reedijk	  Find*() method (in this example,
444f6e4816SNiels Sascha Reedijk	  \link BMessage::FindBool(const char *, int32, bool *) const FindBool() \endlink)
454f6e4816SNiels Sascha Reedijk	  you can retrieve the data.
46575d819aSNiels Sascha Reedijk
47575d819aSNiels Sascha Reedijk	BMessage itself is generic, its syntax and semantics are determined by the
48575d819aSNiels Sascha Reedijk	context. The Haiku API defines several messages and their required data
49575d819aSNiels Sascha Reedijk	members. Several applications provide a scripting interface with defined
50575d819aSNiels Sascha Reedijk	message syntax. You can do the same for your application.
51575d819aSNiels Sascha Reedijk
52575d819aSNiels Sascha Reedijk	\subsection app_messaging_overview_blooper BLooper
53575d819aSNiels Sascha Reedijk
54575d819aSNiels Sascha Reedijk	Objects of the BLooper type are objects that run message loops. Every
55575d819aSNiels Sascha Reedijk	object runs in its own thread. The BLooper objects continually check for
564f6e4816SNiels Sascha Reedijk	incoming messages. To process the messages, the looper looks for message
574f6e4816SNiels Sascha Reedijk	handlers that handle the messages within the thread's context. Message
584f6e4816SNiels Sascha Reedijk	handling within a looper is synchronous.
59575d819aSNiels Sascha Reedijk
60575d819aSNiels Sascha Reedijk	BLooper inherits BHandler, the base class for message handling. However, it
61575d819aSNiels Sascha Reedijk	is possible to chain additional handlers to the object. For example, if you
62575d819aSNiels Sascha Reedijk	have an application that understands different networking protocols, and
63575d819aSNiels Sascha Reedijk	you support extensions that understand the base protocol, these extensions
64575d819aSNiels Sascha Reedijk	can provide handlers that you can chain in your general message parser
65575d819aSNiels Sascha Reedijk	thread. See AddHandler() and SetPreferredHandler() for information on
66575d819aSNiels Sascha Reedijk	handlers.
67575d819aSNiels Sascha Reedijk
68575d819aSNiels Sascha Reedijk	Messages can be posted to the looper by using the object's PostMessage()
69575d819aSNiels Sascha Reedijk	method. This method puts the message in the BMessageQueue of the looper.
70575d819aSNiels Sascha Reedijk	Since PostMessage() is asynchronous, the message might not be handled
7151e5c73eSNiels Sascha Reedijk	immediately. See \ref app_messaging_overview_bmessenger "BMessenger"
7251e5c73eSNiels Sascha Reedijk	for a synchronous implementation.
73575d819aSNiels Sascha Reedijk
74575d819aSNiels Sascha Reedijk	Loopers can have a generic filter that discards messages based on
75575d819aSNiels Sascha Reedijk	user-definable characteristics. The BMessageFilter class provides the
76575d819aSNiels Sascha Reedijk	foundation for the qualifying of messages. See AddCommonFilterList() and
77575d819aSNiels Sascha Reedijk	SetCommonFilterList() for more information.
78575d819aSNiels Sascha Reedijk
79575d819aSNiels Sascha Reedijk	To get the most out of the functionality of BLooper, it is usually
80575d819aSNiels Sascha Reedijk	subclassed to create a self-contained event 'machine'. Most of the time,
81575d819aSNiels Sascha Reedijk	these subclasses also perform the message handling, which is possible
82575d819aSNiels Sascha Reedijk	due to the fact that it is also a subclass of BHandler.
83575d819aSNiels Sascha Reedijk
84575d819aSNiels Sascha Reedijk	In the Haiku API, there are two major classes that inherit BLooper:
85575d819aSNiels Sascha Reedijk	the base application class, BApplication, and the window class, BWindow.
86575d819aSNiels Sascha Reedijk	Because they inherit BLooper, each application and each window has its
87575d819aSNiels Sascha Reedijk	own message loop. This makes every window quick and responsive. To keep
88575d819aSNiels Sascha Reedijk	your applications running smoothly, it is advisable to make sure that
89575d819aSNiels Sascha Reedijk	event handling that requires more processing power, is done within its own
90575d819aSNiels Sascha Reedijk	BLooper context. Networking usually qualifies as a candidate for its own
91575d819aSNiels Sascha Reedijk	thread.
92575d819aSNiels Sascha Reedijk
93b15c8e82SNiels Sascha Reedijk	\subsection app_messaging_overview_bhandler BHandler
94575d819aSNiels Sascha Reedijk
95575d819aSNiels Sascha Reedijk	Objects of the BHandler type are associated to BLoopers. When they are
96575d819aSNiels Sascha Reedijk	created, they should be passed to the BLooper::AddHandler() method of the
97575d819aSNiels Sascha Reedijk	looper they want to handle messages for. They can then either be set as
98575d819aSNiels Sascha Reedijk	preferred handlers (by chaining them with BLooper::SetPreferredHandler()),
99575d819aSNiels Sascha Reedijk	or they can be added to other BHandlers with the SetNextHandler() method.
100575d819aSNiels Sascha Reedijk
101575d819aSNiels Sascha Reedijk	The magic of the class happens in the MessageReceived() method. In your
102575d819aSNiels Sascha Reedijk	subclasses you override this method, to check the incoming BMessage.
103575d819aSNiels Sascha Reedijk	Usually, you check the \c what member of the message in a switch statement.
104575d819aSNiels Sascha Reedijk	If your handler cannot handle the object, it will pass the message on to
105575d819aSNiels Sascha Reedijk	the parent class.
106575d819aSNiels Sascha Reedijk
107*7323d0a2SKacper Kasper	\warning Don't forget to actually call the MessageReceived() method of the
1084f6e4816SNiels Sascha Reedijk		base class. Failing to do this will mean that the message chain will
1094f6e4816SNiels Sascha Reedijk		not completely be followed, which can lead to unhandled messages. There
1104f6e4816SNiels Sascha Reedijk		might be some internal system messages that the Haiku API classes
1114f6e4816SNiels Sascha Reedijk		handle, and not actually handling these messages could lead to
1124f6e4816SNiels Sascha Reedijk		inconsistent internal behavior.
113575d819aSNiels Sascha Reedijk
11451e5c73eSNiels Sascha Reedijk	\subsection app_messaging_overview_bmessenger BMessenger
115575d819aSNiels Sascha Reedijk
116575d819aSNiels Sascha Reedijk	BMessenger objects can send messages to both local and remote targets. For
117575d819aSNiels Sascha Reedijk	local targets, a BMessenger provides an advantage over directly calling
118575d819aSNiels Sascha Reedijk	the BLooper::PostMessage() method: some variants of the
119575d819aSNiels Sascha Reedijk	BMessenger::SendMessage() methods allow for synchronous replies. So, the
120575d819aSNiels Sascha Reedijk	call will actually verify the handling thread processes the message, and
121575d819aSNiels Sascha Reedijk	reply to the sender.
122575d819aSNiels Sascha Reedijk
123575d819aSNiels Sascha Reedijk	The other feature of BMessenger is that it is able to be constructed with
124575d819aSNiels Sascha Reedijk	the signature of another application as argument. This allows the messenger
125575d819aSNiels Sascha Reedijk	to pass messages to other applications. It facilitates inter-application
126575d819aSNiels Sascha Reedijk	communication.
127575d819aSNiels Sascha Reedijk
128b15c8e82SNiels Sascha Reedijk	\subsection app_messaging-overview-other Other messaging classes
129575d819aSNiels Sascha Reedijk
130575d819aSNiels Sascha Reedijk	There are several convenience classes supplied with the application kit,
131575d819aSNiels Sascha Reedijk	which can make your life easier in some specific cases.
132575d819aSNiels Sascha Reedijk
133b15c8e82SNiels Sascha Reedijk	- BInvoker binds together a message and a target. By calling
134575d819aSNiels Sascha Reedijk	  BInvoker::Invoke(), the message will be sent. This class is inherited by
135575d819aSNiels Sascha Reedijk	  the controls in the interface kit, such as BButton.
136b15c8e82SNiels Sascha Reedijk	- A BMessageRunner object will send messages to a specified target with
137575d819aSNiels Sascha Reedijk	  specified intervals in between.
138b15c8e82SNiels Sascha Reedijk	- BMessageQueue is a class that is also internally used by BLooper. It
139575d819aSNiels Sascha Reedijk	  provides a queue of messages, with convenience functions of managing
140575d819aSNiels Sascha Reedijk	  this queue.
141b15c8e82SNiels Sascha Reedijk	- BMessageFilter is the base class of the filters. Filters can be applied
142575d819aSNiels Sascha Reedijk	  to BLoopers to filter all incoming messages, or to BHandlers to filter
143575d819aSNiels Sascha Reedijk	  messages that could be handled by that object. The filter object can be
1444f6e4816SNiels Sascha Reedijk	  subclassed and extended by overriding the \link BMessageFilter::Filter()
1454f6e4816SNiels Sascha Reedijk	  Filter() \endlink method.
146575d819aSNiels Sascha Reedijk
147575d819aSNiels Sascha Reedijk	\section app-messaging-receiving Receiving Messages
148575d819aSNiels Sascha Reedijk
149575d819aSNiels Sascha Reedijk	To do...
150575d819aSNiels Sascha Reedijk
151575d819aSNiels Sascha Reedijk	\section app-messaging-sending Sending Messages
152575d819aSNiels Sascha Reedijk
153575d819aSNiels Sascha Reedijk	To do...
154575d819aSNiels Sascha Reedijk*/
155