1 // SMLooper.cpp 2 3 #include <Message.h> 4 5 #include "SMLooper.h" 6 #include "SMMessages.h" 7 8 // SMLooper 9 10 // constructor 11 SMLooper::SMLooper() 12 : BLooper(NULL, B_NORMAL_PRIORITY, 1), 13 fUnblockTime(0), 14 fReplyDelay(0), 15 fDeliveryTime(-1), 16 fReplyTime(-1) 17 { 18 } 19 20 // destructor 21 SMLooper::~SMLooper() 22 { 23 } 24 25 // MessageReceived 26 void 27 SMLooper::MessageReceived(BMessage *message) 28 { 29 switch (message->what) { 30 case MSG_BLOCK: 31 { 32 bigtime_t now = system_time(); 33 if (now < fUnblockTime) { 34 // port capacity is 1 => the following message blocks the 35 // port until return from MessageReceived(). 36 PostMessage(MSG_UNBLOCK, this); 37 snooze_until(fUnblockTime, B_SYSTEM_TIMEBASE); 38 } 39 break; 40 } 41 case MSG_UNBLOCK: 42 break; 43 case MSG_TEST: 44 SetDeliveryTime(system_time()); 45 if (fReplyDelay > 0) 46 snooze(fReplyDelay); 47 message->SendReply(MSG_REPLY); 48 break; 49 case MSG_REPLY: 50 fReplyTime = system_time(); 51 break; 52 default: 53 BLooper::MessageReceived(message); 54 break; 55 } 56 } 57 58 // BlockUntil 59 void 60 SMLooper::BlockUntil(bigtime_t unblockTime) 61 { 62 fUnblockTime = unblockTime; 63 PostMessage(MSG_BLOCK, this); 64 } 65 66 // SetReplyDelay 67 void 68 SMLooper::SetReplyDelay(bigtime_t replyDelay) 69 { 70 fReplyDelay = replyDelay; 71 } 72 73 // ReplyDelay 74 bigtime_t 75 SMLooper::ReplyDelay() const 76 { 77 return fReplyDelay; 78 } 79 80 // DeliverySuccess 81 bool 82 SMLooper::DeliverySuccess() const 83 { 84 return (fDeliveryTime >= 0); 85 } 86 87 // SetDeliveryTime 88 void 89 SMLooper::SetDeliveryTime(bigtime_t deliveryTime) 90 { 91 fDeliveryTime = deliveryTime; 92 } 93 94 // DeliveryTime 95 bigtime_t 96 SMLooper::DeliveryTime() const 97 { 98 return fDeliveryTime; 99 } 100 101 // ReplySuccess 102 bool 103 SMLooper::ReplySuccess() const 104 { 105 return (fReplyTime >= 0); 106 } 107 108 // SetReplyTime 109 void 110 SMLooper::SetReplyTime(bigtime_t replyTime) 111 { 112 fReplyTime = replyTime; 113 } 114 115 // ReplyTime 116 bigtime_t 117 SMLooper::ReplyTime() const 118 { 119 return fReplyTime; 120 } 121 122 123 // SMHandler 124 125 // constructor 126 SMHandler::SMHandler() 127 : BHandler() 128 { 129 } 130 131 // MessageReceived 132 void 133 SMHandler::MessageReceived(BMessage *message) 134 { 135 switch (message->what) { 136 case MSG_TEST: 137 if (SMLooper *looper = dynamic_cast<SMLooper*>(Looper())) { 138 looper->SetDeliveryTime(system_time()); 139 if (looper->ReplyDelay() > 0) 140 snooze(looper->ReplyDelay()); 141 } 142 message->SendReply(MSG_REPLY); 143 break; 144 case MSG_REPLY: 145 if (SMLooper *looper = dynamic_cast<SMLooper*>(Looper())) 146 looper->SetReplyTime(system_time()); 147 break; 148 default: 149 BHandler::MessageReceived(message); 150 break; 151 } 152 } 153 154 155