From 54693142bdad558f69ff07b7a227f1cc975c7287 Mon Sep 17 00:00:00 2001 From: ehouse Date: Mon, 2 Mar 2009 01:50:14 +0000 Subject: [PATCH] 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. --- relay/configs.cpp | 73 ++++++++++++++++++++++++++-------------------- relay/configs.h | 24 +++++++-------- relay/cref.cpp | 6 ++-- relay/cref.h | 4 +-- relay/crefmgr.cpp | 3 +- relay/xwrelay.conf | 4 +++ relay/xwrelay.cpp | 27 ++++++++++++----- 7 files changed, 81 insertions(+), 60 deletions(-) diff --git a/relay/configs.cpp b/relay/configs.cpp index fb7e4a7d7..7592ec83e 100644 --- a/relay/configs.cpp +++ b/relay/configs.cpp @@ -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::const_iterator* iter, std::vector::const_iterator* end) +RelayConfigs::GetPorts( std::vector::const_iterator* iter, + std::vector::const_iterator* end) { *iter = m_ports.begin(); *end = m_ports.end(); } +bool +RelayConfigs::GetValueFor( const char* key, int* value ) +{ + map::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::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(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 */ + diff --git a/relay/configs.h b/relay/configs.h index e16dc0bde..533089a17 100644 --- a/relay/configs.h +++ b/relay/configs.h @@ -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::const_iterator* iter, vector::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::const_iterator* iter, + vector::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 m_ports; int m_logLevel; - int m_nWorkerThreads; - string m_serverName; - string m_idFileName; + + map m_values; static RelayConfigs* instance; }; diff --git a/relay/cref.cpp b/relay/cref.cpp index 97339deca..98b80169d 100644 --- a/relay/cref.cpp +++ b/relay/cref.cpp @@ -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 ); } diff --git a/relay/cref.h b/relay/cref.h index bbeaf7e8b..1828df29f 100644 --- a/relay/cref.h +++ b/relay/cref.h @@ -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 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 */ diff --git a/relay/crefmgr.cpp b/relay/crefmgr.cpp index 85a96bc7f..72f18bca2 100644 --- a/relay/crefmgr.cpp +++ b/relay/crefmgr.cpp @@ -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 ); } diff --git a/relay/xwrelay.conf b/relay/xwrelay.conf index 67e96faae..4c45dd27e 100644 --- a/relay/xwrelay.conf +++ b/relay/xwrelay.conf @@ -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 diff --git a/relay/xwrelay.cpp b/relay/xwrelay.cpp index d79a5df3b..6d5f51cda 100644 --- a/relay/xwrelay.cpp +++ b/relay/xwrelay.cpp @@ -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