From c1e65d41ee8c5fc1d5b8635342356d733436b409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Tue, 17 Oct 2023 14:18:38 +0200 Subject: [PATCH] finish test 1 (succesfully) --- src/simplified/xf/core/behavior.cpp | 35 +++++++----- src/simplified/xf/core/timeout.cpp | 1 + src/simplified/xf/port/common/dispatcher.cpp | 26 +++++---- .../xf/port/common/timeoutmanager.cpp | 54 ++++++++++++++----- 4 files changed, 78 insertions(+), 38 deletions(-) diff --git a/src/simplified/xf/core/behavior.cpp b/src/simplified/xf/core/behavior.cpp index ab2c3bf..182c3e3 100644 --- a/src/simplified/xf/core/behavior.cpp +++ b/src/simplified/xf/core/behavior.cpp @@ -6,11 +6,13 @@ // TODO done: Implement code for XFBehavior class XFBehavior::XFBehavior() { - + this->deleteOnTerminate_ = false; } XFBehavior::~XFBehavior() { - + if (this->deleteOnTerminate()) { + delete this; + } } void XFBehavior::startBehavior() { @@ -18,20 +20,22 @@ void XFBehavior::startBehavior() { } void XFBehavior::pushEvent(XFEvent *pEvent) { - pEvent->setBehavior(this); + if(pEvent->getBehavior()==nullptr) { + pEvent->setBehavior(this); + } this->getDispatcher()->pushEvent(pEvent); } bool XFBehavior::deleteOnTerminate() const { - return deleteOnTerminate_; + return this->deleteOnTerminate_; } void XFBehavior::setDeleteOnTerminate(bool deleteBehaviour) { - deleteOnTerminate_ = deleteBehaviour; + this->deleteOnTerminate_ = deleteBehaviour; } const XFEvent *XFBehavior::getCurrentEvent() const { - return pCurrentEvent_; + return this->pCurrentEvent_; } interface::XFDispatcher *XFBehavior::getDispatcher() { @@ -40,23 +44,26 @@ interface::XFDispatcher *XFBehavior::getDispatcher() { const XFTimeout *XFBehavior::getCurrentTimeout() { if(pCurrentEvent_->getEventType() == XFEvent::Timeout) { - return (XFTimeout*) pCurrentEvent_; + return (XFTimeout*) this->pCurrentEvent_; } else { return nullptr; } } void XFBehavior::setCurrentEvent(const XFEvent *pEvent) { - pCurrentEvent_ = pEvent; + this->pCurrentEvent_ = pEvent; } XFBehavior::TerminateBehavior XFBehavior::process(const XFEvent *pEvent) { - setCurrentEvent(pEvent); + this->setCurrentEvent(pEvent); XFEventStatus status = XFEventStatus::Unknown; - status = processEvent(); - if(status == XFEventStatus::Consumed) { - return deleteOnTerminate_; - } else { - return false; + status = this->processEvent(); + if(status == XFEventStatus::Consumed && pEvent->deleteAfterConsume()) { + delete pEvent; } + XFBehavior::TerminateBehavior terminateBehavior = false; + if(status == XFEventStatus::Terminate) { + terminateBehavior = true; + } + return terminateBehavior; } diff --git a/src/simplified/xf/core/timeout.cpp b/src/simplified/xf/core/timeout.cpp index 717bad5..264688a 100644 --- a/src/simplified/xf/core/timeout.cpp +++ b/src/simplified/xf/core/timeout.cpp @@ -4,6 +4,7 @@ XFTimeout::XFTimeout(int id, int interval, interface::XFBehavior *pBehavior): XFEvent(XFEventType::Timeout, id), interval_(interval) { + this->setRelTicks(this->getInterval()); setBehavior(pBehavior); } diff --git a/src/simplified/xf/port/common/dispatcher.cpp b/src/simplified/xf/port/common/dispatcher.cpp index 48307ec..b7aa12a 100644 --- a/src/simplified/xf/port/common/dispatcher.cpp +++ b/src/simplified/xf/port/common/dispatcher.cpp @@ -24,10 +24,9 @@ interface::XFDispatcher * interface::XFDispatcher::getInstance() { return &dispatcher; } -// TODO: Implement code for XFDispatcher class +// TODO done: Implement code for XFDispatcher class XFDispatcher::XFDispatcher() { - } XFDispatcher::~XFDispatcher() { @@ -35,14 +34,20 @@ XFDispatcher::~XFDispatcher() { } void XFDispatcher::dispatchEvent(const XFEvent *pEvent) const { - if(pEvent->getBehavior()->process(pEvent)) { // TODO look weird - //events_.pop(); - delete pEvent; + //Trace::out("[DEBUG] - Dispatch Event"); + XFBehavior::TerminateBehavior terminateBehavior; + terminateBehavior = pEvent->getBehavior()->process(pEvent); + if(terminateBehavior) { + delete pEvent->getBehavior(); + if(pEvent->deleteAfterConsume()) { + delete pEvent; + } } } void XFDispatcher::pushEvent(XFEvent *pEvent) { - events_.push(pEvent); + //Trace::out("[DEBUG] - Push Event"); + events_.push(pEvent, false); } void XFDispatcher::scheduleTimeout(int timeoutId, int interval, interface::XFBehavior *pBehavior) { @@ -54,15 +59,16 @@ void XFDispatcher::unscheduleTimeout(int timeoutId, interface::XFBehavior *pBeha } void XFDispatcher::executeOnce() { - //XFEvent* event; - dispatchEvent(events_.front()); - events_.pop(); + if(!this->events_.empty()) { + dispatchEvent(this->events_.front()); + this->events_.pop(); + } } int XFDispatcher::execute(const void *param) { while(true){ - executeOnce(); + this->executeOnce(); } } diff --git a/src/simplified/xf/port/common/timeoutmanager.cpp b/src/simplified/xf/port/common/timeoutmanager.cpp index ace0dea..9b31fa8 100644 --- a/src/simplified/xf/port/common/timeoutmanager.cpp +++ b/src/simplified/xf/port/common/timeoutmanager.cpp @@ -8,6 +8,9 @@ #include "xf/interface/behavior.h" #include "xf/interface/mutex.h" #include "timeoutmanager.h" +#if defined(XF_TRACE_EVENT_PUSH_POP) && (XF_TRACE_EVENT_PUSH_POP != 0) + #include "trace/trace.h" +#endif // XF_TRACE_EVENT_PUSH_POP using Mutex = interface::XFMutex; // Rename XFMutex interface class to Mutex for easier use. @@ -24,6 +27,7 @@ interface::XFTimeoutManager * interface::XFTimeoutManager::getInstance() { // TODO done: Implement code for XFTimeoutManager class XFTimeoutManager::XFTimeoutManager() { + this->pMutex_ = interface::XFMutex::create(); } XFTimeoutManager::~XFTimeoutManager() { @@ -31,41 +35,63 @@ XFTimeoutManager::~XFTimeoutManager() { } void XFTimeoutManager::addTimeout(XFTimeout *pNewTimeout) { - pMutex_->lock(); - timeouts_.push_front(pNewTimeout); - pMutex_->unlock(); + this->pMutex_->lock(); + this->timeouts_.push_front(pNewTimeout); + this->pMutex_->unlock(); } void XFTimeoutManager::returnTimeout(XFTimeout *pTimeout) { - pMutex_->lock(); + this->pMutex_->lock(); XFDispatcher::getInstance()->pushEvent(pTimeout); - timeouts_.remove(pTimeout); - pMutex_->unlock(); + this->timeouts_.remove(pTimeout); + this->pMutex_->unlock(); } void XFTimeoutManager::start(std::function startTimeoutManagerTimer) { - startTimeoutManagerTimer(tickInterval_); + startTimeoutManagerTimer(this->tickInterval_); } void XFTimeoutManager::scheduleTimeout(int32_t timeoutId, int32_t interval, interface::XFBehavior *pBehavior) { + string str = "[DEBUG] - Schedule Timeout: "; + str += to_string(timeoutId); + //Trace::out(str); ::XFTimeout* timeout = new XFTimeout(timeoutId, interval, pBehavior); addTimeout(timeout); } void XFTimeoutManager::unscheduleTimeout(int32_t timeoutId, interface::XFBehavior *pBehavior) { - for(XFTimeout* timeout : timeouts_) { - bool id = ( timeout->getId() == timeoutId ); - bool behavior = ( timeout->getBehavior() == pBehavior); - if( id && behavior ) { - timeouts_.remove(timeout); + string str = "[DEBUG] - Unschedule Timeout: "; + str += to_string(timeoutId); + //Trace::out(str); + this->pMutex_->lock(); + TimeoutList::iterator it; + for(it = this->timeouts_.begin(); it != this->timeouts_.end(); it++){ + + if((*it)->getId()==timeoutId && (*it)->getBehavior() == pBehavior) { + it = this->timeouts_.erase(it); } + } + this->pMutex_->unlock(); } void XFTimeoutManager::tick() { - for(XFTimeout* timeout : timeouts_) { - timeout->substractFromRelTicks(tickInterval_); + //Trace::out("[DEBUG] - Tick"); + if(!this->timeouts_.empty()) { + TimeoutList::iterator it; + + this->pMutex_->lock(); + for(it=this->timeouts_.begin(); it != this->timeouts_.end(); it++) { + if((*it)->getRelTicks() > 0) { + (*it)->substractFromRelTicks(this->tickInterval_); + } else { + XFEvent* ev = *(it); + XFDispatcher::getInstance()->pushEvent(ev); + it = this->timeouts_.erase(it); + } + } + this->pMutex_->unlock(); } }