fetch relayIDs from db

This commit is contained in:
Eric House 2017-10-21 16:05:54 -07:00
parent a65af79953
commit 47a048d553
3 changed files with 90 additions and 0 deletions

View file

@ -311,6 +311,40 @@ listGames( sqlite3* pDb )
return list;
}
GHashTable*
getRowsToRelayIDsMap( sqlite3* pDb )
{
GHashTable* table = g_hash_table_new( g_int64_hash, g_int64_equal );
sqlite3_stmt *ppStmt;
int result = sqlite3_prepare_v2( pDb, "SELECT rowid, relayid FROM games",
-1, &ppStmt, NULL );
assertPrintResult( pDb, result, SQLITE_OK );
XP_USE( result );
while ( NULL != ppStmt ) {
switch( sqlite3_step( ppStmt ) ) {
case SQLITE_ROW: /* have data */
{
sqlite3_int64* key = g_malloc( sizeof( *key ) );
*key = sqlite3_column_int64( ppStmt, 0 );
XP_UCHAR relayID[32];
getColumnText( ppStmt, 1, relayID, VSIZE(relayID) );
gpointer value = g_strdup( relayID );
g_hash_table_insert( table, key, value );
}
break;
case SQLITE_DONE:
sqlite3_finalize( ppStmt );
ppStmt = NULL;
break;
default:
XP_ASSERT( 0 );
break;
}
}
return table;
}
XP_Bool
getGameInfo( sqlite3* pDb, sqlite3_int64 rowid, GameInfo* gib )
{

View file

@ -56,6 +56,8 @@ void summarize( CommonGlobals* cGlobals );
/* Return GSList whose data is (ptrs to) rowids */
GSList* listGames( sqlite3* dbp );
GHashTable *getRowsToRelayIDsMap(sqlite3* dbp);
XP_Bool getGameInfo( sqlite3* dbp, sqlite3_int64 rowid, GameInfo* gib );
void getRowsForGameID( sqlite3* dbp, XP_U32 gameID, sqlite3_int64* rowids,
int* nRowIDs );

View file

@ -27,9 +27,15 @@
#include "relaycon.h"
#include "linuxmain.h"
#include "comtypes.h"
#include "gamesdb.h"
#define MAX_MOVE_CHECK_SECS ((XP_U16)(60 * 60 * 24))
typedef struct _RelayConStorage {
pthread_t mainThread;
guint moveCheckerID;
XP_U16 nextMoveCheckSecs;
int socket;
RelayConnProcs procs;
void* procsClosure;
@ -48,6 +54,7 @@ static RelayConStorage* getStorage( LaunchParams* params );
static XP_U32 hostNameToIP( const XP_UCHAR* name );
static gboolean relaycon_receive( GIOChannel *source, GIOCondition condition,
gpointer data );
static void scheule_next_check( RelayConStorage* storage );
static ssize_t sendIt( RelayConStorage* storage, const XP_U8* msgbuf, XP_U16 len );
static size_t addVLIStr( XP_U8* buf, size_t len, const XP_UCHAR* str );
static void getNetString( const XP_U8** ptr, XP_U16 len, XP_UCHAR* buf );
@ -216,6 +223,10 @@ relaycon_init( LaunchParams* params, const RelayConnProcs* procs,
storage->params = params;
storage->proto = XWPDEV_PROTO_VERSION_1;
if ( params->useHTTP ) {
scheule_next_check( storage );
}
}
/* Send existing relay-assigned rDevID to relay, or empty string if we have
@ -643,6 +654,49 @@ post( RelayConStorage* storage, const XP_U8* msgbuf, XP_U16 len )
return len;
}
static gboolean
checkForMoves( gpointer user_data )
{
LOG_FUNC();
RelayConStorage* storage = (RelayConStorage*)user_data;
XP_ASSERT( onMainThread(storage) );
sqlite3* dbp = storage->params->pDb;
GHashTable* map = getRowsToRelayIDsMap( dbp );
GList* ids = g_hash_table_get_values( map );
for ( GList* iter = ids; !!iter; iter = iter->next ) {
gpointer data = iter->data;
XP_LOGF( "checkForMoves: got id: %s", (char*)data );
}
g_list_free( ids );
g_hash_table_destroy( map );
scheule_next_check( storage );
return FALSE;
}
static void
scheule_next_check( RelayConStorage* storage )
{
XP_ASSERT( onMainThread(storage) );
if ( storage->moveCheckerID != 0 ) {
g_source_remove( storage->moveCheckerID );
storage->moveCheckerID = 0;
}
storage->nextMoveCheckSecs *= 2;
if ( storage->nextMoveCheckSecs > MAX_MOVE_CHECK_SECS ) {
storage->nextMoveCheckSecs = MAX_MOVE_CHECK_SECS;
} else if ( storage->nextMoveCheckSecs == 0 ) {
storage->nextMoveCheckSecs = 1;
}
storage->moveCheckerID = g_timeout_add( 1000 * storage->nextMoveCheckSecs,
checkForMoves, storage );
XP_ASSERT( storage->moveCheckerID != 0 );
}
static ssize_t
sendIt( RelayConStorage* storage, const XP_U8* msgbuf, XP_U16 len )
{