added EventScheduler public class

This commit is contained in:
maroy 2004-11-07 08:44:12 +00:00
parent 057da6a786
commit 790dac4fa2
7 changed files with 523 additions and 7 deletions

View File

@ -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

View File

@ -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 <boost/date_time/posix_time/posix_time.hpp>
#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<Thread>::Ref thread;
/**
* The scheduler thread runnable object.
*/
Ptr<RunnableInterface>::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<EventContainerInterface>::Ref eventContainer,
Ptr<time_duration>::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

View File

@ -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<EventContainerInterface>::Ref eventContainer,
Ptr<time_duration>::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();
}

View File

@ -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 <unistd.h>
#else
#error "Need unistd.h"
#endif
#include <string>
#include <iostream>
#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<TestScheduledEvent>::Ref event;
Ptr<TestEventContainer>::Ref container;
Ptr<EventScheduler>::Ref eventScheduler;
Ptr<ptime>::Ref now;
Ptr<ptime>::Ref when;
Ptr<time_duration>::Ref initTime;
Ptr<time_duration>::Ref eventLength;
Ptr<time_duration>::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();
}

View File

@ -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 <cppunit/extensions/HelperMacros.h>
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

View File

@ -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)

View File

@ -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