2 * Copyright 1999, Be Incorporated.
3 * Copyright (c) 1999-2000, Eric Moon.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions, and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ////////////////////////////////////////////////////////////
36 // Implements the MultiInvoker class.
39 #include <Messenger.h>
40 #include "MultiInvoker.h"
42 MultiInvoker::MultiInvoker()
45 m_timeout
= B_INFINITE_TIMEOUT
;
49 MultiInvoker::MultiInvoker(const MultiInvoker
& src
)
54 MultiInvoker::~MultiInvoker()
59 MultiInvoker
& MultiInvoker::operator=(const MultiInvoker
& src
)
68 void MultiInvoker::Clear()
71 int32 i
=CountTargets();
77 void MultiInvoker::Clone(const MultiInvoker
& src
)
79 m_message
= new BMessage(*src
.Message());
80 int32 len
=src
.CountTargets();
81 for (int32 i
=0; i
<len
; i
++) {
82 AddTarget(src
.TargetAt(i
));
84 m_timeout
= src
.Timeout();
85 m_replyHandler
= src
.HandlerForReply();
88 void MultiInvoker::SetMessage(BMessage
* message
)
94 BMessage
* MultiInvoker::Message() const
99 uint32
MultiInvoker::Command() const
101 return (m_message
) ? m_message
->what
: 0;
104 status_t
MultiInvoker::AddTarget(const BHandler
* h
, const BLooper
* loop
)
107 BMessenger
* msgr
= new BMessenger(h
, loop
, &err
);
109 m_messengers
.AddItem(msgr
);
115 status_t
MultiInvoker::AddTarget(BMessenger
* msgr
)
118 m_messengers
.AddItem(msgr
);
125 void MultiInvoker::RemoveTarget(const BHandler
* h
)
127 int32 i
= IndexOfTarget(h
);
132 void MultiInvoker::RemoveTarget(int32 index
)
134 BMessenger
* msgr
= static_cast<BMessenger
*>
135 (m_messengers
.RemoveItem(index
));
139 int32
MultiInvoker::IndexOfTarget(const BHandler
* h
) const
141 int32 len
= CountTargets();
142 for (int32 i
=0; i
<len
; i
++) {
143 BMessenger
* msgr
= MessengerAt(i
);
144 if (msgr
&& msgr
->Target(0) == h
) {
151 int32
MultiInvoker::CountTargets() const
153 return m_messengers
.CountItems();
156 BHandler
* MultiInvoker::TargetAt(int32 index
, BLooper
** looper
) const
158 BMessenger
* msgr
= MessengerAt(index
);
160 return msgr
->Target(looper
);
162 if (looper
) *looper
= 0;
167 BMessenger
* MultiInvoker::MessengerAt(int32 index
) const
169 return static_cast<BMessenger
*>
170 (m_messengers
.ItemAt(index
));
173 bool MultiInvoker::IsTargetLocal(int32 index
) const
175 BMessenger
* msgr
= MessengerAt(index
);
176 return (msgr
) ? msgr
->IsTargetLocal() : false;
179 void MultiInvoker::SetTimeout(bigtime_t timeout
)
184 bigtime_t
MultiInvoker::Timeout() const
189 void MultiInvoker::SetHandlerForReply(BHandler
* h
)
194 BHandler
* MultiInvoker::HandlerForReply() const
196 return m_replyHandler
;
199 status_t
MultiInvoker::Invoke(BMessage
* msg
)
201 BMessage
* sendMsg
= (msg
) ? msg
: m_message
;
205 status_t err
, finalResult
=B_OK
;
207 int32 len
= CountTargets();
208 for (int32 i
=0; i
<len
; i
++) {
209 BMessenger
* msgr
= MessengerAt(i
);
211 err
= msgr
->SendMessage(sendMsg
,
212 HandlerForReply(), m_timeout
);
213 if (err
!= B_OK
) finalResult
= err
;