From 4bd5553596eebd979f257e96d90f8d427fc9284c Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 1 Dec 2010 21:08:22 -0800 Subject: [PATCH] track mtime per host rather than per game -- make it an array in the db. --- xwords4/relay/cref.cpp | 20 ++++++++++++++++++-- xwords4/relay/cref.h | 1 + xwords4/relay/dbmgr.cpp | 29 +++++++++++++++-------------- xwords4/relay/dbmgr.h | 4 ++-- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/xwords4/relay/cref.cpp b/xwords4/relay/cref.cpp index c9f9e9a19..33053b458 100644 --- a/xwords4/relay/cref.cpp +++ b/xwords4/relay/cref.cpp @@ -256,6 +256,22 @@ CookieRef::_Shutdown() handleEvents(); } /* _Shutdown */ +HostID +CookieRef::HostForSocket( int sock ) +{ + HostID hid = -1; + ASSERT_LOCKED(); + vector::const_iterator iter; + for ( iter = m_sockets.begin(); iter != m_sockets.end(); ++iter ) { + if ( iter->m_socket == sock ) { + hid = iter->m_hostID; + logf( XW_LOGINFO, "%s: assigning hid of %d", __func__, hid ); + break; + } + } + return hid; +} + int CookieRef::SocketForHost( HostID dest ) { @@ -590,9 +606,9 @@ CookieRef::handleEvents() /* break; */ case XWA_SEND_RERSP: + increasePlayerCounts( &evt, true ); sendResponse( &evt, false ); sendAnyStored( &evt ); - increasePlayerCounts( &evt, true ); postCheckAllHere(); break; @@ -994,7 +1010,7 @@ CookieRef::send_msg( int socket, HostID id, XWRelayMsg msg, XWREASON why, void CookieRef::RecordSent( int nBytes, int socket ) { m_totalSent += nBytes; - DBMgr::Get()->RecordSent( ConnName(), nBytes ); + DBMgr::Get()->RecordSent( ConnName(), HostForSocket(socket), nBytes ); } void diff --git a/xwords4/relay/cref.h b/xwords4/relay/cref.h index 48a7720cb..e6638e1ec 100644 --- a/xwords4/relay/cref.h +++ b/xwords4/relay/cref.h @@ -93,6 +93,7 @@ class CookieRef { int GetHeartbeat() { return m_heatbeat; } int SocketForHost( HostID dest ); + HostID HostForSocket( int sock ); /* connect case */ bool AlreadyHere( unsigned short seed, int socket ); diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index 4ea2cb726..6fb537e15 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -208,10 +208,10 @@ DBMgr::AddDevice( const char* connName, HostID curID, int nToAdd, assert( newID <= 4 ); const char* fmt = "UPDATE " GAMES_TABLE " SET nPerDevice[%d] = %d," - " seeds[%d] = %d, mtime='now'" + " seeds[%d] = %d, mtimes[%d]='now'" " WHERE connName = '%s'"; char query[256]; - snprintf( query, sizeof(query), fmt, newID, nToAdd, newID, seed, connName ); + snprintf( query, sizeof(query), fmt, newID, nToAdd, newID, seed, newID, connName ); logf( XW_LOGINFO, "%s: query: %s", __func__, query ); execSql( query ); @@ -223,9 +223,9 @@ bool DBMgr::RmDevice( const char* connName, HostID hid ) { const char* fmt = "UPDATE " GAMES_TABLE " SET nPerDevice[%d] = 0, " - "seeds[%d] = 0, mtime='now' WHERE connName = '%s'"; + "seeds[%d] = 0, mtimes[%d]='now' WHERE connName = '%s'"; char query[256]; - snprintf( query, sizeof(query), fmt, hid, hid, connName ); + snprintf( query, sizeof(query), fmt, hid, hid, hid, connName ); logf( XW_LOGINFO, "%s: query: %s", __func__, query ); return execSql( query ); @@ -249,8 +249,8 @@ DBMgr::HaveDevice( const char* connName, HostID hid, int seed ) void DBMgr::AddCID( const char* const connName, CookieID cid ) { - const char* fmt = "UPDATE " GAMES_TABLE " SET cid = %d, " - " mtime='now' WHERE connName = '%s'"; + const char* fmt = "UPDATE " GAMES_TABLE " SET cid = %d " + " WHERE connName = '%s'"; char query[256]; snprintf( query, sizeof(query), fmt, cid, connName ); logf( XW_LOGINFO, "%s: query: %s", __func__, query ); @@ -271,16 +271,17 @@ DBMgr::ClearCID( const char* connName ) } void -DBMgr::RecordSent( const char* const connName, int nBytes ) +DBMgr::RecordSent( const char* const connName, HostID hid, int nBytes ) { - const char* fmt = "UPDATE " GAMES_TABLE " SET" - " nsent = nsent + %d, mtime = 'now'" - " WHERE connName = '%s'"; - char query[256]; - snprintf( query, sizeof(query), fmt, nBytes, connName ); - logf( XW_LOGINFO, "%s: query: %s", __func__, query ); + assert( hid >= 0 && hid <= 4 ); + const char* fmt = "UPDATE " GAMES_TABLE " SET" + " nsent = nsent + %d, mtimes[%d] = 'now'" + " WHERE connName = '%s'"; + char query[256]; + snprintf( query, sizeof(query), fmt, nBytes, hid, connName ); + logf( XW_LOGINFO, "%s: query: %s", __func__, query ); - execSql( query ); + execSql( query ); } void diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index 7d778a46e..196b13420 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -52,9 +52,9 @@ class DBMgr { int nToAdd, unsigned short seed ); bool RmDevice( const char* const connName, HostID id ); bool HaveDevice( const char* const connName, HostID id, int seed ); - void AddCID( const char* connName, CookieID cid ); + void AddCID( const char* const connName, CookieID cid ); void ClearCID( const char* connName ); - void RecordSent( const char* const connName, int nBytes ); + void RecordSent( const char* const connName, HostID hid, int nBytes ); void GetPlayerCounts( const char* const connName, int* nTotal, int* nHere );