From 68815423f7e6ebd3c1a84356f6c8a830a59f366c Mon Sep 17 00:00:00 2001 From: Sascha Volkenandt Date: Tue, 2 Jan 2007 21:09:12 +0000 Subject: - added options -i and -p to specify port and (possibly multiple) ip(s) - moved commandline-helpstring to setup class to ease maintaining - added default library directory /usr/local/lib --- live.cpp | 4 +-- setup.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- setup.h | 13 +++++++++ tntconfig.cpp | 17 +++++++----- 4 files changed, 107 insertions(+), 11 deletions(-) diff --git a/live.cpp b/live.cpp index da7c300..7cfdeb7 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.1 2007/01/02 19:18:27 lordjaxom Exp $ + * $Id: live.cpp,v 1.2 2007/01/02 21:09:12 lordjaxom Exp $ */ #include @@ -42,7 +42,7 @@ Plugin::Plugin(void) const char *Plugin::CommandLineHelp(void) { - return "-L DIR --lib=DIR libtnt-live.so will be searched in DIR\n"; + return Setup::Get().Help(); } bool Plugin::ProcessArgs(int argc, char *argv[]) diff --git a/setup.cpp b/setup.cpp index ab518dc..538d2c8 100644 --- a/setup.cpp +++ b/setup.cpp @@ -1,26 +1,104 @@ +#include +#include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include #include "setup.h" namespace vdrlive { -Setup::Setup() +using namespace std; + +Setup::Setup(): + m_libraryPath( "/usr/local/lib" ), + m_serverPort( 8001 ) { } bool Setup::Parse( int argc, char* argv[] ) { static struct option opts[] = { - { "lib", required_argument, NULL, 'L' }, + { "lib", required_argument, NULL, 'L' }, + { "port", required_argument, NULL, 'p' }, + { "ip", required_argument, NULL, 'i' }, { 0 } }; int optchar, optind = 0; - while ( ( optchar = getopt_long( argc, argv, "L:", opts, &optind ) ) != -1 ) { + while ( ( optchar = getopt_long( argc, argv, "L:p:i:", opts, &optind ) ) != -1 ) { switch ( optchar ) { case 'L': m_libraryPath = optarg; break; + case 'p': m_serverPort = atoi( optarg ); break; + case 'i': m_serverIps.push_back( optarg ); break; default: return false; } } + + return CheckLibraryPath() && + CheckServerPort() && + CheckServerIps(); +} + +char const* Setup::Help() const +{ + if ( m_helpString.empty() ) { + ostringstream builder; + builder << " -L DIR, --lib=DIR libtnt-live.so will be searched in DIR\n" + " (default: " << m_libraryPath << ")\n" + << " -p PORT, --port=PORT use PORT to listen for incoming\n" + " connections (default: " << m_serverPort << ")\n" + << " -i IP, --ip=IP bind server only to specified IP, can\n" + " appear multiple times\n" + " (default: 0.0.0.0)\n"; + m_helpString = builder.str(); + } + return m_helpString.c_str(); +} + +bool Setup::CheckLibraryPath() +{ + ostringstream builder; + builder << m_libraryPath << "/libtnt-live.so"; + if ( access( builder.str().c_str(), R_OK ) != 0 ) { + esyslog( "ERROR: live can't open content library %s: %s", builder.str().c_str(), strerror( errno ) ); + cerr << "ERROR: live can't open content library " << builder << ": " << strerror( errno ) << endl; + return false; + } + return true; +} + +bool Setup::CheckServerPort() +{ + if ( m_serverPort <= 0 || m_serverPort > numeric_limits< uint16_t >::max() ) { + esyslog( "ERROR: live server port %d is not a valid port number", m_serverPort ); + cerr << "ERROR: live server port " << m_serverPort << " is not a valid port number" << endl; + return false; + } + return true; +} + +bool Setup::CheckServerIps() +{ + if ( m_serverIps.empty() ) { + m_serverIps.push_back( "0.0.0.0" ); + return true; + } + + for ( IpList::const_iterator ip = m_serverIps.begin(); ip != m_serverIps.end(); ++ip ) { + if ( inet_addr( ip->c_str() ) == static_cast< in_addr_t >( -1 ) ) { + esyslog( "ERROR: live server ip %s is not a valid ip address", ip->c_str() ); + cerr << "ERROR: live server ip " << *ip << " is not a valid ip address" << endl; + return false; + } + } return true; } diff --git a/setup.h b/setup.h index d3a6027..6d08ccf 100644 --- a/setup.h +++ b/setup.h @@ -2,23 +2,36 @@ #define VDR_LIVE_SETUP_H #include +#include namespace vdrlive { class Setup { public: + typedef std::list< std::string > IpList; + static Setup& Get(); std::string const& GetLibraryPath() const { return m_libraryPath; } + int GetServerPort() const { return m_serverPort; } + IpList const& GetServerIps() const { return m_serverIps; } bool Parse( int argc, char* argv[] ); + char const* Help() const; private: Setup(); Setup( Setup const& ); + mutable std::string m_helpString; std::string m_libraryPath; + int m_serverPort; + std::list< std::string > m_serverIps; + + bool CheckLibraryPath(); + bool CheckServerPort(); + bool CheckServerIps(); }; } // namespace vdrlive diff --git a/tntconfig.cpp b/tntconfig.cpp index 1b0aadf..3735db2 100644 --- a/tntconfig.cpp +++ b/tntconfig.cpp @@ -32,10 +32,15 @@ void TntConfig::WriteConfig() } // XXX modularize - file << "MapUrl /([^.]+)(\\..+)? $1@libtnt-live" << std::endl; - file << "Listen 0.0.0.0 8003" << std::endl; - file << "PropertyFile " << m_propertiesPath << std::endl; - file << "CompPath " << Setup::Get().GetLibraryPath() << "/" << std::endl; + file << "MapUrl /([^.]+)(\\..+)? $1@libtnt-live" << endl; + file << "PropertyFile " << m_propertiesPath << endl; + file << "CompPath " << Setup::Get().GetLibraryPath() << endl; + + Setup::IpList const& ips = Setup::Get().GetServerIps(); + int port = Setup::Get().GetServerPort(); + for ( Setup::IpList::const_iterator ip = ips.begin(); ip != ips.end(); ++ip ) { + file << "Listen " << *ip << " " << port << endl; + } } void TntConfig::WriteProperties() @@ -52,8 +57,8 @@ void TntConfig::WriteProperties() } // XXX modularize - file << "rootLogger=INFO" << std::endl; - file << "logger.tntnet=INFO" << std::endl; + file << "rootLogger=INFO" << endl; + file << "logger.tntnet=INFO" << endl; } TntConfig const& TntConfig::Get() -- cgit v1.2.3