add db method to count messages for connName/hid pairs and handle

query using it.
This commit is contained in:
Andy2 2010-09-24 05:50:02 -07:00
parent c3e1b243b6
commit b0c6e6da9c
3 changed files with 66 additions and 1 deletions

View file

@ -256,6 +256,34 @@ DBMgr::PublicRooms( int lang, int nPlayers, int* nNames, string& names )
*nNames = nTuples;
}
int
DBMgr::PendingMsgCount( const char* connNameIDPair )
{
int count = 0;
const char* hid = strrchr( connNameIDPair, '/' );
if ( NULL != hid ) {
char name[MAX_CONNNAME_LEN];
int connNameLen = hid - connNameIDPair;
strncpy( name, connNameIDPair, connNameLen );
name[connNameLen] = '\0';
const char* fmt = "SELECT COUNT(*) FROM " MSGS_TABLE
" WHERE connName = '%s' AND hid = %s";
char query[256];
snprintf( query, sizeof(query), fmt, name, hid+1 );
logf( XW_LOGINFO, "%s: query: %s", __func__, query );
MutexLock ml( &m_dbMutex );
PGresult* result = PQexec( m_pgconn, query );
if ( 1 == PQntuples( result ) ) {
count = atoi( PQgetvalue( result, 0, 0 ) );
}
PQclear( result );
}
return count;
}
void
DBMgr::execSql( const char* query )
{

View file

@ -57,6 +57,9 @@ class DBMgr {
queries.*/
void PublicRooms( int lang, int nPlayers, int* nNames, string& names );
/* Return number of messages pending for connName:hostid pair passed in */
int PendingMsgCount( const char* const connNameIDPair );
/* message storage -- different DB */
int CountStoredMessages( const char* const connName );
void StoreMessage( const char* const connName, int hid,

View file

@ -671,9 +671,11 @@ handle_proxy_tproc( void* closure )
int sock = (int)closure;
unsigned char buf[MAX_PROXY_MSGLEN];
int len = read_packet( sock, buf, sizeof(buf) );
int len = read_packet( sock, buf, sizeof(buf)-1 );
if ( len > 0 ) {
buf[len] = '\0'; /* so can use strtok */
unsigned char* bufp = buf;
unsigned char* end = bufp + len;
if ( (0 == *bufp++) ) { /* protocol */
XWPRXYCMD cmd = (XWPRXYCMD)*bufp++;
switch( cmd ) {
@ -695,6 +697,38 @@ handle_proxy_tproc( void* closure )
}
break;
case PRX_HAS_MSGS:
if ( len >= 2 ) {
unsigned short nameCount;
if ( getNetShort( &bufp, end, &nameCount ) ) {
char* in = (char*)bufp;
char* saveptr;
vector<int> ids;
for ( ; ; ) {
char* name = strtok_r( in, "\n", &saveptr );
if ( NULL == name ) {
break;
}
ids.push_back( DBMgr::Get()->
PendingMsgCount( name ) );
in = NULL;
}
unsigned short len =
(ids.size() * sizeof(unsigned short))
+ sizeof( unsigned short );
len = htons( len );
write( sock, &len, sizeof(len) );
len = htons( nameCount );
write( sock, &len, sizeof(len) );
vector<int>::const_iterator iter;
for ( iter = ids.begin(); iter != ids.end(); ++iter ) {
unsigned short num = *iter;
num = htons( num );
write( sock, &num, sizeof(num) );
}
}
}
break;
}
}