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 enum { 37 NOT_IMPLEMENTED = B_ERROR, 38 }; 39 40 41 /*! \brief Creates an unitialized BMessenger. 42 */ 43 BMessenger::BMessenger() 44 : 45 fPort(-1), 46 fHandlerToken(B_NULL_TOKEN), 47 fTeam(-1) 48 { 49 } 50 51 52 /*! \brief Creates a BMessenger and initializes it to have the same target 53 as the supplied messemger. 54 55 \param from The messenger to be copied. 56 */ 57 BMessenger::BMessenger(const BMessenger& from) 58 : 59 fPort(from.fPort), 60 fHandlerToken(from.fHandlerToken), 61 fTeam(from.fTeam) 62 { 63 } 64 65 66 /*! \brief Frees all resources associated with this object. 67 */ 68 BMessenger::~BMessenger() 69 { 70 } 71 72 73 // #pragma mark - Operators and misc 74 75 76 /*! \brief Makes this BMessenger a copy of the supplied one. 77 78 \param from the messenger to be copied. 79 \return A reference to this object. 80 */ 81 BMessenger & 82 BMessenger::operator=(const BMessenger &from) 83 { 84 if (this != &from) { 85 fPort = from.fPort; 86 fHandlerToken = from.fHandlerToken; 87 fTeam = from.fTeam; 88 } 89 return *this; 90 } 91 92 93 /*! \brief Returns whether this and the supplied messenger have the same 94 target. 95 96 \param other The other messenger. 97 \return \c true, if the messengers have the same target or if both aren't 98 properly initialzed, \c false otherwise. 99 */ 100 bool 101 BMessenger::operator==(const BMessenger &other) const 102 { 103 // Note: The fTeam fields are not compared. 104 return fPort == other.fPort 105 && fHandlerToken == other.fHandlerToken; 106 } 107 108 109 /*! \brief Returns whether the messenger's target looper does still exist. 110 111 It is not checked whether the target handler is also still existing. 112 113 \return \c true, if the messenger's target looper does still exist, 114 \c false otherwise. 115 */ 116 bool 117 BMessenger::IsValid() const 118 { 119 return fPort >= 0; 120 } 121 122 123 /*! \brief Returns the ID of the team the messenger's target lives in. 124 125 \return The team of the messenger's target. 126 */ 127 team_id 128 BMessenger::Team() const 129 { 130 return fTeam; 131 } 132 133 134 // #pragma mark - Private or reserved 135 136 137 /*! \brief Sets the messenger's team, target looper port and handler token. 138 139 To target the preferred handler, use B_PREFERRED_TOKEN as token. 140 141 \param team The target's team. 142 \param port The target looper port. 143 \param token The target handler token. 144 */ 145 void 146 BMessenger::_SetTo(team_id team, port_id port, int32 token) 147 { 148 fTeam = team; 149 fPort = port; 150 fHandlerToken = token; 151 } 152 153 154 /*! \brief Returns whether the first one of two BMessengers is less than the 155 second one. 156 157 This method defines an order on BMessengers based on their member 158 variables \c fPort, \c fHandlerToken and \c fPreferredTarget. 159 160 \param a The first messenger. 161 \param b The second messenger. 162 \return \c true, if \a a is less than \a b, \c false otherwise. 163 */ 164 bool 165 operator<(const BMessenger &_a, const BMessenger &_b) 166 { 167 BMessenger::Private a(const_cast<BMessenger&>(_a)); 168 BMessenger::Private b(const_cast<BMessenger&>(_b)); 169 170 // significance: 171 // 1. fPort 172 // 2. fHandlerToken 173 // 3. fPreferredTarget 174 // fTeam is insignificant 175 return (a.Port() < b.Port() 176 || (a.Port() == b.Port() 177 && (a.Token() < b.Token() 178 || (a.Token() == b.Token() 179 && !a.IsPreferredTarget() 180 && b.IsPreferredTarget())))); 181 } 182 183 184 /*! \brief Returns whether two BMessengers have not the same target. 185 186 \param a The first messenger. 187 \param b The second messenger. 188 \return \c false, if \a a and \a b have the same targets or are both not 189 properly initialized, \c true otherwise. 190 */ 191 bool 192 operator!=(const BMessenger &a, const BMessenger &b) 193 { 194 return !(a == b); 195 } 196 197