1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2001-2005, Haiku 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a 5 // copy of this software and associated documentation files (the "Software"), 6 // to deal in the Software without restriction, including without limitation 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 // and/or sell copies of the Software, and to permit persons to whom the 9 // Software is furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 // DEALINGS IN THE SOFTWARE. 21 // 22 // File Name: Messenger.h 23 // Author: Ingo Weinhold (bonefish@users.sf.net) 24 // Description: BMessenger delivers messages to local or remote targets. 25 //------------------------------------------------------------------------------ 26 27 // debugging 28 //#define DBG(x) x 29 #define DBG(x) 30 #define OUT printf 31 32 // Standard Includes ----------------------------------------------------------- 33 #include <new> 34 #include <stdio.h> 35 #include <string.h> 36 37 // System Includes ------------------------------------------------------------- 38 #include <Application.h> 39 #include <Handler.h> 40 #include <Looper.h> 41 #include <LooperList.h> 42 #include <Message.h> 43 #include <Messenger.h> 44 #include <MessengerPrivate.h> 45 #include <OS.h> 46 #include <Roster.h> 47 #include <TokenSpace.h> 48 49 // Project Includes ------------------------------------------------------------ 50 #include <AppMisc.h> 51 #include <MessageUtils.h> 52 #include "ObjectLocker.h" 53 #include "TokenSpace.h" 54 55 // Local Includes -------------------------------------------------------------- 56 57 // Local Defines --------------------------------------------------------------- 58 59 // Globals --------------------------------------------------------------------- 60 61 using BPrivate::gDefaultTokens; 62 using BPrivate::gLooperList; 63 using BPrivate::BLooperList; 64 using BPrivate::BObjectLocker; 65 66 enum { 67 NOT_IMPLEMENTED = B_ERROR, 68 }; 69 70 71 // constructor 72 /*! \brief Creates an unitialized BMessenger. 73 */ 74 BMessenger::BMessenger() 75 : fPort(-1), 76 fHandlerToken(B_NULL_TOKEN), 77 fTeam(-1), 78 fPreferredTarget(false) 79 { 80 } 81 82 // copy constructor 83 /*! \brief Creates a BMessenger and initializes it to have the same target 84 as the supplied messemger. 85 86 \param from The messenger to be copied. 87 */ 88 BMessenger::BMessenger(const BMessenger &from) 89 : fPort(from.fPort), 90 fHandlerToken(from.fHandlerToken), 91 fTeam(from.fTeam), 92 fPreferredTarget(from.fPreferredTarget) 93 { 94 } 95 96 // destructor 97 /*! \brief Frees all resources associated with this object. 98 */ 99 BMessenger::~BMessenger() 100 { 101 } 102 103 104 // Operators and misc 105 106 // = 107 /*! \brief Makes this BMessenger a copy of the supplied one. 108 109 \param from the messenger to be copied. 110 \return A reference to this object. 111 */ 112 BMessenger & 113 BMessenger::operator=(const BMessenger &from) 114 { 115 if (this != &from) { 116 fPort = from.fPort; 117 fHandlerToken = from.fHandlerToken; 118 fTeam = from.fTeam; 119 fPreferredTarget = from.fPreferredTarget; 120 } 121 return *this; 122 } 123 124 // == 125 /*! \brief Returns whether this and the supplied messenger have the same 126 target. 127 128 \param other The other messenger. 129 \return \c true, if the messengers have the same target or if both aren't 130 properly initialzed, \c false otherwise. 131 */ 132 bool 133 BMessenger::operator==(const BMessenger &other) const 134 { 135 // Note: The fTeam fields are not compared. 136 return (fPort == other.fPort 137 && fHandlerToken == other.fHandlerToken 138 && fPreferredTarget == other.fPreferredTarget); 139 } 140 141 // IsValid 142 /*! \brief Returns whether the messenger's target looper does still exist. 143 144 It is not checked whether the target handler is also still existing. 145 146 \return \c true, if the messenger's target looper does still exist, 147 \c false otherwise. 148 */ 149 bool 150 BMessenger::IsValid() const 151 { 152 port_info info; 153 // return (fPort >= 0 && get_port_info(fPort, &info) == B_OK); 154 return (fPort >= 0); 155 } 156 157 // Team 158 /*! \brief Returns the ID of the team the messenger's target lives in. 159 160 \return The team of the messenger's target. 161 */ 162 team_id 163 BMessenger::Team() const 164 { 165 return fTeam; 166 } 167 168 169 //----- Private or reserved ----------------------------------------- 170 171 // SetTo 172 /*! \brief Sets the messenger's team, target looper port and handler token. 173 174 If \a preferred is \c true, \a token is ignored. 175 176 \param team The target's team. 177 \param port The target looper port. 178 \param token The target handler token. 179 \param preferred \c true to rather use the looper's preferred handler 180 instead of the one specified by \a token. 181 */ 182 void 183 BMessenger::SetTo(team_id team, port_id port, int32 token, bool preferred) 184 { 185 fTeam = team; 186 fPort = port; 187 fHandlerToken = (preferred ? B_PREFERRED_TOKEN : token); 188 fPreferredTarget = preferred; 189 } 190 191 // < 192 /*! \brief Returns whether the first one of two BMessengers is less than the 193 second one. 194 195 This method defines an order on BMessengers based on their member 196 variables \c fPort, \c fHandlerToken and \c fPreferredTarget. 197 198 \param a The first messenger. 199 \param b The second messenger. 200 \return \c true, if \a a is less than \a b, \c false otherwise. 201 */ 202 bool 203 operator<(const BMessenger &_a, const BMessenger &_b) 204 { 205 BMessenger::Private a(const_cast<BMessenger&>(_a)); 206 BMessenger::Private b(const_cast<BMessenger&>(_b)); 207 208 209 // significance: 210 // 1. fPort 211 // 2. fHandlerToken 212 // 3. fPreferredTarget 213 // fTeam is insignificant 214 return (a.Port() < b.Port() 215 || a.Port() == b.Port() 216 && (a.Token() < b.Token() 217 || a.Token() == b.Token() 218 && !a.IsPreferredTarget() 219 && b.IsPreferredTarget())); 220 } 221 222 // != 223 /*! \brief Returns whether two BMessengers have not the same target. 224 225 \param a The first messenger. 226 \param b The second messenger. 227 \return \c false, if \a a and \a b have the same targets or are both not 228 properly initialized, \c true otherwise. 229 */ 230 bool 231 operator!=(const BMessenger &a, const BMessenger &b) 232 { 233 return !(a == b); 234 } 235 236