From 47a048d553352306f0d8c3febe95cab77d0e9670 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 21 Oct 2017 16:05:54 -0700 Subject: [PATCH] fetch relayIDs from db --- xwords4/linux/gamesdb.c | 34 +++++++++++++++++++++++++ xwords4/linux/gamesdb.h | 2 ++ xwords4/linux/relaycon.c | 54 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/xwords4/linux/gamesdb.c b/xwords4/linux/gamesdb.c index 9e623239f..33f27c2db 100644 --- a/xwords4/linux/gamesdb.c +++ b/xwords4/linux/gamesdb.c @@ -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 ) { diff --git a/xwords4/linux/gamesdb.h b/xwords4/linux/gamesdb.h index f8ac3316a..214f324f9 100644 --- a/xwords4/linux/gamesdb.h +++ b/xwords4/linux/gamesdb.h @@ -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 ); diff --git a/xwords4/linux/relaycon.c b/xwords4/linux/relaycon.c index 491d1a3af..2d011277e 100644 --- a/xwords4/linux/relaycon.c +++ b/xwords4/linux/relaycon.c @@ -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 ) {