From 790dac4fa2a5446e2ec3eedf6200342cda0489e2 Mon Sep 17 00:00:00 2001 From: maroy Date: Sun, 7 Nov 2004 08:44:12 +0000 Subject: [PATCH] added EventScheduler public class --- .../modules/eventScheduler/etc/Makefile.in | 6 +- .../EventScheduler/EventScheduler.h | 134 +++++++++++++ .../eventScheduler/src/EventScheduler.cxx | 88 +++++++++ .../eventScheduler/src/EventSchedulerTest.cxx | 183 ++++++++++++++++++ .../eventScheduler/src/EventSchedulerTest.h | 109 +++++++++++ .../src/SchedulerThreadTest.cxx | 4 +- .../eventScheduler/src/SchedulerThreadTest.h | 6 +- 7 files changed, 523 insertions(+), 7 deletions(-) create mode 100644 livesupport/modules/eventScheduler/include/LiveSupport/EventScheduler/EventScheduler.h create mode 100644 livesupport/modules/eventScheduler/src/EventScheduler.cxx create mode 100644 livesupport/modules/eventScheduler/src/EventSchedulerTest.cxx create mode 100644 livesupport/modules/eventScheduler/src/EventSchedulerTest.h diff --git a/livesupport/modules/eventScheduler/etc/Makefile.in b/livesupport/modules/eventScheduler/etc/Makefile.in index 3dfbdae84..3851f8fa7 100644 --- a/livesupport/modules/eventScheduler/etc/Makefile.in +++ b/livesupport/modules/eventScheduler/etc/Makefile.in @@ -21,7 +21,7 @@ # # # Author : $Author: maroy $ -# Version : $Revision: 1.1 $ +# Version : $Revision: 1.2 $ # Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/etc/Makefile.in,v $ # # @configure_input@ @@ -93,10 +93,12 @@ LDFLAGS = @LDFLAGS@ -L${USR_LIB_DIR} -L${CORE_LIB_DIR} -L${LIB_DIR} #------------------------------------------------------------------------------- # Dependencies #------------------------------------------------------------------------------- -EVENT_SCHEDULER_LIB_OBJS = ${TMP_DIR}/SchedulerThread.o +EVENT_SCHEDULER_LIB_OBJS = ${TMP_DIR}/SchedulerThread.o \ + ${TMP_DIR}/EventScheduler.o TEST_RUNNER_OBJS = ${TMP_DIR}/TestScheduledEvent.o \ ${TMP_DIR}/TestEventContainer.o \ ${TMP_DIR}/SchedulerThreadTest.o \ + ${TMP_DIR}/EventSchedulerTest.o \ ${TMP_DIR}/TestRunner.o TEST_RUNNER_LIBS = -l${EVENT_SCHEDULER_LIB} -l${CORE_LIB} \ -lxml++-1.0 -lcppunit -ldl diff --git a/livesupport/modules/eventScheduler/include/LiveSupport/EventScheduler/EventScheduler.h b/livesupport/modules/eventScheduler/include/LiveSupport/EventScheduler/EventScheduler.h new file mode 100644 index 000000000..4f86c3592 --- /dev/null +++ b/livesupport/modules/eventScheduler/include/LiveSupport/EventScheduler/EventScheduler.h @@ -0,0 +1,134 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport 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. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: maroy $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/include/LiveSupport/EventScheduler/EventScheduler.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef LiveSupport_EventScheduler_EventScheduler_h +#define LiveSupport_EventScheduler_EventScheduler_h + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include + +#include "LiveSupport/Core/Thread.h" +#include "LiveSupport/Core/RunnableInterface.h" +#include "LiveSupport/EventScheduler/EventContainerInterface.h" + + +namespace LiveSupport { +namespace EventScheduler { + +using namespace boost::posix_time; + +using namespace LiveSupport; +using namespace LiveSupport::Core; + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/** + * A generic event scheduler, for non-everlapping subsequent events. + * + * @author $Author: maroy $ + * @version $Revision: 1.1 $ + */ +class EventScheduler +{ + private: + /** + * The thread of the scheduler. + */ + Ptr::Ref thread; + + /** + * The scheduler thread runnable object. + */ + Ptr::Ref runnable; + + + public: + /** + * Constructor. + * + * @param eventContainer the container this thread will get its + * events to schedule from. + * @param granularity the granularity of the thread: the time the + * thread will sleep between checking up on things. + */ + EventScheduler(Ptr::Ref eventContainer, + Ptr::Ref granularity) + throw (); + + /** + * A virtual destructor, as this class has virtual functions. + */ + virtual + ~EventScheduler(void) throw () + { + } + + /** + * Start the event scheduler. + * This function starts the event scheduler in the background, + * and returns immediately. + */ + virtual void + start(void) throw (); + + /** + * Stop the event scheduler. + */ + virtual void + stop(void) throw (); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace EventScheduler +} // namespace LiveSupport + + +#endif // LiveSupport_EventScheduler_EventScheduler_h + diff --git a/livesupport/modules/eventScheduler/src/EventScheduler.cxx b/livesupport/modules/eventScheduler/src/EventScheduler.cxx new file mode 100644 index 000000000..a7e81902d --- /dev/null +++ b/livesupport/modules/eventScheduler/src/EventScheduler.cxx @@ -0,0 +1,88 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport 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. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: maroy $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/EventScheduler.cxx,v $ + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include "LiveSupport/Core/TimeConversion.h" +#include "LiveSupport/EventScheduler/EventScheduler.h" + +#include "SchedulerThread.h" + + +using namespace LiveSupport::Core; +using namespace LiveSupport::EventScheduler; + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Constructor. + *----------------------------------------------------------------------------*/ +LiveSupport::EventScheduler::EventScheduler :: EventScheduler( + Ptr::Ref eventContainer, + Ptr::Ref granularity) + throw () +{ + runnable.reset(new SchedulerThread(eventContainer, granularity)); + thread.reset(new Thread(runnable)); +} + + +/*------------------------------------------------------------------------------ + * Start the event scheduler. + *----------------------------------------------------------------------------*/ +void +LiveSupport::EventScheduler::EventScheduler :: start(void) throw () +{ + thread->start(); +} + + +/*------------------------------------------------------------------------------ + * Stop the event scheduler. + *----------------------------------------------------------------------------*/ +void +LiveSupport::EventScheduler::EventScheduler :: stop(void) throw () +{ + thread->stop(); + thread->join(); +} + diff --git a/livesupport/modules/eventScheduler/src/EventSchedulerTest.cxx b/livesupport/modules/eventScheduler/src/EventSchedulerTest.cxx new file mode 100644 index 000000000..16d2e4ea5 --- /dev/null +++ b/livesupport/modules/eventScheduler/src/EventSchedulerTest.cxx @@ -0,0 +1,183 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport 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. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: maroy $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/EventSchedulerTest.cxx,v $ + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#if HAVE_UNISTD_H +#include +#else +#error "Need unistd.h" +#endif + + +#include +#include + +#include "LiveSupport/Core/TimeConversion.h" + +#include "LiveSupport/EventScheduler/EventScheduler.h" +#include "TestScheduledEvent.h" +#include "TestEventContainer.h" +#include "EventSchedulerTest.h" + + +using namespace boost::posix_time; + +using namespace LiveSupport::Core; +using namespace LiveSupport::EventScheduler; + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + +CPPUNIT_TEST_SUITE_REGISTRATION(EventSchedulerTest); + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Set up the test environment + *----------------------------------------------------------------------------*/ +void +EventSchedulerTest :: setUp(void) throw () +{ +} + + +/*------------------------------------------------------------------------------ + * Clean up the test environment + *----------------------------------------------------------------------------*/ +void +EventSchedulerTest :: tearDown(void) throw () +{ +} + + +/*------------------------------------------------------------------------------ + * A simple test for the event scheduler thread + *----------------------------------------------------------------------------*/ +void +EventSchedulerTest :: firstTest(void) + throw (CPPUNIT_NS::Exception) +{ + Ptr::Ref event; + Ptr::Ref container; + Ptr::Ref eventScheduler; + Ptr::Ref now; + Ptr::Ref when; + Ptr::Ref initTime; + Ptr::Ref eventLength; + Ptr::Ref granularity; + TestScheduledEvent::State state; + + /* time timeline for this test is: + initialize - 1sec, sometime before start + start - now + 5sec + stop - start + 3 sec + */ + + now = TimeConversion::now(); + when.reset(new ptime(*now + seconds(5))); + initTime.reset(new time_duration(seconds(1))); + eventLength.reset(new time_duration(seconds(3))); + granularity.reset(new time_duration(seconds(1))); + + event.reset(new TestScheduledEvent(when, initTime, eventLength)); + container.reset(new TestEventContainer(event)); + + eventScheduler.reset(new EventScheduler(container, granularity)); + + eventScheduler->start(); + + CPPUNIT_ASSERT(event->getState() == TestScheduledEvent::created); + state = event->getState(); + + for (bool running = true; running; ) { + // check for each state, and see if they are entered into in a correct + // order + now = TimeConversion::now(); + switch (event->getState()) { + case TestScheduledEvent::created: + CPPUNIT_ASSERT(state == TestScheduledEvent::created); + break; + + case TestScheduledEvent::initializing: + // as the init time is same as granularity, we will only + // see initializing once, and can assume that the previous + // state was 'created' + CPPUNIT_ASSERT(state == TestScheduledEvent::created); + break; + + case TestScheduledEvent::initialized: + CPPUNIT_ASSERT(state == TestScheduledEvent::initializing + || state == TestScheduledEvent::initialized); + break; + + case TestScheduledEvent::running: + CPPUNIT_ASSERT(state == TestScheduledEvent::initialized + || state == TestScheduledEvent::running); + // see if the state changed from initialized to running + // at the appropriate time + if (state == TestScheduledEvent::initialized) { + CPPUNIT_ASSERT(*when <= *now + && *now <= *when + *granularity); + } + break; + + case TestScheduledEvent::stopped: + CPPUNIT_ASSERT(state == TestScheduledEvent::running + || state == TestScheduledEvent::stopped); + break; + + case TestScheduledEvent::deInitialized: + // accept running as a possible previous state, as we might + // not catch the stopped state at all + CPPUNIT_ASSERT(state == TestScheduledEvent::running + || state == TestScheduledEvent::stopped); + running = false; + break; + + default: + CPPUNIT_FAIL("unrecognized event state"); + } + state = event->getState(); + TimeConversion::sleep(granularity); + } + + eventScheduler->stop(); +} + diff --git a/livesupport/modules/eventScheduler/src/EventSchedulerTest.h b/livesupport/modules/eventScheduler/src/EventSchedulerTest.h new file mode 100644 index 000000000..439913ea4 --- /dev/null +++ b/livesupport/modules/eventScheduler/src/EventSchedulerTest.h @@ -0,0 +1,109 @@ +/*------------------------------------------------------------------------------ + + Copyright (c) 2004 Media Development Loan Fund + + This file is part of the LiveSupport project. + http://livesupport.campware.org/ + To report bugs, send an e-mail to bugs@campware.org + + LiveSupport is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + LiveSupport 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. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with LiveSupport; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + + Author : $Author: maroy $ + Version : $Revision: 1.1 $ + Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/EventSchedulerTest.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef EventSchedulerTest_h +#define EventSchedulerTest_h + +#ifndef __cplusplus +#error This is a C++ include file +#endif + + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include + + +namespace LiveSupport { +namespace EventScheduler { + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/** + * Unit test for the EventScheduler class. + * + * @author $Author: maroy $ + * @version $Revision: 1.1 $ + * @see EventScheduler + */ +class EventSchedulerTest : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE(EventSchedulerTest); + CPPUNIT_TEST(firstTest); + CPPUNIT_TEST_SUITE_END(); + + private: + + + protected: + + /** + * A simple test. + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + firstTest(void) throw (CPPUNIT_NS::Exception); + + public: + + /** + * Set up the environment for the test case. + */ + void + setUp(void) throw (); + + /** + * Clean up the environment after the test case. + */ + void + tearDown(void) throw (); +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace EventScheduler +} // namespace LiveSupport + +#endif // EventSchedulerTest_h + diff --git a/livesupport/modules/eventScheduler/src/SchedulerThreadTest.cxx b/livesupport/modules/eventScheduler/src/SchedulerThreadTest.cxx index 8b8debe54..4d3288df3 100644 --- a/livesupport/modules/eventScheduler/src/SchedulerThreadTest.cxx +++ b/livesupport/modules/eventScheduler/src/SchedulerThreadTest.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.2 $ + Version : $Revision: 1.3 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/SchedulerThreadTest.cxx,v $ ------------------------------------------------------------------------------*/ @@ -89,7 +89,7 @@ SchedulerThreadTest :: tearDown(void) throw () /*------------------------------------------------------------------------------ - * Test to see if the HelixPlayer engine can be started and stopped + * A simple test for the event scheduler thread *----------------------------------------------------------------------------*/ void SchedulerThreadTest :: firstTest(void) diff --git a/livesupport/modules/eventScheduler/src/SchedulerThreadTest.h b/livesupport/modules/eventScheduler/src/SchedulerThreadTest.h index ee3d257f1..8c4107c08 100644 --- a/livesupport/modules/eventScheduler/src/SchedulerThreadTest.h +++ b/livesupport/modules/eventScheduler/src/SchedulerThreadTest.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.1 $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/SchedulerThreadTest.h,v $ ------------------------------------------------------------------------------*/ @@ -55,10 +55,10 @@ namespace EventScheduler { /* =============================================================== data types */ /** - * Unit test for the EventScheduler class. + * Unit test for the SchedulerThread class. * * @author $Author: maroy $ - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * @see EventScheduler */ class SchedulerThreadTest : public CPPUNIT_NS::TestFixture