#include "uscxml/Interpreter.h" #include "uscxml/interpreter/InterpreterMonitor.h" #include "uscxml/interpreter/InterpreterImpl.h" #include "uscxml/interpreter/BasicEventQueue.h" #include "uscxml/interpreter/BasicDelayedEventQueue.h" #include "uscxml/PausableDelayedEventQueue.h" #include // for evutil_socket_t #include #include using namespace uscxml; class MyPausableDelayedEventQueue; std::shared_ptr nestedDelayQueue; class MyPausableDelayedEventQueue : public PausableDelayedEventQueue { MyPausableDelayedEventQueue(DelayedEventQueueCallbacks* callbacks) : PausableDelayedEventQueue(callbacks) { } std::shared_ptr create(DelayedEventQueueCallbacks* callbacks) { // remember as nestedDelayQueue in global scope nestedDelayQueue = std::shared_ptr(new MyPausableDelayedEventQueue(callbacks)); return nestedDelayQueue; } }; // from issue 96: // https://github.com/tklab-tud/uscxml/issues/96 static const char *customDelayedEQ = "" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ""; bool testPausableEventQueue() { Interpreter interpreter = Interpreter::fromXML(customDelayedEQ, ""); PausableDelayedEventQueue* queue = new PausableDelayedEventQueue(interpreter.getImpl().get()); ActionLanguage lang; lang.delayedQueue = DelayedEventQueue(std::shared_ptr(queue)); interpreter.setActionLanguage(lang); StateTransitionMonitor mon; mon.copyToInvokers(true); interpreter.addMonitor(&mon); size_t iterations = 10; InterpreterState state = InterpreterState::USCXML_UNDEF; while (state != USCXML_FINISHED) { state = interpreter.step(); if (nestedDelayQueue) { /* * As soon as we have the nested event queue instantiated, we pause and resume it * We pause for 500ms, and run for 500ms. This will effectively double the time required * for delayed events. * This would usually done in another thread .. */ std::cout << "<- pausing" << std::endl; nestedDelayQueue->pause(); std::this_thread::sleep_for(std::chrono::milliseconds(500)); std::cout << "-> continuing" << std::endl; nestedDelayQueue->resume(); std::this_thread::sleep_for(std::chrono::milliseconds(500)); if (iterations-- == 0) return true; } } return true; } int main(int argc, char** argv) { testPausableEventQueue(); }