diff options
Diffstat (limited to 'src/master')
-rw-r--r-- | src/master/Makefile | 17 | ||||
-rw-r--r-- | src/master/common.h | 2 | ||||
-rw-r--r-- | src/master/messages.c | 30 | ||||
-rw-r--r-- | src/master/stats.c | 135 |
4 files changed, 174 insertions, 10 deletions
diff --git a/src/master/Makefile b/src/master/Makefile index df2deed4..dab5c08a 100644 --- a/src/master/Makefile +++ b/src/master/Makefile @@ -3,16 +3,16 @@ BD_RELEASE=release-$(PLATFORM)-$(ARCH) ifeq ($(PLATFORM),mingw32) BINEXT=.exe - RELEASE_LDFLAGS=-lwsock32 - DEBUG_LDFLAGS=-lwsock32 - RM=del - MKDIR=mkdir + RELEASE_LDFLAGS=-lwsock32 + DEBUG_LDFLAGS=-lwsock32 + RM=del + MKDIR=mkdir else BINEXT= - RELEASE_LDFLAGS= - DEBUG_LDFLAGS= - RM=rm -f - MKDIR=mkdir + RELEASE_LDFLAGS=-static -ltdb + DEBUG_LDFLAGS=-static -ltdb + RM=rm -f + MKDIR=mkdir endif CC=gcc @@ -21,6 +21,7 @@ DEBUG_CFLAGS=-g OBJECTS= \ $(BD)/master.o \ $(BD)/messages.o \ + $(BD)/stats.o \ $(BD)/servers.o release: makedirs diff --git a/src/master/common.h b/src/master/common.h index e38aa039..47c29a93 100644 --- a/src/master/common.h +++ b/src/master/common.h @@ -84,5 +84,7 @@ extern char peer_address [128]; // Print a message to screen, depending on its verbose level int MsgPrint (msg_level_t msg_level, const char* format, ...); +void RecordClientStat( const char *address, const char *version, const char *renderer ); +void RecordGameStat( const char *address, const char *dataText ); #endif // #ifndef _COMMON_H_ diff --git a/src/master/messages.c b/src/master/messages.c index 665b9382..b30bc334 100644 --- a/src/master/messages.c +++ b/src/master/messages.c @@ -44,6 +44,9 @@ // "heartbeat Tremulous\n" #define S2M_HEARTBEAT "heartbeat" +// "gamestat <data>" +#define S2M_GAMESTAT "gamestat" + // "getinfo A_Challenge" #define M2S_GETINFO "getinfo" @@ -408,6 +411,7 @@ static void HandleGetMotd( const char* msg, const struct sockaddr_in* addr ) const char *motd = ""; //FIXME size_t packetind; char *value; + char version[ 1024 ], renderer[ 1024 ]; MsgPrint( MSG_DEBUG, "%s ---> getmotd\n", peer_address ); @@ -424,17 +428,21 @@ static void HandleGetMotd( const char* msg, const struct sockaddr_in* addr ) value = SearchInfostring( msg, "renderer" ); if( value ) { - //FIXME: create renderer stats + strncpy( renderer, value, 1024 ); MsgPrint( MSG_DEBUG, "%s is using renderer %s\n", peer_address, value ); } value = SearchInfostring( msg, "version" ); if( value ) { - //FIXME: create version stats + strncpy( version, value, 1024 ); MsgPrint( MSG_DEBUG, "%s is using version %s\n", peer_address, value ); } +#ifndef _WIN32 + RecordClientStat( peer_address, version, renderer ); +#endif + // Initialize the packet contents with the header packetind = headersize; memcpy( packet, packetheader, headersize ); @@ -461,6 +469,18 @@ static void HandleGetMotd( const char* msg, const struct sockaddr_in* addr ) sizeof( *addr ) ); } +/* +==================== +HandleGameStat +==================== +*/ +static void HandleGameStat( const char* msg, const struct sockaddr_in* addr ) +{ +#ifndef _WIN32 + RecordGameStat( peer_address, msg ); +#endif +} + // ---------- Public functions ---------- // /* @@ -523,4 +543,10 @@ void HandleMessage (const char* msg, size_t length, { HandleGetMotd (msg + strlen (C2M_GETMOTD), address); } + + // If it's a game statistic + else if( !strncmp( S2M_GAMESTAT, msg, strlen ( S2M_GAMESTAT ) ) ) + { + HandleGameStat( msg + strlen( S2M_GAMESTAT ), address ); + } } diff --git a/src/master/stats.c b/src/master/stats.c new file mode 100644 index 00000000..98ebb32e --- /dev/null +++ b/src/master/stats.c @@ -0,0 +1,135 @@ +/* +stats.c + +Statistics for tremmaster + +Copyright (C) 2006 Tim Angus + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#ifndef _WIN32 + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <time.h> +#include <tdb.h> + +#include "common.h" + +#define MAX_DATA_SIZE 1024 +#define CS_FILENAME "clientStats.tdb" + +/* +==================== +RecordClientStat +==================== +*/ +void RecordClientStat( const char *address, const char *version, const char *renderer ) +{ + TDB_CONTEXT *tctx = NULL; + TDB_DATA key, data; + char ipText[ 22 ]; + char dataText[ MAX_DATA_SIZE ] = { 0 }; + char *p; + int i; + + tctx = tdb_open( CS_FILENAME, 0, 0, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR ); + + if( !tctx ) + { + MsgPrint( MSG_DEBUG, "Couldn't open %s\n", CS_FILENAME ); + return; + } + + strncpy( ipText, address, 22 ); + if( ( p = strrchr( ipText, ':' ) ) ) // Remove port + *p = '\0'; + + key.dptr = ipText; + key.dsize = strlen( ipText ); + + strncat( dataText, "\"", MAX_DATA_SIZE ); + strncat( dataText, version, MAX_DATA_SIZE ); + + // Remove last three tokens (the date) + for( i = 0; i < 3; i++ ) + { + if( ( p = strrchr( dataText, ' ' ) ) ) + *p = '\0'; + } + strncat( dataText, "\"", MAX_DATA_SIZE ); + + strncat( dataText, " \"", MAX_DATA_SIZE ); + strncat( dataText, renderer, MAX_DATA_SIZE ); + strncat( dataText, "\"", MAX_DATA_SIZE ); + + data.dptr = dataText; + data.dsize = strlen( dataText ); + + if( tdb_store( tctx, key, data, 0 ) < 0 ) + MsgPrint( MSG_DEBUG, "tdb_store failed\n" ); + + tdb_close( tctx ); + MsgPrint( MSG_DEBUG, "Recorded client stat for %s\n", address ); +} + +#define GS_FILENAME "gameStats.tdb" + +/* +==================== +RecordGameStat +==================== +*/ +void RecordGameStat( const char *address, const char *dataText ) +{ + TDB_CONTEXT *tctx = NULL; + TDB_DATA key, data; + char keyText[ MAX_DATA_SIZE ] = { 0 }; + char *p; + time_t tm = time( NULL ); + + tctx = tdb_open( GS_FILENAME, 0, 0, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR ); + + if( !tctx ) + { + MsgPrint( MSG_DEBUG, "Couldn't open %s\n", GS_FILENAME ); + return; + } + + strncpy( keyText, address, 22 ); + if( ( p = strrchr( keyText, ':' ) ) ) // Remove port + *p = '\0'; + + strncat( keyText, " ", MAX_DATA_SIZE ); + strncat( keyText, asctime( gmtime( &tm ) ), MAX_DATA_SIZE ); + + key.dptr = keyText; + key.dsize = strlen( keyText ); + + data.dptr = (char *)dataText; + data.dsize = strlen( dataText ); + + if( tdb_store( tctx, key, data, 0 ) < 0 ) + MsgPrint( MSG_DEBUG, "tdb_store failed\n" ); + + tdb_close( tctx ); + MsgPrint( MSG_NORMAL, "Recorded game stat from %s\n", address ); +} + +#endif |