2 * Copyright 2001-2010 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Ingo Weinhold, ingo_weinhold@gmx.de
10 #include <MessageRunner.h>
12 #include <Application.h>
14 #include <RegistrarDefs.h>
16 #include <RosterPrivate.h>
19 using namespace BPrivate
;
22 BMessageRunner::BMessageRunner(BMessenger target
, const BMessage
* message
,
23 bigtime_t interval
, int32 count
)
27 _InitData(target
, message
, interval
, count
, be_app_messenger
);
31 BMessageRunner::BMessageRunner(BMessenger target
, const BMessage
& message
,
32 bigtime_t interval
, int32 count
)
36 _InitData(target
, &message
, interval
, count
, be_app_messenger
);
40 BMessageRunner::BMessageRunner(BMessenger target
, const BMessage
* message
,
41 bigtime_t interval
, int32 count
, BMessenger replyTo
)
45 _InitData(target
, message
, interval
, count
, replyTo
);
49 BMessageRunner::BMessageRunner(BMessenger target
, const BMessage
& message
,
50 bigtime_t interval
, int32 count
, BMessenger replyTo
)
54 _InitData(target
, &message
, interval
, count
, replyTo
);
58 BMessageRunner::~BMessageRunner()
63 // compose the request message
64 BMessage
request(B_REG_UNREGISTER_MESSAGE_RUNNER
);
65 status_t result
= request
.AddInt32("token", fToken
);
70 result
= BRoster::Private().SendTo(&request
, &reply
, false);
72 // ignore the reply, we can't do anything anyway
77 BMessageRunner::InitCheck() const
79 return fToken
>= 0 ? B_OK
: fToken
;
84 BMessageRunner::SetInterval(bigtime_t interval
)
86 return _SetParams(true, interval
, false, 0);
91 BMessageRunner::SetCount(int32 count
)
93 return _SetParams(false, 0, true, count
);
98 BMessageRunner::GetInfo(bigtime_t
* interval
, int32
* count
) const
100 status_t result
= fToken
>= 0 ? B_OK
: B_BAD_VALUE
;
102 // compose the request message
103 BMessage
request(B_REG_GET_MESSAGE_RUNNER_INFO
);
105 result
= request
.AddInt32("token", fToken
);
110 result
= BRoster::Private().SendTo(&request
, &reply
, false);
112 // evaluate the reply
113 if (result
== B_OK
) {
114 if (reply
.what
== B_REG_SUCCESS
) {
117 if (reply
.FindInt32("count", &_count
) == B_OK
) {
125 if (reply
.FindInt64("interval", &_interval
) == B_OK
) {
127 *interval
= _interval
;
131 if (reply
.FindInt32("error", &result
) != B_OK
)
141 BMessageRunner::StartSending(BMessenger target
, const BMessage
* message
,
142 bigtime_t interval
, int32 count
)
144 int32 token
= _RegisterRunner(target
, message
, interval
, count
, true,
147 return token
>= B_OK
? B_OK
: token
;
152 BMessageRunner::StartSending(BMessenger target
, const BMessage
* message
,
153 bigtime_t interval
, int32 count
, BMessenger replyTo
)
155 int32 token
= _RegisterRunner(target
, message
, interval
, count
, true,
158 return token
>= B_OK
? B_OK
: token
;
163 void BMessageRunner::_ReservedMessageRunner1() {}
164 void BMessageRunner::_ReservedMessageRunner2() {}
165 void BMessageRunner::_ReservedMessageRunner3() {}
166 void BMessageRunner::_ReservedMessageRunner4() {}
167 void BMessageRunner::_ReservedMessageRunner5() {}
168 void BMessageRunner::_ReservedMessageRunner6() {}
171 //! Privatized copy constructor to prevent usage.
172 BMessageRunner::BMessageRunner(const BMessageRunner
&)
179 //! Privatized assignment operator to prevent usage.
181 BMessageRunner::operator=(const BMessageRunner
&)
187 /*! Initializes the BMessageRunner.
189 The success of the initialization can (and should) be asked for via
192 \note As soon as the last message has been sent, the message runner
193 becomes unusable. InitCheck() will still return \c B_OK, but
194 SetInterval(), SetCount() and GetInfo() will fail.
196 \param target Target of the message(s).
197 \param message The message to be sent to the target.
198 \param interval Period of time before the first message is sent and
199 between messages (if more than one shall be sent) in microseconds.
200 \param count Specifies how many times the message shall be sent.
201 A value less than \c 0 for an unlimited number of repetitions.
202 \param replyTo Target replies to the delivered message(s) shall be sent to.
205 BMessageRunner::_InitData(BMessenger target
, const BMessage
* message
,
206 bigtime_t interval
, int32 count
, BMessenger replyTo
)
208 fToken
= _RegisterRunner(target
, message
, interval
, count
, false, replyTo
);
212 /*! Registers the BMessageRunner in the registrar.
214 \param target Target of the message(s).
215 \param message The message to be sent to the target.
216 \param interval Period of time before the first message is sent and
217 between messages (if more than one shall be sent) in microseconds.
218 \param count Specifies how many times the message shall be sent.
219 A value less than \c 0 for an unlimited number of repetitions.
220 \param replyTo Target replies to the delivered message(s) shall be sent to.
222 \return The token the message runner is registered with, or the error code
223 while trying to register it.
226 BMessageRunner::_RegisterRunner(BMessenger target
, const BMessage
* message
,
227 bigtime_t interval
, int32 count
, bool detach
, BMessenger replyTo
)
229 status_t result
= B_OK
;
230 if (message
== NULL
|| count
== 0 || (count
< 0 && detach
))
231 result
= B_BAD_VALUE
;
233 // compose the request message
234 BMessage
request(B_REG_REGISTER_MESSAGE_RUNNER
);
236 result
= request
.AddInt32("team", BPrivate::current_team());
239 result
= request
.AddMessenger("target", target
);
242 result
= request
.AddMessage("message", message
);
245 result
= request
.AddInt64("interval", interval
);
248 result
= request
.AddInt32("count", count
);
251 result
= request
.AddMessenger("reply_target", replyTo
);
256 result
= BRoster::Private().SendTo(&request
, &reply
, false);
260 // evaluate the reply
261 if (result
== B_OK
) {
262 if (reply
.what
== B_REG_SUCCESS
) {
263 if (reply
.FindInt32("token", &token
) != B_OK
)
266 if (reply
.FindInt32("error", &result
) != B_OK
)
278 /*! Sets the message runner's interval and count parameters.
280 The parameters \a resetInterval and \a resetCount specify whether
281 the interval or the count parameter respectively shall be reset.
283 At least one parameter must be set, otherwise the methods returns
286 \param resetInterval \c true, if the interval shall be reset, \c false
287 otherwise -- then \a interval is ignored.
288 \param interval The new interval in microseconds.
289 \param resetCount \c true, if the count shall be reset, \c false
290 otherwise -- then \a count is ignored.
291 \param count Specifies how many times the message shall be sent.
292 A value less than \c 0 for an unlimited number of repetitions.
294 \return A status code.
295 \retval B_OK Everything went fine.
296 \retval B_BAD_VALUE The message runner is not longer valid. All the
297 messages that had to be sent have already been sent. Or both
298 \a resetInterval and \a resetCount are \c false.
301 BMessageRunner::_SetParams(bool resetInterval
, bigtime_t interval
,
302 bool resetCount
, int32 count
)
304 if ((!resetInterval
&& !resetCount
) || fToken
< 0)
307 // compose the request message
308 BMessage
request(B_REG_SET_MESSAGE_RUNNER_PARAMS
);
309 status_t result
= request
.AddInt32("token", fToken
);
310 if (result
== B_OK
&& resetInterval
)
311 result
= request
.AddInt64("interval", interval
);
313 if (result
== B_OK
&& resetCount
)
314 result
= request
.AddInt32("count", count
);
319 result
= BRoster::Private().SendTo(&request
, &reply
, false);
321 // evaluate the reply
322 if (result
== B_OK
) {
323 if (reply
.what
!= B_REG_SUCCESS
) {
324 if (reply
.FindInt32("error", &result
) != B_OK
)