rename now() as uptime(); drop NTHREADS to 1 for now; include time of

server boot in connName and keep counter in memory rather than a file.
This commit is contained in:
ehouse 2009-03-10 12:52:17 +00:00
parent 13037b059d
commit 483858b0fb
10 changed files with 32 additions and 59 deletions

View file

@ -780,8 +780,8 @@ CookieRef::noteHeartbeat( const CRefEvent* evt )
assert( iter->second.m_socket == socket );
logf( XW_LOGVERBOSE1, "upping m_lastHeartbeat from %d to %d",
iter->second.m_lastHeartbeat, now() );
iter->second.m_lastHeartbeat = now();
iter->second.m_lastHeartbeat, uptime() );
iter->second.m_lastHeartbeat = uptime();
}
} /* noteHeartbeat */
@ -856,7 +856,7 @@ CookieRef::_PrintCookieInfo( string& out )
/* open since when */
snprintf( buf, sizeof(buf), "Hosts connected=%d; cur time = %ld\n",
m_sockets.size(), now() );
m_sockets.size(), uptime() );
out += buf;
map<HostID,HostRec>::iterator iter = m_sockets.begin();
while ( iter != m_sockets.end() ) {

View file

@ -41,7 +41,7 @@ class HostRec {
: m_socket(socket)
, m_nPlayersH(nPlayersH)
, m_nPlayersT(nPlayersT)
, m_lastHeartbeat(now())
, m_lastHeartbeat(uptime())
{}
~HostRec() {}
int m_socket;

View file

@ -413,7 +413,7 @@ CRefMgr::UnlockCref( CookieRef* cref )
CRefMgr::heartbeatProc( void* closure )
{
CRefMgr* self = (CRefMgr*)closure;
self->checkHeartbeats( now() );
self->checkHeartbeats( uptime() );
} /* heartbeatProc */
#endif

View file

@ -348,7 +348,7 @@ cmd_rev( int socket, const char** args )
void
format_uptime( char* buf, int len )
{
time_t seconds = now();
time_t seconds = uptime();
int days = seconds / (24*60*60);
seconds %= (24*60*60);

View file

@ -29,38 +29,18 @@ using namespace std;
pthread_mutex_t PermID::s_guard = PTHREAD_MUTEX_INITIALIZER;
string PermID::s_serverName;
string PermID::s_idFileName;
int PermID::s_nextId = 0;
string PermID::s_startTime;
string
PermID::GetNextUniqueID()
{
const char* fileName = s_idFileName.c_str();
MutexLock ml( &s_guard );
string s = s_serverName;
assert( s.length() > 0 );
s += ":";
char buf[32]; /* should last for a while :-) */
FILE* f = fopen( fileName, "r+" );
if ( f ) {
fscanf( f, "%s\n", buf );
rewind( f );
} else {
f = fopen( fileName, "w" );
assert ( f != NULL );
buf[0] = '0';
buf[1] = '\0';
}
int n = atoi(buf) + 1;
sprintf( buf, "%d", n );
fprintf( f, "%s\n", buf );
fclose( f );
s += buf;
char buf[64];
snprintf( buf, sizeof(buf), "%s:%s:%d", s_serverName.c_str(),
s_startTime.c_str(), ++s_nextId );
string s(buf);
return s;
}
@ -72,7 +52,10 @@ PermID::SetServerName( const char* name )
}
/* static */ void
PermID::SetIDFileName( const char* name )
PermID::SetStartTime( time_t startTime )
{
s_idFileName = name;
char buf[16];
snprintf( buf, sizeof(buf), "%ld", startTime );
s_startTime = buf;
logf( XW_LOGINFO, "assigned startTime: %s", s_startTime.c_str() );
}

View file

@ -34,7 +34,7 @@
class PermID {
public:
static void SetServerName( const char* name );
static void SetIDFileName( const char* name );
static void SetStartTime( time_t startTime );
static std::string GetNextUniqueID();
private:
@ -44,7 +44,8 @@ class PermID {
this, which is supposed to be
unique to this relay
instance. */
static std::string s_idFileName; /* The incremented part of the
name is stored where? */
static int s_nextId; /* numeric part of ID */
static std::string s_startTime; /* allows multiple servers per
host to have uniquie names */
};
#endif

View file

@ -47,11 +47,11 @@ void
TimerMgr::SetTimer( time_t inMillis, TimerProc proc, void* closure,
int interval )
{
logf( XW_LOGINFO, "setTimer: now = %d", now() );
logf( XW_LOGINFO, "setTimer: uptime = %ld", uptime() );
TimerInfo ti;
ti.proc = proc;
ti.closure = closure;
ti.when = now() + inMillis;
ti.when = uptime() + inMillis;
ti.interval = interval;
MutexLock ml( &m_timersMutex );
@ -75,7 +75,7 @@ TimerMgr::GetPollTimeout()
if ( tout == 0 ) {
tout = -1;
} else {
tout -= now();
tout -= uptime();
if ( tout < 0 ) {
tout = 0;
}
@ -103,7 +103,7 @@ TimerMgr::figureNextFire()
{
/* Don't call this unless have the lock!!! */
time_t t = 0x7FFFFFFF;
time_t cur = now();
time_t cur = uptime();
list<TimerInfo>::iterator iter = m_timers.begin();
@ -141,7 +141,7 @@ TimerMgr::FireElapsedTimers()
{
pthread_mutex_lock( &m_timersMutex );
time_t curTime = now();
time_t curTime = uptime();
vector<TimerProc> procs;
vector<void*> closures;

View file

@ -12,7 +12,7 @@ HEARTBEAT=60
ALLCONN=300
# How many worker threads in the thread pool? Default is five.
NTHREADS=5
NTHREADS=1
# What port do we listen on for incomming connections?
PORTS=10997,10998,10999
@ -32,10 +32,6 @@ WWW_REFRESH_SECS=30
# create game ids guaranteed to be unique
SERVERNAME=eehouse.org
# Where will the file live that stores the last ID used for a new
# connName.
IDFILE=./xwrelay_id.txt
# Initial level of logging. See xwrelay_priv.h for values. Currently
# 0 means errors only, 1 info, 2 verbose and 3 very verbose.
LOGLEVEL=2

View file

@ -360,7 +360,7 @@ killSocket( int socket, const char* why )
}
time_t
now( void )
uptime( void )
{
static time_t startTime = time(NULL);
return time(NULL) - startTime;
@ -563,7 +563,7 @@ main( int argc, char** argv )
bool doDaemon = true;
bool doFork = true;
(void)now(); /* force capture of start time */
(void)uptime(); /* force capture of start time */
/* Verify sizes here... */
assert( sizeof(CookieID) == 2 );
@ -651,15 +651,8 @@ main( int argc, char** argv )
}
}
char idFileNameBuf[128];
if ( idFileName == NULL ) {
if ( cfg->GetValueFor( "IDFILE", idFileNameBuf, sizeof(idFileName) ) ) {
idFileName = idFileNameBuf;
}
}
PermID::SetServerName( serverName );
PermID::SetIDFileName( idFileName );
PermID::SetStartTime( time(NULL) );
/* add signal handling here */

View file

@ -21,7 +21,7 @@ void killSocket( int socket, const char* why );
bool send_with_length_unsafe( int socket, unsigned char* buf, int bufLen );
time_t now(void);
time_t uptime(void);
int make_socket( unsigned long addr, unsigned short port );