From 61e31de98b97f556cfb2a0cafa172bde673e7859 Mon Sep 17 00:00:00 2001 From: Sascha Volkenandt Date: Thu, 4 Jan 2007 17:42:33 +0000 Subject: - removed thread-unsafe call of cPlugin::ConfigDirectory and replaced it with Plugin::GetConfigDirectory - implemented proper shutdown of server thread on vdr termination --- live.cpp | 6 +++++- live.h | 5 +++++ thread.cpp | 33 ++++++++++++++++++++++++++------- thread.h | 7 ++++++- tntconfig.cpp | 5 +++-- 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/live.cpp b/live.cpp index 88f4718..fd83cb4 100644 --- a/live.cpp +++ b/live.cpp @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: live.cpp,v 1.6 2007/01/04 15:02:00 lordjaxom Exp $ + * $Id: live.cpp,v 1.7 2007/01/04 17:42:33 lordjaxom Exp $ */ #include @@ -20,8 +20,11 @@ using namespace std; const char *Plugin::VERSION = "0.0.1"; const char *Plugin::DESCRIPTION = "Live Integrated VDR Environment"; +std::string Plugin::m_configDirectory; + Plugin::Plugin(void) { + m_configDirectory = cPlugin::ConfigDirectory( PLUGIN_NAME_I18N ); } const char *Plugin::CommandLineHelp(void) @@ -45,6 +48,7 @@ bool Plugin::Start(void) void Plugin::Stop(void) { + m_thread->Stop(); } void Plugin::MainThreadHook(void) diff --git a/live.h b/live.h index ac1188b..eba7108 100644 --- a/live.h +++ b/live.h @@ -2,6 +2,7 @@ #define VDR_LIVE_LIVE_H #include +#include #include #include "thread.h" @@ -40,10 +41,14 @@ public: virtual Setup& GetLiveSetup(); virtual TimerManager& GetLiveTimerManager(); + static std::string const& GetConfigDirectory() { return m_configDirectory; } + private: static const char *VERSION; static const char *DESCRIPTION; + static std::string m_configDirectory; + std::auto_ptr< ServerThread > m_thread; }; diff --git a/thread.cpp b/thread.cpp index cb2954a..9b0b436 100644 --- a/thread.cpp +++ b/thread.cpp @@ -11,25 +11,44 @@ namespace vdrlive { using namespace std; using namespace tnt; -ServerThread::ServerThread(): - m_configPath( 0 ) +class ProtectedCString +{ +public: + ProtectedCString( char const* string ): m_string( strdup( string ) ) {} + ~ProtectedCString() { free( m_string ); } + + operator char*() { return m_string; } + +private: + char* m_string; +}; + +ServerThread::ServerThread() { } ServerThread::~ServerThread() { - free( m_configPath ); + Stop(); +} + +void ServerThread::Stop() +{ + if ( Active() ) { + m_server->shutdown(); + Cancel( 5 ); + } } void ServerThread::Action() { try { - m_configPath = strdup( TntConfig::Get().GetConfigPath().c_str() ); + ProtectedCString configPath( TntConfig::Get().GetConfigPath().c_str() ); - char* argv[] = { "tntnet", "-c", m_configPath }; + char* argv[] = { "tntnet", "-c", configPath }; int argc = sizeof( argv ) / sizeof( argv[0] ); - Tntnet app( argc, argv ); - app.run(); + m_server.reset( new Tntnet( argc, argv ) ); + m_server->run(); } catch ( exception const& ex ) { // XXX move initial error handling to live.cpp esyslog( "ERROR: live httpd server crashed: %s", ex.what() ); diff --git a/thread.h b/thread.h index d0a3bca..ee1c764 100644 --- a/thread.h +++ b/thread.h @@ -1,8 +1,11 @@ #ifndef VDR_LIVE_THREAD_H #define VDR_LIVE_THREAD_H +#include #include +namespace tnt { class Tntnet; } + namespace vdrlive { class ServerThread : public cThread { @@ -10,11 +13,13 @@ public: ServerThread(); virtual ~ServerThread(); + void Stop(); + protected: virtual void Action(); private: - char* m_configPath; + std::auto_ptr< tnt::Tntnet > m_server; }; } // namespace vdrlive diff --git a/tntconfig.cpp b/tntconfig.cpp index 60bfad2..2036a46 100644 --- a/tntconfig.cpp +++ b/tntconfig.cpp @@ -4,6 +4,7 @@ #include #include #include +#include "live.h" #include "setup.h" #include "tntconfig.h" @@ -21,7 +22,7 @@ void TntConfig::WriteConfig() WriteProperties(); ostringstream builder; - builder << cPlugin::ConfigDirectory( PLUGIN_NAME_I18N ) << "/httpd.config"; + builder << Plugin::GetConfigDirectory() << "/httpd.config"; m_configPath = builder.str(); ofstream file( m_configPath.c_str(), ios::out | ios::trunc ); @@ -47,7 +48,7 @@ void TntConfig::WriteConfig() void TntConfig::WriteProperties() { ostringstream builder; - builder << cPlugin::ConfigDirectory( PLUGIN_NAME_I18N ) << "/httpd.properties"; + builder << Plugin::GetConfigDirectory() << "/httpd.properties"; m_propertiesPath = builder.str(); ofstream file( m_propertiesPath.c_str(), ios::out | ios::trunc ); -- cgit v1.2.3