diff options
author | Stefan Radomski <github@mintwerk.de> | 2017-01-13 16:47:44 (GMT) |
---|---|---|
committer | Stefan Radomski <github@mintwerk.de> | 2017-01-13 16:47:44 (GMT) |
commit | 4f6cbe9e7aec2b4a6c8f286f9097abfb011a6235 (patch) | |
tree | 8c023473bb342780ddf51a893d18369f1319bb5c /contrib/src/uscxml | |
parent | 0aa0fe08dc308c94379c47d0bf9745e341cb4c81 (diff) | |
download | uscxml-4f6cbe9e7aec2b4a6c8f286f9097abfb011a6235.zip uscxml-4f6cbe9e7aec2b4a6c8f286f9097abfb011a6235.tar.gz uscxml-4f6cbe9e7aec2b4a6c8f286f9097abfb011a6235.tar.bz2 |
First support for serialization and some bug fixes for DOM per data.src
Diffstat (limited to 'contrib/src/uscxml')
-rw-r--r-- | contrib/src/uscxml/PausableDelayedEventQueue.cpp | 94 | ||||
-rw-r--r-- | contrib/src/uscxml/PausableDelayedEventQueue.h | 53 |
2 files changed, 147 insertions, 0 deletions
diff --git a/contrib/src/uscxml/PausableDelayedEventQueue.cpp b/contrib/src/uscxml/PausableDelayedEventQueue.cpp new file mode 100644 index 0000000..cb7a373 --- /dev/null +++ b/contrib/src/uscxml/PausableDelayedEventQueue.cpp @@ -0,0 +1,94 @@ +/** + * @file + * @author 2017 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#include "PausableDelayedEventQueue.h" +#include <assert.h> + +namespace uscxml { + +PausableDelayedEventQueue::PausableDelayedEventQueue(DelayedEventQueueCallbacks* callbacks) : BasicDelayedEventQueue(callbacks) { + _pausedAt.tv_sec = 0; + _pausedAt.tv_usec = 0; +} + +std::shared_ptr<DelayedEventQueueImpl> PausableDelayedEventQueue::create(DelayedEventQueueCallbacks* callbacks) { + return std::shared_ptr<PausableDelayedEventQueue>(new PausableDelayedEventQueue(callbacks)); +} + +void PausableDelayedEventQueue::pause() { + if(_pausedAt.tv_sec != 0 || _pausedAt.tv_usec != 0) { + return; // we are already paused! + } + + evutil_gettimeofday(&_pausedAt, NULL); // remember when we paused + + { + // Verbatim copy of stop() without cancelAllDelayed() + if (_isStarted) { + _isStarted = false; + event_base_loopbreak(_eventLoop); + } + if (_thread) { + _thread->join(); + delete _thread; + _thread = NULL; + } + } + + std::lock_guard<std::recursive_mutex> lock(_mutex); + + // remove all events from libevent without deleting them + for(auto callbackData : _callbackData) { + Event data = callbackData.second.userData; + event_del(callbackData.second.event); + } +} + +void PausableDelayedEventQueue::resume() { + if (_pausedAt.tv_sec != 0 || _pausedAt.tv_usec != 0) { + struct timeval now; + struct timeval pausedFor; + + evutil_gettimeofday(&now, NULL); + evutil_timersub(&now, &_pausedAt, &pausedFor); + _pausedAt.tv_sec = 0; + _pausedAt.tv_usec = 0; + + for(auto& callbackData : _callbackData) { + // add the time we were paused to all due times + evutil_timeradd(&callbackData.second.due, &pausedFor, &callbackData.second.due); + + struct timeval remain; + evutil_timersub(&callbackData.second.due, &now, &remain); + +#if 0 + std::cout << "Now : " << now.tv_sec << "." << now.tv_usec << std::endl; + std::cout << "Paused : " << pausedFor.tv_sec << "." << pausedFor.tv_usec << std::endl; + std::cout << "Remaining: " << remain.tv_sec << "." << remain.tv_usec << std::endl; +#endif + assert(remain.tv_usec >= 0 && remain.tv_sec >= 0); + + // reenqueue with libevent + event_add(callbackData.second.event, &remain); + } + } + start(); +} + +} diff --git a/contrib/src/uscxml/PausableDelayedEventQueue.h b/contrib/src/uscxml/PausableDelayedEventQueue.h new file mode 100644 index 0000000..ae64e72 --- /dev/null +++ b/contrib/src/uscxml/PausableDelayedEventQueue.h @@ -0,0 +1,53 @@ +/** + * @file + * @author 2017 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#ifndef PAUSABLEDELAYEDEVENTQUEUE_H_F26D6161 +#define PAUSABLEDELAYEDEVENTQUEUE_H_F26D6161 + +#include "uscxml/config.h" +#include "uscxml/Common.h" +#include "uscxml/interpreter/BasicDelayedEventQueue.h" + +#include <memory> +#include <sys/time.h> + +namespace uscxml { + +/** + * A DelayedEventQueue that implements pause/resume + * @ingroup eventqueue + * @ingroup impl + */ +class USCXML_API PausableDelayedEventQueue : public BasicDelayedEventQueue { +public: + PausableDelayedEventQueue(DelayedEventQueueCallbacks* callbacks); + std::shared_ptr<DelayedEventQueueImpl> create(DelayedEventQueueCallbacks* callbacks); + + void pause(); + void resume(); + +protected: + timeval _pausedAt; +}; + +} + + + +#endif /* end of include guard: PAUSABLEDELAYEDEVENTQUEUE_H_F26D6161 */ |