1/*
2 * Copyright 2001-2007, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Ingo Weinhold (bonefish@users.sf.net)
7 */
8
9
10#include <AppMisc.h>
11#include <MessageUtils.h>
12#include "TokenSpace.h"
13
14#include <Application.h>
15#include <Handler.h>
16#include <Looper.h>
17#include <LooperList.h>
18#include <Message.h>
19#include <MessagePrivate.h>
20#include <Messenger.h>
21#include <OS.h>
22#include <Roster.h>
23#include <TokenSpace.h>
24
25#include <new>
26#include <stdio.h>
27#include <string.h>
28
29
30// debugging
31//#define DBG(x) x
32#define DBG(x)
33#define OUT	printf
34
35
36/*!	\brief Creates an unitialized BMessenger.
37*/
38BMessenger::BMessenger()
39	:
40	fPort(-1),
41	fHandlerToken(B_NULL_TOKEN),
42	fTeam(-1)
43{
44}
45
46
47/*!	\brief Creates a BMessenger and initializes it to have the same target
48	as the supplied messemger.
49
50	\param from The messenger to be copied.
51*/
52BMessenger::BMessenger(const BMessenger& from)
53	:
54	fPort(from.fPort),
55	fHandlerToken(from.fHandlerToken),
56	fTeam(from.fTeam)
57{
58}
59
60
61/*!	\brief Frees all resources associated with this object.
62*/
63BMessenger::~BMessenger()
64{
65}
66
67
68//	#pragma mark - Operators and misc
69
70
71/*!	\brief Makes this BMessenger a copy of the supplied one.
72
73	\param from the messenger to be copied.
74	\return A reference to this object.
75*/
76BMessenger &
77BMessenger::operator=(const BMessenger &from)
78{
79	if (this != &from) {
80		fPort = from.fPort;
81		fHandlerToken = from.fHandlerToken;
82		fTeam = from.fTeam;
83	}
84	return *this;
85}
86
87
88/*!	\brief Returns whether this and the supplied messenger have the same
89	target.
90
91	\param other The other messenger.
92	\return \c true, if the messengers have the same target or if both aren't
93			properly initialzed, \c false otherwise.
94*/
95bool
96BMessenger::operator==(const BMessenger &other) const
97{
98	// Note: The fTeam fields are not compared.
99	return fPort == other.fPort
100		&& fHandlerToken == other.fHandlerToken;
101}
102
103
104/*!	\brief Returns whether the messenger's target looper does still exist.
105
106	It is not checked whether the target handler is also still existing.
107
108	\return \c true, if the messenger's target looper does still exist,
109			\c false otherwise.
110*/
111bool
112BMessenger::IsValid() const
113{
114	return fPort >= 0;
115}
116
117
118/*!	\brief Returns the ID of the team the messenger's target lives in.
119
120	\return The team of the messenger's target.
121*/
122team_id
123BMessenger::Team() const
124{
125	return fTeam;
126}
127
128
129//	#pragma mark - Private or reserved
130
131
132/*!	\brief Sets the messenger's team, target looper port and handler token.
133
134	To target the preferred handler, use B_PREFERRED_TOKEN as token.
135
136	\param team The target's team.
137	\param port The target looper port.
138	\param token The target handler token.
139*/
140void
141BMessenger::_SetTo(team_id team, port_id port, int32 token)
142{
143	fTeam = team;
144	fPort = port;
145	fHandlerToken = token;
146}
147
148
149/*!	\brief Returns whether the first one of two BMessengers is less than the
150	second one.
151
152	This method defines an order on BMessengers based on their member
153	variables \c fPort, \c fHandlerToken and \c fPreferredTarget.
154
155	\param a The first messenger.
156	\param b The second messenger.
157	\return \c true, if \a a is less than \a b, \c false otherwise.
158*/
159bool
160operator<(const BMessenger &_a, const BMessenger &_b)
161{
162	BMessenger::Private a(const_cast<BMessenger&>(_a));
163	BMessenger::Private b(const_cast<BMessenger&>(_b));
164
165	// significance:
166	// 1. fPort
167	// 2. fHandlerToken
168	// 3. fPreferredTarget
169	// fTeam is insignificant
170	return (a.Port() < b.Port()
171			|| (a.Port() == b.Port()
172				&& (a.Token() < b.Token()
173					|| (a.Token() == b.Token()
174						&& !a.IsPreferredTarget()
175						&& b.IsPreferredTarget()))));
176}
177
178
179/*!	\brief Returns whether two BMessengers have not the same target.
180
181	\param a The first messenger.
182	\param b The second messenger.
183	\return \c false, if \a a and \a b have the same targets or are both not
184			properly initialized, \c true otherwise.
185*/
186bool
187operator!=(const BMessenger &a, const BMessenger &b)
188{
189	return !(a == b);
190}
191
192