Implemented BMessageRunner class.
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1502 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
121f83dd58
commit
6f1a85effd
@ -24,11 +24,11 @@
|
||||
// Description: A BMessageRunner periodically sends a message to a
|
||||
// specified target.
|
||||
//------------------------------------------------------------------------------
|
||||
#include <Application.h>
|
||||
#include <AppMisc.h>
|
||||
#include <MessageRunner.h>
|
||||
|
||||
enum {
|
||||
NOT_IMPLEMENTED = B_ERROR
|
||||
};
|
||||
#include <RegistrarDefs.h>
|
||||
#include <Roster.h>
|
||||
|
||||
// constructor
|
||||
/*! \brief Creates and initializes a new BMessageRunner.
|
||||
@ -53,7 +53,7 @@ BMessageRunner::BMessageRunner(BMessenger target, const BMessage *message,
|
||||
bigtime_t interval, int32 count)
|
||||
: fToken(-1)
|
||||
{
|
||||
// not implemented
|
||||
InitData(target, message, interval, count, be_app_messenger);
|
||||
}
|
||||
|
||||
// constructor
|
||||
@ -82,7 +82,7 @@ BMessageRunner::BMessageRunner(BMessenger target, const BMessage *message,
|
||||
BMessenger replyTo)
|
||||
: fToken(-1)
|
||||
{
|
||||
// not implemented
|
||||
InitData(target, message, interval, count, replyTo);
|
||||
}
|
||||
|
||||
// destructor
|
||||
@ -90,7 +90,16 @@ BMessageRunner::BMessageRunner(BMessenger target, const BMessage *message,
|
||||
*/
|
||||
BMessageRunner::~BMessageRunner()
|
||||
{
|
||||
// not implemented
|
||||
status_t error = B_OK;
|
||||
// compose the request message
|
||||
BMessage request(B_REG_UNREGISTER_MESSAGE_RUNNER);
|
||||
if (error == B_OK)
|
||||
error = request.AddInt32("token", fToken);
|
||||
// send the request
|
||||
BMessage reply;
|
||||
if (error == B_OK)
|
||||
error = _send_to_roster_(&request, &reply, false);
|
||||
// ignore the reply, we can't do anything anyway
|
||||
}
|
||||
|
||||
// InitCheck
|
||||
@ -106,7 +115,7 @@ BMessageRunner::~BMessageRunner()
|
||||
status_t
|
||||
BMessageRunner::InitCheck() const
|
||||
{
|
||||
return NOT_IMPLEMENTED; // not implemented
|
||||
return (fToken >= 0 ? B_OK : fToken);
|
||||
}
|
||||
|
||||
// SetInterval
|
||||
@ -121,7 +130,7 @@ BMessageRunner::InitCheck() const
|
||||
status_t
|
||||
BMessageRunner::SetInterval(bigtime_t interval)
|
||||
{
|
||||
return NOT_IMPLEMENTED; // not implemented
|
||||
return SetParams(true, interval, false, 0);
|
||||
}
|
||||
|
||||
// SetCount
|
||||
@ -137,7 +146,7 @@ BMessageRunner::SetInterval(bigtime_t interval)
|
||||
status_t
|
||||
BMessageRunner::SetCount(int32 count)
|
||||
{
|
||||
return NOT_IMPLEMENTED; // not implemented
|
||||
return SetParams(false, 0, true, count);
|
||||
}
|
||||
|
||||
// GetInfo
|
||||
@ -155,7 +164,36 @@ BMessageRunner::SetCount(int32 count)
|
||||
status_t
|
||||
BMessageRunner::GetInfo(bigtime_t *interval, int32 *count) const
|
||||
{
|
||||
return NOT_IMPLEMENTED; // not implemented
|
||||
status_t error = (fToken >= 0 ? B_OK : B_BAD_VALUE);
|
||||
// compose the request message
|
||||
BMessage request(B_REG_GET_MESSAGE_RUNNER_INFO);
|
||||
if (error == B_OK)
|
||||
error = request.AddInt32("token", fToken);
|
||||
// send the request
|
||||
BMessage reply;
|
||||
if (error == B_OK)
|
||||
error = _send_to_roster_(&request, &reply, false);
|
||||
// evaluate the reply
|
||||
if (error == B_OK) {
|
||||
if (reply.what == B_REG_SUCCESS) {
|
||||
// count
|
||||
int32 _count;
|
||||
if (reply.FindInt32("count", &_count) == B_OK) {
|
||||
if (count)
|
||||
*count = _count;
|
||||
} else
|
||||
error = B_ERROR;
|
||||
// interval
|
||||
bigtime_t _interval;
|
||||
if (reply.FindInt64("interval", &_interval) == B_OK) {
|
||||
if (interval)
|
||||
*interval = _interval;
|
||||
} else
|
||||
error = B_ERROR;
|
||||
} else
|
||||
reply.FindInt32("error", &error);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
// FBC
|
||||
@ -184,11 +222,58 @@ BMessageRunner::operator=(const BMessageRunner &)
|
||||
}
|
||||
|
||||
// InitData
|
||||
/*! \brief Initializes the BMessageRunner.
|
||||
|
||||
The success of the initialization can (and should) be asked for via
|
||||
InitCheck().
|
||||
|
||||
\note As soon as the last message has been sent, the message runner
|
||||
becomes unusable. InitCheck() will still return \c B_OK, but
|
||||
SetInterval(), SetCount() and GetInfo() will fail.
|
||||
|
||||
\param target Target of the message(s).
|
||||
\param message The message to be sent to the target.
|
||||
\param interval Period of time before the first message is sent and
|
||||
between messages (if more than one shall be sent) in microseconds.
|
||||
\param count Specifies how many times the message shall be sent.
|
||||
A value less than \c 0 for an unlimited number of repetitions.
|
||||
\param replyTo Target replies to the delivered message(s) shall be sent to.
|
||||
*/
|
||||
void
|
||||
BMessageRunner::InitData(BMessenger target, const BMessage *message,
|
||||
bigtime_t interval, int32 count, BMessenger replyTo)
|
||||
{
|
||||
// not implemented
|
||||
status_t error = (message ? B_OK : B_BAD_VALUE);
|
||||
if (error == B_OK && count == 0)
|
||||
error = B_ERROR;
|
||||
// compose the request message
|
||||
BMessage request(B_REG_REGISTER_MESSAGE_RUNNER);
|
||||
if (error == B_OK)
|
||||
error = request.AddInt32("team", BPrivate::current_team());
|
||||
if (error == B_OK)
|
||||
error = request.AddMessenger("target", target);
|
||||
if (error == B_OK)
|
||||
error = request.AddMessage("message", message);
|
||||
if (error == B_OK)
|
||||
error = request.AddInt64("interval", interval);
|
||||
if (error == B_OK)
|
||||
error = request.AddInt32("count", count);
|
||||
if (error == B_OK)
|
||||
error = request.AddMessenger("reply_target", replyTo);
|
||||
// send the request
|
||||
BMessage reply;
|
||||
if (error == B_OK)
|
||||
error = _send_to_roster_(&request, &reply, false);
|
||||
// evaluate the reply
|
||||
if (error == B_OK) {
|
||||
if (reply.what == B_REG_SUCCESS) {
|
||||
if (reply.FindInt32("token", &fToken) != B_OK)
|
||||
error = B_ERROR;
|
||||
} else
|
||||
reply.FindInt32("error", &error);
|
||||
}
|
||||
if (error != B_OK)
|
||||
fToken = error;
|
||||
}
|
||||
|
||||
// SetParams
|
||||
@ -196,6 +281,25 @@ status_t
|
||||
BMessageRunner::SetParams(bool resetInterval, bigtime_t interval,
|
||||
bool resetCount, int32 count)
|
||||
{
|
||||
return NOT_IMPLEMENTED; // not implemented
|
||||
status_t error = ((resetInterval || resetCount) && fToken >= 0
|
||||
? B_OK : B_BAD_VALUE);
|
||||
// compose the request message
|
||||
BMessage request(B_REG_SET_MESSAGE_RUNNER_PARAMS);
|
||||
if (error == B_OK)
|
||||
error = request.AddInt32("token", fToken);
|
||||
if (error == B_OK && resetInterval)
|
||||
error = request.AddInt64("interval", interval);
|
||||
if (error == B_OK && resetCount)
|
||||
error = request.AddInt32("count", count);
|
||||
// send the request
|
||||
BMessage reply;
|
||||
if (error == B_OK)
|
||||
error = _send_to_roster_(&request, &reply, false);
|
||||
// evaluate the reply
|
||||
if (error == B_OK) {
|
||||
if (reply.what != B_REG_SUCCESS)
|
||||
reply.FindInt32("error", &error);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user