From a279b355c1caa1da870d7bb5a0e0db5f76598939 Mon Sep 17 00:00:00 2001 From: maroy Date: Thu, 25 Nov 2004 12:33:22 +0000 Subject: [PATCH] replaced handler thread implementation from direct POSIX thread to LiveSupport::Core::Thread --- .../modules/playlistExecutor/etc/Makefile.in | 16 +- .../src/AudioPlayerFactory.cxx | 3 +- .../src/AudioPlayerFactoryTest.cxx | 18 ++- .../src/HelixEventHandlerThread.cxx | 84 ++++++++++ .../src/HelixEventHandlerThread.h | 146 ++++++++++++++++++ .../playlistExecutor/src/HelixPlayer.cxx | 41 ++--- .../playlistExecutor/src/HelixPlayer.h | 39 ++--- 7 files changed, 276 insertions(+), 71 deletions(-) create mode 100644 livesupport/modules/playlistExecutor/src/HelixEventHandlerThread.cxx create mode 100644 livesupport/modules/playlistExecutor/src/HelixEventHandlerThread.h diff --git a/livesupport/modules/playlistExecutor/etc/Makefile.in b/livesupport/modules/playlistExecutor/etc/Makefile.in index 387664e43..cf68855f2 100644 --- a/livesupport/modules/playlistExecutor/etc/Makefile.in +++ b/livesupport/modules/playlistExecutor/etc/Makefile.in @@ -21,7 +21,7 @@ # # # Author : $Author: maroy $ -# Version : $Revision: 1.3 $ +# Version : $Revision: 1.4 $ # Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/playlistExecutor/etc/Makefile.in,v $ # # @configure_input@ @@ -85,6 +85,8 @@ TEST_RUNNER = ${TMP_DIR}/testRunner DOXYGEN_CONFIG = ${ETC_DIR}/doxygen.config +export LD_LIBRARY_PATH=${USR_LIB_DIR} + #------------------------------------------------------------------------------- # Configuration parameters @@ -110,18 +112,19 @@ LDFLAGS = @LDFLAGS@ -pthread \ # Dependencies #------------------------------------------------------------------------------- PLAYLIST_EXECUTOR_LIB_OBJS = ${TMP_DIR}/HelixPlayer.o \ + ${TMP_DIR}/HelixEventHandlerThread.o \ ${TMP_DIR}/AdviseSink.o \ ${TMP_DIR}/AuthenticationManager.o \ ${TMP_DIR}/ClientContext.o \ ${TMP_DIR}/ErrorSink.o \ ${TMP_DIR}/HelixIIDs.o \ ${TMP_DIR}/AudioPlayerFactory.o -TEST_RUNNER_OBJS = ${TMP_DIR}/HelixPlayerTest.o \ - ${TMP_DIR}/AudioPlayerFactoryTest.o \ - ${TMP_DIR}/TestRunner.o +TEST_RUNNER_OBJS = ${TMP_DIR}/TestRunner.o \ + ${TMP_DIR}/HelixPlayerTest.o \ + ${TMP_DIR}/AudioPlayerFactoryTest.o TEST_RUNNER_LIBS = -l${PLAYLIST_EXECUTOR_LIB} -l${CORE_LIB} \ ${HELIX_LIBS} \ - -lxml++-1.0 -lcppunit -ldl -lm -lpthread + -lxml++-1.0 -lcppunit -ldl -lm #------------------------------------------------------------------------------- @@ -152,8 +155,7 @@ distclean: clean docclean ${RMDIR} ${TMP_DIR}/config* ${TMP_DIR}/autom4te* check: all ${TEST_RUNNER} - LD_LIBRARY_PATH=${USR_LIB_DIR} ${TEST_RUNNER} \ - -o ${TEST_RESULTS} -s ${TEST_XSLT} + ${TEST_RUNNER} -o ${TEST_RESULTS} -s ${TEST_XSLT} #------------------------------------------------------------------------------- diff --git a/livesupport/modules/playlistExecutor/src/AudioPlayerFactory.cxx b/livesupport/modules/playlistExecutor/src/AudioPlayerFactory.cxx index 286ed294b..364ea5750 100644 --- a/livesupport/modules/playlistExecutor/src/AudioPlayerFactory.cxx +++ b/livesupport/modules/playlistExecutor/src/AudioPlayerFactory.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.1 $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/playlistExecutor/src/AudioPlayerFactory.cxx,v $ ------------------------------------------------------------------------------*/ @@ -100,7 +100,6 @@ AudioPlayerFactory :: configure(const xmlpp::Element & element) dynamic_cast (*(nodes.begin())); Ptr::Ref hp(new HelixPlayer()); hp->configure(*configElement); - hp->initialize(); audioPlayer = hp; } diff --git a/livesupport/modules/playlistExecutor/src/AudioPlayerFactoryTest.cxx b/livesupport/modules/playlistExecutor/src/AudioPlayerFactoryTest.cxx index 7e5a5317d..908d2505c 100644 --- a/livesupport/modules/playlistExecutor/src/AudioPlayerFactoryTest.cxx +++ b/livesupport/modules/playlistExecutor/src/AudioPlayerFactoryTest.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/playlistExecutor/src/Attic/AudioPlayerFactoryTest.cxx,v $ ------------------------------------------------------------------------------*/ @@ -85,6 +85,11 @@ AudioPlayerFactoryTest :: setUp(void) throw () audioPlayerFactory = AudioPlayerFactory::getInstance(); audioPlayerFactory->configure(*root); + // initialize the audio player configured by the factory + Ptr::Ref audioPlayer; + audioPlayer = audioPlayerFactory->getAudioPlayer(); + audioPlayer->initialize(); + } catch (std::invalid_argument &e) { std::cerr << "semantic error in configuration file" << std::endl; } catch (xmlpp::exception &e) { @@ -99,6 +104,17 @@ AudioPlayerFactoryTest :: setUp(void) throw () void AudioPlayerFactoryTest :: tearDown(void) throw () { + try { + Ptr::Ref audioPlayerFactory; + audioPlayerFactory = AudioPlayerFactory::getInstance(); + + // de-initialize the audio player configured by the factory + Ptr::Ref audioPlayer; + audioPlayer = audioPlayerFactory->getAudioPlayer(); + audioPlayer->deInitialize(); + } catch (xmlpp::exception &e) { + std::cerr << e.what() << std::endl; + } } diff --git a/livesupport/modules/playlistExecutor/src/HelixEventHandlerThread.cxx b/livesupport/modules/playlistExecutor/src/HelixEventHandlerThread.cxx new file mode 100644 index 000000000..b0124b007 --- /dev/null +++ b/livesupport/modules/playlistExecutor/src/HelixEventHandlerThread.cxx @@ -0,0 +1,84 @@ +/*------------------------------------------------------------------------------ + + 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/playlistExecutor/src/Attic/HelixEventHandlerThread.cxx,v $ + +------------------------------------------------------------------------------*/ + +/* ============================================================ include files */ + +#ifdef HAVE_CONFIG_H +#include "configure.h" +#endif + +#include "LiveSupport/Core/TimeConversion.h" + +#include "HelixEventHandlerThread.h" + + +using namespace LiveSupport::Core; +using namespace LiveSupport::PlaylistExecutor; + + +/* =================================================== local data structures */ + + +/* ================================================ local constants & macros */ + + +/* =============================================== local function prototypes */ + + +/* ============================================================= module code */ + +/*------------------------------------------------------------------------------ + * Constructor. + *----------------------------------------------------------------------------*/ +HelixEventHandlerThread :: HelixEventHandlerThread( + IHXClientEngine * clientEngine, + Ptr::Ref granularity) + throw () +{ + this->clientEngine = clientEngine; + this->granularity = granularity; + this->shouldRun = false; +} + + +/*------------------------------------------------------------------------------ + * The main execution body of the thread. + *----------------------------------------------------------------------------*/ +void +HelixEventHandlerThread :: run(void) throw () +{ + shouldRun = true; + + while (shouldRun) { + struct _HXxEvent * event = 0; + clientEngine->EventOccurred(event); + TimeConversion::sleep(granularity); + } +} + diff --git a/livesupport/modules/playlistExecutor/src/HelixEventHandlerThread.h b/livesupport/modules/playlistExecutor/src/HelixEventHandlerThread.h new file mode 100644 index 000000000..ffe12f03b --- /dev/null +++ b/livesupport/modules/playlistExecutor/src/HelixEventHandlerThread.h @@ -0,0 +1,146 @@ +/*------------------------------------------------------------------------------ + + 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/playlistExecutor/src/Attic/HelixEventHandlerThread.h,v $ + +------------------------------------------------------------------------------*/ +#ifndef HelixEventHandlerThread_h +#define HelixEventHandlerThread_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/RunnableInterface.h" +#include "HelixPlayer.h" + + +namespace LiveSupport { +namespace PlaylistExecutor { + +using namespace boost::posix_time; + +using namespace LiveSupport::Core; + +/* ================================================================ constants */ + + +/* =================================================================== macros */ + + +/* =============================================================== data types */ + +/** + * A thread that checks on Helix events every once in a while. + * + * @author $Author: maroy $ + * @version $Revision: 1.1 $ + */ +class HelixEventHandlerThread : public virtual RunnableInterface +{ + private: + /** + * The Helix client engine to check. + */ + IHXClientEngine * clientEngine; + + /** + * The granularity of the thread: the time it will sleep + * between checking up on the state of things. + */ + Ptr::Ref granularity; + + /** + * Flag indicating whether the thread should still run, or + * actually terminate. + */ + bool shouldRun; + + /** + * Default constructor. + */ + HelixEventHandlerThread(void) throw () + { + } + + + public: + /** + * Constructor. + * + * @param clientEngine the Helix client engine to check. + * @param granularity the granularity of the thread: the time the + * thread will sleep between checking up on things. + */ + HelixEventHandlerThread(IHXClientEngine * clientEngine, + Ptr::Ref granularity) + throw (); + + /** + * A virtual destructor, as this class has virtual functions. + */ + virtual + ~HelixEventHandlerThread(void) throw () + { + } + + /** + * The main execution loop for the thread. + */ + virtual void + run(void) throw (); + + /** + * Signal the thread to stop, gracefully. + */ + virtual void + stop(void) throw () + { + shouldRun = false; + } +}; + + +/* ================================================= external data structures */ + + +/* ====================================================== function prototypes */ + + +} // namespace PlaylistExecutor +} // namespace LiveSupport + + +#endif // HelixEventHandlerThread_h + diff --git a/livesupport/modules/playlistExecutor/src/HelixPlayer.cxx b/livesupport/modules/playlistExecutor/src/HelixPlayer.cxx index 4775a62a7..d1859d750 100644 --- a/livesupport/modules/playlistExecutor/src/HelixPlayer.cxx +++ b/livesupport/modules/playlistExecutor/src/HelixPlayer.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.4 $ + Version : $Revision: 1.5 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/playlistExecutor/src/Attic/HelixPlayer.cxx,v $ ------------------------------------------------------------------------------*/ @@ -36,6 +36,7 @@ #include "HelixDefs.h" #include "LiveSupport/Core/TimeConversion.h" +#include "HelixEventHandlerThread.h" #include "HelixPlayer.h" @@ -77,25 +78,6 @@ static const std::string clntcoreName = "/clntcore.so"; /* =============================================== local function prototypes */ -/*------------------------------------------------------------------------------ - * The main thread function for handling Helix events. - *----------------------------------------------------------------------------*/ -void * -LiveSupport::PlaylistExecutor::eventHandlerThread(void * helixPlayer) - throw () -{ - HelixPlayer * hPlayer = (HelixPlayer *) helixPlayer; - Ptr::Ref sleepT(new time_duration(microseconds(10))); - - while (hPlayer->handleEvents) { - struct _HXxEvent * event = 0; - hPlayer->clientEngine->EventOccurred(event); - TimeConversion::sleep(sleepT); - } - - return 0; -} - /* ============================================================= module code */ @@ -202,15 +184,12 @@ HelixPlayer :: initialize(void) throw (std::exception) } // start the event handling thread - handleEvents = true; - int ret; - if ((ret = pthread_create(&eventHandlingThread, - NULL, - eventHandlerThread, - (void *) this))) { - // TODO: signal return code - throw std::exception(); - } + Ptr::Ref granurality(new time_duration(microseconds(10))); + Ptr::Ref handler(new HelixEventHandlerThread( + clientEngine, + granurality)); + eventHandlerThread.reset(new Thread(handler)); + eventHandlerThread->start(); // set up other variables playing = false; @@ -226,8 +205,8 @@ HelixPlayer :: deInitialize(void) throw () { if (initialized) { // signal stop to and wait for the event handling thread to stop - handleEvents = false; - pthread_join(eventHandlingThread, 0); + eventHandlerThread->stop(); + eventHandlerThread->join(); // release Helix resources clientContext->Release(); diff --git a/livesupport/modules/playlistExecutor/src/HelixPlayer.h b/livesupport/modules/playlistExecutor/src/HelixPlayer.h index 554a38994..a64660caf 100644 --- a/livesupport/modules/playlistExecutor/src/HelixPlayer.h +++ b/livesupport/modules/playlistExecutor/src/HelixPlayer.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.3 $ + Version : $Revision: 1.4 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/playlistExecutor/src/Attic/HelixPlayer.h,v $ ------------------------------------------------------------------------------*/ @@ -41,12 +41,11 @@ #endif -#include - #include #include #include "LiveSupport/Core/Configurable.h" +#include "LiveSupport/Core/Thread.h" #include "LiveSupport/PlaylistExecutor/AudioPlayerInterface.h" #include "AdviseSink.h" @@ -90,13 +89,11 @@ using namespace LiveSupport::Core; * * * @author $Author: maroy $ - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ */ class HelixPlayer : virtual public Configurable, virtual public AudioPlayerInterface { - friend void * eventHandlerThread(void *) throw(); - private: /** * The name of the configuration XML elmenent used by HelixPlayer @@ -143,24 +140,11 @@ class HelixPlayer : virtual public Configurable, */ std::string url; - /** - * The event handling thread. - */ - pthread_t eventHandlingThread; - /** * Flag to indicate if this object has been initialized. */ bool initialized; - /** - * Flag to mark if the event handling thread should be running - * and handling events. - * This is set by the HelixPlayer object, and read by the thread - * to determine when to stop. - */ - bool handleEvents; - /** * Flag to indicate if the player is currently playing. * Make sure no to rely on this flag, as it's not aware of @@ -169,6 +153,12 @@ class HelixPlayer : virtual public Configurable, */ bool playing; + /** + * A thread for handling helix events, on a regular basis. + * Helix apperantly needs to be polled all the time to function. + */ + Ptr::Ref eventHandlerThread; + public: /** @@ -284,17 +274,6 @@ class HelixPlayer : virtual public Configurable, /* ====================================================== function prototypes */ -/** - * The main function of the thread that calls for handling of events - * in the createEngine all the time. - * - * @param helixPlayer a pointer to the HelixPlayer object that started - * this thread. - * @return always 0 - */ -void * -eventHandlerThread(void * helixPlayer) throw (); - } // namespace PlaylistExecutor } // namespace LiveSupport