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