mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-28 07:58:08 +01:00
do loglevel and ports via the general-purpose config mech. No special
cases remain.
This commit is contained in:
parent
952a73fdbb
commit
cf5a10084e
5 changed files with 66 additions and 37 deletions
|
@ -1,7 +1,8 @@
|
|||
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
||||
|
||||
/*
|
||||
* Copyright 2005 by Eric House (xwords@eehouse.org). All rights reserved.
|
||||
* Copyright 2005-2009 by Eric House (xwords@eehouse.org). All rights
|
||||
* reserved.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
|
@ -53,14 +54,6 @@ RelayConfigs::RelayConfigs( const char* cfile )
|
|||
(void)parse( cfile, prev );
|
||||
} /* RelayConfigs::RelayConfigs */
|
||||
|
||||
void
|
||||
RelayConfigs::GetPorts( std::vector<int>::const_iterator* iter,
|
||||
std::vector<int>::const_iterator* end)
|
||||
{
|
||||
*iter = m_ports.begin();
|
||||
*end = m_ports.end();
|
||||
}
|
||||
|
||||
bool
|
||||
RelayConfigs::GetValueFor( const char* key, int* value )
|
||||
{
|
||||
|
@ -94,6 +87,43 @@ RelayConfigs::GetValueFor( const char* key, char* buf, int len )
|
|||
return found;
|
||||
}
|
||||
|
||||
bool
|
||||
RelayConfigs::GetValueFor( const char* key, vector<int>& ints )
|
||||
{
|
||||
map<string,string>::const_iterator iter = m_values.find(key);
|
||||
bool found = iter != m_values.end();
|
||||
const char* str = iter->second.c_str();
|
||||
int len = strlen(str);
|
||||
char buf[len+1];
|
||||
strcpy( buf, str );
|
||||
char* port = buf;
|
||||
|
||||
for ( ; ; ) {
|
||||
char* end = strstr( port, "," );
|
||||
if ( !!end ) {
|
||||
*end = '\0';
|
||||
}
|
||||
|
||||
logf( XW_LOGINFO, "adding %s to ports", port );
|
||||
ints.push_back( atoi(port) );
|
||||
|
||||
if ( !end ) {
|
||||
break;
|
||||
}
|
||||
port = end + 1;
|
||||
}
|
||||
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
void
|
||||
RelayConfigs::SetValueFor( const char* key, const char* value )
|
||||
{
|
||||
/* Does this leak in the case where we're replacing a value? */
|
||||
m_values.insert( pair<string,string>(string(key),string(value) ) );
|
||||
}
|
||||
|
||||
ino_t
|
||||
RelayConfigs::parse( const char* fname, ino_t prev )
|
||||
{
|
||||
|
@ -131,10 +161,6 @@ RelayConfigs::parse( const char* fname, ino_t prev )
|
|||
|
||||
m_values.insert( pair<string,string>
|
||||
(string(line),string(value) ) );
|
||||
|
||||
if ( 0 == strcmp( line, "PORT" ) ) {
|
||||
m_ports.push_back( atoi( value ) );
|
||||
}
|
||||
}
|
||||
fclose( f );
|
||||
}
|
||||
|
|
|
@ -41,23 +41,18 @@ class RelayConfigs {
|
|||
|
||||
~RelayConfigs() {}
|
||||
|
||||
void GetPorts( vector<int>::const_iterator* iter, vector<int>::const_iterator* end);
|
||||
int GetLogLevel(void) { return m_logLevel; }
|
||||
void SetLogLevel(int ll) { m_logLevel = ll; }
|
||||
|
||||
bool GetValueFor( const char* key, int* intValue );
|
||||
bool GetValueFor( const char* key, time_t* timeValue );
|
||||
bool GetValueFor( const char* key, vector<int>::const_iterator* iter,
|
||||
vector<int>::const_iterator* end );
|
||||
bool GetValueFor( const char* key, vector<int>& ints );
|
||||
bool GetValueFor( const char* key, char* buf, int len );
|
||||
|
||||
void SetValueFor( const char* key, const char* value );
|
||||
|
||||
private:
|
||||
RelayConfigs( const char* cfile );
|
||||
ino_t parse( const char* fname, ino_t prev );
|
||||
|
||||
time_t m_allConnInterval;
|
||||
vector<int> m_ports;
|
||||
int m_logLevel;
|
||||
|
||||
map<string,string> m_values;
|
||||
|
||||
|
|
|
@ -250,9 +250,9 @@ cmd_get( int socket, const char** args )
|
|||
break;
|
||||
case 2: {
|
||||
RelayConfigs* rc = RelayConfigs::GetConfigs();
|
||||
if ( NULL != rc ) {
|
||||
print_to_sock( socket, true, "loglevel=%d\n",
|
||||
rc->GetLogLevel() );
|
||||
int level;
|
||||
if ( NULL != rc && rc->GetValueFor( "LOGLEVEL", &level ) ) {
|
||||
print_to_sock( socket, true, "loglevel=%d\n", level );
|
||||
needsHelp = false;
|
||||
} else {
|
||||
logf( XW_LOGERROR, "RelayConfigs::GetConfigs() => NULL" );
|
||||
|
@ -306,7 +306,7 @@ cmd_set( int socket, const char** args )
|
|||
if ( NULL != val && val[0] != '\0' ) {
|
||||
RelayConfigs* rc = RelayConfigs::GetConfigs();
|
||||
if ( rc != NULL ) {
|
||||
rc->SetLogLevel( atoi(val) );
|
||||
rc->SetValueFor( "LOGLEVEL", val );
|
||||
needsHelp = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,17 +15,13 @@ ALLCONN=300
|
|||
NTHREADS=5
|
||||
|
||||
# What port do we listen on for incomming connections?
|
||||
PORT=10997
|
||||
PORT=10998
|
||||
PORT=10999
|
||||
# intentional duplicate for testing
|
||||
PORT=10999
|
||||
PORTS=10997,10998,10999
|
||||
|
||||
# And the control port is?
|
||||
CTLPORT=11000
|
||||
|
||||
# port for web interface
|
||||
WWWPORT=11001
|
||||
WWW_PORT=11001
|
||||
#--- INADDR_ANY: 0x00000000
|
||||
#WWW_LISTEN_ADDR=0
|
||||
#--- INADDR_LOOPBACK: 0x7f000001/2130706433
|
||||
|
|
|
@ -80,7 +80,16 @@ void
|
|||
logf( XW_LogLevel level, const char* format, ... )
|
||||
{
|
||||
RelayConfigs* rc = RelayConfigs::GetConfigs();
|
||||
if ( NULL == rc || level <= rc->GetLogLevel() ) {
|
||||
int configLevel = level;
|
||||
|
||||
if ( NULL != rc ) {
|
||||
|
||||
if ( ! rc->GetValueFor( "LOGLEVEL", &configLevel ) ) {
|
||||
configLevel = level - 1; /* drop it */
|
||||
}
|
||||
}
|
||||
|
||||
if ( level <= configLevel ) {
|
||||
#ifdef USE_SYSLOG
|
||||
char buf[256];
|
||||
va_list ap;
|
||||
|
@ -628,7 +637,7 @@ main( int argc, char** argv )
|
|||
}
|
||||
#ifdef DO_HTTP
|
||||
if ( httpport == 0 ) {
|
||||
(void)cfg->GetValueFor( "WWWPORT", &httpport );
|
||||
(void)cfg->GetValueFor( "WWW_PORT", &httpport );
|
||||
}
|
||||
#endif
|
||||
if ( nWorkerThreads == 0 ) {
|
||||
|
@ -711,16 +720,19 @@ main( int argc, char** argv )
|
|||
if ( port != 0 ) {
|
||||
g_listeners.AddListener( port );
|
||||
}
|
||||
vector<int>::const_iterator iter, end;
|
||||
cfg->GetPorts( &iter, &end );
|
||||
while ( iter != end ) {
|
||||
vector<int> ints;
|
||||
if ( !cfg->GetValueFor( "PORTS", ints ) ) {
|
||||
exit( 1 );
|
||||
}
|
||||
|
||||
vector<int>::const_iterator iter;
|
||||
for ( iter = ints.begin(); iter != ints.end(); ++iter ) {
|
||||
int port = *iter;
|
||||
if ( !g_listeners.PortInUse( port ) ) {
|
||||
g_listeners.AddListener( port );
|
||||
} else {
|
||||
logf( XW_LOGERROR, "port %d was in use", port );
|
||||
}
|
||||
++iter;
|
||||
}
|
||||
|
||||
g_control = make_socket( INADDR_LOOPBACK, ctrlport );
|
||||
|
|
Loading…
Add table
Reference in a new issue