#include <Message.h>
#include "SMLooper.h"
#include "SMMessages.h"
SMLooper::SMLooper()
: BLooper(NULL, B_NORMAL_PRIORITY, 1),
fUnblockTime(0),
fReplyDelay(0),
fDeliveryTime(-1),
fReplyTime(-1)
{
}
SMLooper::~SMLooper()
{
}
void
SMLooper::MessageReceived(BMessage *message)
{
switch (message->what) {
case MSG_BLOCK:
{
bigtime_t now = system_time();
if (now < fUnblockTime) {
PostMessage(MSG_UNBLOCK, this);
snooze_until(fUnblockTime, B_SYSTEM_TIMEBASE);
}
break;
}
case MSG_UNBLOCK:
break;
case MSG_TEST:
SetDeliveryTime(system_time());
if (fReplyDelay > 0)
snooze(fReplyDelay);
message->SendReply(MSG_REPLY);
break;
case MSG_REPLY:
fReplyTime = system_time();
break;
default:
BLooper::MessageReceived(message);
break;
}
}
void
SMLooper::BlockUntil(bigtime_t unblockTime)
{
fUnblockTime = unblockTime;
PostMessage(MSG_BLOCK, this);
}
void
SMLooper::SetReplyDelay(bigtime_t replyDelay)
{
fReplyDelay = replyDelay;
}
bigtime_t
SMLooper::ReplyDelay() const
{
return fReplyDelay;
}
bool
SMLooper::DeliverySuccess() const
{
return (fDeliveryTime >= 0);
}
void
SMLooper::SetDeliveryTime(bigtime_t deliveryTime)
{
fDeliveryTime = deliveryTime;
}
bigtime_t
SMLooper::DeliveryTime() const
{
return fDeliveryTime;
}
bool
SMLooper::ReplySuccess() const
{
return (fReplyTime >= 0);
}
void
SMLooper::SetReplyTime(bigtime_t replyTime)
{
fReplyTime = replyTime;
}
bigtime_t
SMLooper::ReplyTime() const
{
return fReplyTime;
}
SMHandler::SMHandler()
: BHandler()
{
}
void
SMHandler::MessageReceived(BMessage *message)
{
switch (message->what) {
case MSG_TEST:
if (SMLooper *looper = dynamic_cast<SMLooper*>(Looper())) {
looper->SetDeliveryTime(system_time());
if (looper->ReplyDelay() > 0)
snooze(looper->ReplyDelay());
}
message->SendReply(MSG_REPLY);
break;
case MSG_REPLY:
if (SMLooper *looper = dynamic_cast<SMLooper*>(Looper()))
looper->SetReplyTime(system_time());
break;
default:
BHandler::MessageReceived(message);
break;
}
}