Change config class from having a method for every configurable value

to returing values for string keys so adding a new config doesn't mean
modifying that class.  Then add new config for addr passed to bind so
relay can be run to accept http connections from remote machines.
This commit is contained in:
ehouse 2009-03-02 01:50:14 +00:00
parent f09994153c
commit 54693142bd
7 changed files with 81 additions and 60 deletions

View file

@ -27,14 +27,6 @@
#define MAX_LINE 128
#ifndef _HEARTBEAT
# define _HEARTBEAT 60
#endif
#ifndef _ALLCONN
# define _ALLCONN 120
#endif
RelayConfigs* RelayConfigs::instance = NULL;
@ -52,8 +44,6 @@ RelayConfigs::InitConfigs( const char* cfile )
}
RelayConfigs::RelayConfigs( const char* cfile )
: m_allConnInterval(_ALLCONN)
, m_heartbeatInterval(_HEARTBEAT)
{
/* There's an order here. Open multiple files, if present. File in /etc
is first, but overridden by local file which is in turn overridden by
@ -64,12 +54,46 @@ RelayConfigs::RelayConfigs( const char* cfile )
} /* RelayConfigs::RelayConfigs */
void
RelayConfigs::GetPorts( std::vector<int>::const_iterator* iter, std::vector<int>::const_iterator* end)
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 )
{
map<string,string>::const_iterator iter = m_values.find(key);
bool found = iter != m_values.end();
if ( found ) {
*value = atoi( iter->second.c_str() );
}
return found;
}
bool
RelayConfigs::GetValueFor( const char* key, time_t* value )
{
int val;
bool success = GetValueFor( key, &val );
if ( success ) {
*value = val;
}
return success;
}
bool
RelayConfigs::GetValueFor( const char* key, char* buf, int len )
{
map<string,string>::const_iterator iter = m_values.find(key);
bool found = iter != m_values.end();
if ( found ) {
snprintf( buf, len, "%s", iter->second.c_str() );
}
return found;
}
ino_t
RelayConfigs::parse( const char* fname, ino_t prev )
{
@ -104,28 +128,12 @@ RelayConfigs::parse( const char* fname, ino_t prev )
}
*value++ = '\0'; /* terminate "key" substring */
if ( 0 == strcmp( line, "HEARTBEAT" ) ) {
m_heartbeatInterval = atoi( value );
} else if ( 0 == strcmp( line, "ALLCONN" ) ) {
m_allConnInterval = atoi( value );
} else if ( 0 == strcmp( line, "CTLPORT" ) ) {
m_ctrlport = atoi( value );
} else if ( 0 == strcmp( line, "WWWPORT" ) ) {
m_httpport = atoi( value );
} else if ( 0 == strcmp( line, "PORT" ) ) {
m_values.insert( pair<string,string>
(string(line),string(value) ) );
if ( 0 == strcmp( line, "PORT" ) ) {
m_ports.push_back( atoi( value ) );
} else if ( 0 == strcmp( line, "NTHREADS" ) ) {
m_nWorkerThreads = atoi( value );
} else if ( 0 == strcmp( line, "SERVERNAME" ) ) {
m_serverName = value;
} else if ( 0 == strcmp( line, "IDFILE" ) ) {
m_idFileName = value;
} else if ( 0 == strcmp( line, "LOGLEVEL" ) ) {
m_logLevel = atoi(value);
} else {
logf( XW_LOGERROR, "unknown key %s with value %s\n",
line, value );
assert( 0 );
}
}
fclose( f );
@ -134,3 +142,4 @@ RelayConfigs::parse( const char* fname, ino_t prev )
}
return inode;
} /* parse */

View file

@ -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
@ -41,29 +42,24 @@ class RelayConfigs {
~RelayConfigs() {}
void GetPorts( vector<int>::const_iterator* iter, vector<int>::const_iterator* end);
int GetCtrlPort() { return m_ctrlport; }
int GetHttpPort() { return m_httpport; }
int GetNWorkerThreads() { return m_nWorkerThreads; }
time_t GetAllConnectedInterval() { return m_allConnInterval; }
time_t GetHeartbeatInterval() { return m_heartbeatInterval; }
const char* GetServerName() { return m_serverName.c_str(); }
const char* GetIdFileName() { return m_idFileName.c_str(); }
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, char* buf, int len );
private:
RelayConfigs( const char* cfile );
ino_t parse( const char* fname, ino_t prev );
time_t m_allConnInterval;
time_t m_heartbeatInterval;
int m_ctrlport;
int m_httpport;
vector<int> m_ports;
int m_logLevel;
int m_nWorkerThreads;
string m_serverName;
string m_idFileName;
map<string,string> m_values;
static RelayConfigs* instance;
};

View file

@ -72,8 +72,7 @@ SocketsIterator::Next()
*****************************************************************************/
CookieRef::CookieRef( const char* cookie, const char* connName, CookieID id )
: m_heatbeat(RelayConfigs::GetConfigs()->GetHeartbeatInterval())
, m_cookie(cookie==NULL?"":cookie)
: m_cookie(cookie==NULL?"":cookie)
, m_connName(connName)
, m_cookieID(id)
, m_totalSent(0)
@ -84,6 +83,7 @@ CookieRef::CookieRef( const char* cookie, const char* connName, CookieID id )
, m_nPlayersTotal(0)
, m_nPlayersHere(0)
{
RelayConfigs::GetConfigs()->GetValueFor( "HEARTBEAT", &m_heatbeat );
logf( XW_LOGINFO, "creating cref for cookie %s, connName %s",
m_cookie.c_str(), m_connName.c_str() );
}
@ -613,7 +613,7 @@ void
CookieRef::setAllConnectedTimer()
{
time_t inHowLong;
inHowLong = RelayConfigs::GetConfigs()->GetAllConnectedInterval();
RelayConfigs::GetConfigs()->GetValueFor( "ALLCONN", &inHowLong );
TimerMgr::GetTimerMgr()->SetTimer( inHowLong,
s_checkAllConnected, this, 0 );
}

View file

@ -76,7 +76,7 @@ class CookieRef {
const char* Cookie() { return m_cookie.c_str(); }
const char* ConnName() { return m_connName.c_str(); }
short GetHeartbeat() { return m_heatbeat; }
int GetHeartbeat() { return m_heatbeat; }
int SocketForHost( HostID dest );
bool NeverFullyConnected();
@ -192,7 +192,7 @@ class CookieRef {
map<HostID,HostRec> m_sockets;
/* pthread_rwlock_t m_sockets_rwlock; */
short m_heatbeat; /* might change per carrier or something. */
int m_heatbeat; /* might change per carrier or something. */
string m_cookie; /* cookie used for initial connections */
string m_connName; /* globally unique name */
CookieID m_cookieID; /* Unique among current games on this server */

View file

@ -435,7 +435,8 @@ CRefMgr::AddNew( const char* cookie, const char* connName, CookieID id )
#ifdef RELAY_HEARTBEAT
if ( m_cookieMap.size() == 1 ) {
RelayConfigs* cfg = RelayConfigs::GetConfigs();
short heartbeat = cfg->GetHeartbeatInterval();
int heartbeat;
cfg->GetValueFor( "HEARTBEAT", &heartbeat );
TimerMgr::GetTimerMgr()->SetTimer( heartbeat, heartbeatProc, this,
heartbeat );
}

View file

@ -26,6 +26,10 @@ CTLPORT=11000
# port for web interface
WWWPORT=11001
#--- INADDR_ANY: 0x00000000
#WWW_LISTEN_ADDR=0
#--- INADDR_LOOPBACK: 0x7f000001/2130706433
WWW_LISTEN_ADDR=2130706433
# Need a unique name for every instance of the relay so they can
# create game ids guaranteed to be unique

View file

@ -624,21 +624,29 @@ main( int argc, char** argv )
RelayConfigs* cfg = RelayConfigs::GetConfigs();
if ( ctrlport == 0 ) {
ctrlport = cfg->GetCtrlPort();
(void)cfg->GetValueFor( "CTLPORT", &ctrlport );
}
#ifdef DO_HTTP
if ( httpport == 0 ) {
httpport = cfg->GetHttpPort();
(void)cfg->GetValueFor( "WWWPORT", &httpport );
}
#endif
if ( nWorkerThreads == 0 ) {
nWorkerThreads = cfg->GetNWorkerThreads();
(void)cfg->GetValueFor( "NTHREADS", &nWorkerThreads );
}
char serverNameBuf[128];
if ( serverName == NULL ) {
serverName = cfg->GetServerName();
if ( cfg->GetValueFor( "SERVERNAME", serverNameBuf,
sizeof(serverNameBuf) ) ) {
serverName = serverNameBuf;
}
}
char idFileNameBuf[128];
if ( idFileName == NULL ) {
idFileName = cfg->GetIdFileName();
if ( cfg->GetValueFor( "IDFILE", idFileNameBuf, sizeof(idFileName) ) ) {
idFileName = idFileNameBuf;
}
}
PermID::SetServerName( serverName );
@ -720,9 +728,12 @@ main( int argc, char** argv )
exit( 1 );
}
#ifdef DO_HTTP
g_http = make_socket( INADDR_LOOPBACK, httpport );
if ( g_http == -1 ) {
exit( 1 );
int addr;
if ( cfg->GetValueFor( "WWW_LISTEN_ADDR", &addr ) ) {
g_http = make_socket( addr, httpport );
if ( g_http == -1 ) {
exit( 1 );
}
}
#endif