00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <bogotel/Portability.h>
00011 #include <bogotel/EvtQueue.h>
00012 #include <bogotel/BgtErrors.h>
00013 #include <bogotel/util.h>
00014 #include <bogotel/SignalDev.h>
00015 #include <bogotel/VoiceDev.h>
00016
00017 #include <srllib.h>
00018 #include <gclib.h>
00019
00020 #include <boost/thread/xtime.hpp>
00021
00022 #include <bogotel/STLhelp.h>
00023
00024 namespace bogotel {
00025
00026 CEvtQueue::CEvtQueue()
00027 {
00028
00029 }
00030
00031 CEvtQueue::~CEvtQueue()
00032 {
00033 }
00034
00035 int CEvtQueue::wait(long lTimeout, CEvt** ppEvt)
00036 {
00037 int rc;
00038
00039 if ((rc = getFromQueue(ppEvt)) != resultSUCCESS) {
00040 g_util->log(3, -1, "CEvtQueue::wait(): getFromQueue() failed. rc is %d", rc);
00041 return rc;
00042 }
00043
00044 if (*ppEvt) {
00045 g_util->log(9, -1, "CEvtQueue::wait() completed. An event was returned.");
00046 return resultSUCCESS;
00047 }
00048
00049
00050 if (lTimeout == 0) {
00051 (*ppEvt) = NULL;
00052 g_util->log(9, -1, "CEvtQueue::wait() completed. No event was returned.");
00053 return resultSUCCESS;
00054 }
00055
00056 {
00057 boost::mutex::scoped_lock lock(m_mtxWait);
00058 boost::xtime xt;
00059 ContainerNotEmpty<DEQUE_EVT> notEmpty(m_dequeEvt);
00060
00061
00062 boost::xtime_get(&xt, boost::TIME_UTC);
00063 xt.sec += lTimeout / 1000;
00064 if (! m_condEvent.timed_wait(lock, xt, notEmpty)) {
00065
00066
00067 return resultSUCCESS;
00068 }
00069 }
00070
00071
00072 if ((rc = getFromQueue(ppEvt)) != resultSUCCESS) {
00073 g_util->log(3, -1, "CEvtQueue::wait(): getFromQueue() failed.");
00074 return rc;
00075 }
00076 if ((*ppEvt) == NULL) {
00077 g_util->log(3, -1, "CEvtQueue::wait(): event expected.");
00078 return resultUNEXPECTED;
00079 }
00080 g_util->log(9, -1, "CEvtQueue::wait() completed. Event posted.");
00081 return resultSUCCESS;
00082 }
00083
00084 int CEvtQueue::getFromQueue(CEvt** ppEvt)
00085 {
00086 boost::mutex::scoped_lock lock(m_mtxQueue);
00087
00088 if (!m_dequeEvt.empty()) {
00089 (*ppEvt) = m_dequeEvt.front();
00090 m_dequeEvt.pop_front();
00091 } else {
00092 (*ppEvt) = NULL;
00093 }
00094
00095 return resultSUCCESS;
00096 }
00097
00098 int CEvtQueue::add(CEvt* pEvt)
00099 {
00100 if (pEvt == NULL) {
00101 return resultUNEXPECTED;
00102 }
00103
00104 {
00105 boost::mutex::scoped_lock lock(m_mtxQueue);
00106 m_dequeEvt.push_back(pEvt);
00107 }
00108
00109 std::string strEvent = pEvt->toString();
00110
00111 m_condEvent.notify_one();
00112
00113 g_util->log(9, -1, "CEvtQueue::add completed. Event: %s", strEvent);
00114
00115 return resultSUCCESS;
00116 }
00117
00118 const std::string& CEvt::toString()
00119 {
00120 if (m_lType & DT_GC) {
00121 return CSignalDev::getEventName(m_lType);
00122 } else {
00123 return CVoiceDev::getEventName(m_lType);
00124 }
00125 }
00126
00127 }