another snapshot: invites are received but don't create game correctly yet

This commit is contained in:
Eric House 2013-12-12 07:58:02 -08:00
parent 5f97f8197c
commit 78d8c0398f
5 changed files with 186 additions and 30 deletions

View file

@ -1472,8 +1472,11 @@ handle_invite_button( GtkWidget* XP_UNUSED(widget), GtkGameGlobals* globals )
int port = atoi( portstr );
XP_LOGF( "need to invite using number %s and port %d", phone, port );
XP_ASSERT( 0 != port );
linux_sms2_invite( globals->cGlobals.params,
globals->cGlobals.gi, phone, port );
const CurGameInfo* gi = globals->cGlobals.gi;
gchar gameName[64];
snprintf( gameName, VSIZE(gameName), "Game %ld", gi->gameID );
linux_sms2_invite( globals->cGlobals.params, gi, gameName, 1,
phone, port );
}
g_free( phone );
g_free( portstr );
@ -2496,12 +2499,16 @@ drop_msg_toggle( GtkWidget* toggle, GtkGameGlobals* globals )
/* } */
static void
initGlobalsNoDraw( GtkGameGlobals* globals, LaunchParams* params )
initGlobalsNoDraw( GtkGameGlobals* globals, LaunchParams* params,
CurGameInfo* gi )
{
memset( globals, 0, sizeof(*globals) );
globals->cGlobals.gi = &globals->gi;
gi_copy( MPPARM(params->mpool) globals->cGlobals.gi, &params->pgi );
if ( !gi ) {
gi = &params->pgi;
}
gi_copy( MPPARM(params->mpool) globals->cGlobals.gi, gi );
globals->cGlobals.params = params;
globals->cGlobals.lastNTilesToUse = MAX_TRAY_TILES;
@ -2538,7 +2545,7 @@ initGlobalsNoDraw( GtkGameGlobals* globals, LaunchParams* params )
}
void
initGlobals( GtkGameGlobals* globals, LaunchParams* params )
initGlobals( GtkGameGlobals* globals, LaunchParams* params, CurGameInfo* gi )
{
short width, height;
GtkWidget* window;
@ -2551,7 +2558,7 @@ initGlobals( GtkGameGlobals* globals, LaunchParams* params )
GtkWidget* dropCheck;
#endif
initGlobalsNoDraw( globals, params );
initGlobalsNoDraw( globals, params, gi );
globals->window = window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
if ( !!params->fileName ) {
@ -2666,7 +2673,7 @@ loadGameNoDraw( GtkGameGlobals* globals, LaunchParams* params,
{
LOG_FUNC();
sqlite3* pDb = params->pDb;
initGlobalsNoDraw( globals, params );
initGlobalsNoDraw( globals, params, NULL );
TransportProcs procs;
setTransportProcs( &procs, globals );

View file

@ -168,7 +168,8 @@ typedef struct GtkGameGlobals {
#define GTK_BOTTOM_MARGIN GTK_TOP_MARGIN
#define GTK_RIGHT_MARGIN GTK_BOARD_LEFT_MARGIN
void initGlobals( GtkGameGlobals* globals, LaunchParams* params );
void initGlobals( GtkGameGlobals* globals, LaunchParams* params,
CurGameInfo* gi );
void freeGlobals( GtkGameGlobals* globals );
XP_Bool makeNewGame( GtkGameGlobals* globals );
XP_Bool loadGameNoDraw( GtkGameGlobals* globals, LaunchParams* params,

View file

@ -20,6 +20,7 @@
#ifdef PLATFORM_GTK
#include "strutils.h"
#include "main.h"
#include "gtkmain.h"
#include "gamesdb.h"
@ -211,7 +212,7 @@ handle_newgame_button( GtkWidget* XP_UNUSED(widget), void* closure )
XP_LOGF( "%s called", __func__ );
GtkGameGlobals* globals = malloc( sizeof(*globals) );
apg->params->needsNewGame = XP_FALSE;
initGlobals( globals, apg->params );
initGlobals( globals, apg->params, NULL );
if ( !makeNewGame( globals ) ) {
freeGlobals( globals );
} else {
@ -231,7 +232,7 @@ handle_open_button( GtkWidget* XP_UNUSED(widget), void* closure )
if ( -1 != selRow && !gameIsOpen( apg, selRow ) ) {
apg->params->needsNewGame = XP_FALSE;
GtkGameGlobals* globals = malloc( sizeof(*globals) );
initGlobals( globals, apg->params );
initGlobals( globals, apg->params, NULL );
globals->cGlobals.pDb = apg->params->pDb;
globals->cGlobals.selRow = selRow;
recordOpened( apg, globals );
@ -506,6 +507,38 @@ gtkNoticeRcvd( void* closure )
(void)g_idle_add( requestMsgs, apg );
}
static void
gtkInviteReceived( void* closure, const XP_UCHAR* gameName, XP_U32 gameID,
XP_U16 dictLang, const XP_UCHAR* dictName, XP_U16 nPlayers,
XP_U16 nHere )
{
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
LaunchParams* params = apg->params;
XP_LOGF( "%s(gameName=%s, dictName=%s, nPlayers=%d, nHere=%d)", __func__,
gameName, dictName, nPlayers, nHere );
CurGameInfo gi = {0};
gi_copy( MPPARM(params->mpool) &gi, &params->pgi );
gi.gameID = gameID;
gi.nPlayers = nPlayers;
gi.dictLang = dictLang;
replaceStringIfDifferent( params->mpool, &gi.dictName, dictName );
GtkGameGlobals* globals = malloc( sizeof(*globals) );
params->needsNewGame = XP_FALSE;
initGlobals( globals, params, &gi );
/* if ( !makeNewGame( globals ) ) { */
/* } */
GtkWidget* gameWindow = globals->window;
globals->cGlobals.pDb = apg->params->pDb;
globals->cGlobals.selRow = -1;
recordOpened( apg, globals );
gtk_widget_show( gameWindow );
}
static gboolean
keepalive_timer( gpointer data )
{
@ -619,8 +652,9 @@ gtkmain( LaunchParams* params )
if ( !!phone ) {
SMSProcs smsProcs = {
.socketChanged = gtkSocketChanged,
.inviteReceived = gtkInviteReceived,
};
linux_sms2_init( params, &smsProcs, &apg );
linux_sms2_init( params, phone, &smsProcs, &apg );
} else {
XP_LOGF( "not activating SMS: I don't have a phone" );
}

View file

@ -269,11 +269,17 @@ typedef struct _LinSMS2Data {
XP_UCHAR myQueue[256];
XP_U16 port;
FILE* lock;
XP_U16 count;
const gchar* myPhone;
const SMSProcs* procs;
void* procClosure;
} LinSMS2Data;
typedef enum { NONE, INVITE, DATA, DEATH, ACK, } SMS_CMD;
#define SMS_PROTO_VERSION 0
static LinSMS2Data* getStorage( LaunchParams* params );
static void
@ -294,10 +300,62 @@ unlock_queue2( LinSMS2Data* data )
data->lock = NULL;
}
XP_S16
linux_sms_send2( LaunchParams* params, const XP_U8* buf, XP_U16 buflen,
const XP_UCHAR* phone, XP_U16 port )
{
XP_S16 nSent = -1;
LinSMS2Data* data = getStorage( params );
XP_ASSERT( !!data );
char path[256];
lock_queue2( data );
#ifdef DEBUG
gchar* str64 = g_base64_encode( buf, buflen );
#endif
makeQueuePath( phone, port, path, sizeof(path) );
g_mkdir_with_parents( path, 0777 ); /* just in case */
int len = strlen( path );
snprintf( &path[len], sizeof(path)-len, "/%d", ++data->count );
XP_UCHAR sms[buflen*2]; /* more like (buflen*4/3) */
XP_U16 smslen = sizeof(sms);
binToSms( sms, &smslen, buf, buflen );
XP_ASSERT( smslen == strlen(sms) );
#ifdef DEBUG
XP_ASSERT( !strcmp( str64, sms ) );
g_free( str64 );
XP_U8 testout[buflen];
XP_U16 lenout = sizeof( testout );
XP_ASSERT( smsToBin( testout, &lenout, sms, smslen ) );
XP_ASSERT( lenout == buflen );
XP_ASSERT( XP_MEMCMP( testout, buf, smslen ) );
#endif
FILE* fp = fopen( path, "w" );
XP_ASSERT( !!fp );
(void)fprintf( fp, "from: %s\n", data->myPhone );
(void)fprintf( fp, "%s\n", sms );
fclose( fp );
sync();
unlock_queue2( data );
nSent = buflen;
return nSent;
} /* linux_sms_send */
static XP_S16
decodeAndDelete2( LinSMS2Data* data, const gchar* name,
XP_U8* buf, XP_U16 buflen )
{
LOG_FUNC();
XP_S16 nRead = -1;
char path[256];
snprintf( path, sizeof(path), "%s/%s", data->myQueue, name );
@ -335,15 +393,52 @@ decodeAndDelete2( LinSMS2Data* data, const gchar* name,
g_free( contents );
LOG_RETURNF( "%d", nRead );
return nRead;
} /* decodeAndDelete */
} /* decodeAndDelete2 */
static void
parseAndDispatch( LinSMS2Data* data, uint8_t* buf, int len )
dispatch_invite( LinSMS2Data* data, XP_U16 XP_UNUSED(proto), XWStreamCtxt* stream )
{
XP_USE( data );
XP_USE( buf );
XP_USE( len );
XP_UCHAR gameName[256];
XP_UCHAR dictName[256];
XP_U32 gameID = stream_getU32( stream );
stringFromStreamHere( stream, gameName, VSIZE(gameName) );
XP_U32 dictLang = stream_getU32( stream );
stringFromStreamHere( stream, dictName, VSIZE(dictName) );
XP_U8 nMissing = stream_getU8( stream );
XP_U8 nPlayers = stream_getU8( stream );
(*data->procs->inviteReceived)( data->procClosure, gameName, gameID,
dictLang, dictName, nPlayers, nMissing );
}
static void
parseAndDispatch( LaunchParams* params, uint8_t* buf, int len )
{
LinSMS2Data* data = getStorage( params );
XWStreamCtxt* stream = mem_stream_make( MPPARM(params->mpool)
params->vtMgr,
NULL, CHANNEL_NONE, NULL );
stream_putBytes( stream, buf, len );
XP_U8 proto = stream_getU8( stream );
XP_ASSERT( SMS_PROTO_VERSION == proto );
XP_U8 cmd = stream_getU8( stream );
switch( cmd ) {
case INVITE:
dispatch_invite( data, proto, stream );
break;
case DATA:
case DEATH:
case ACK:
break;
default:
XP_ASSERT( 0 );
}
stream_destroy( stream );
}
static void
@ -388,21 +483,22 @@ sms2_receive( void* closure, int socket )
unlock_queue2( data );
if ( 0 < nRead ) {
parseAndDispatch( data, buf, nRead );
parseAndDispatch( params, buf, nRead );
}
}
void
linux_sms2_init( LaunchParams* params, const SMSProcs* procs,
void* procClosure )
linux_sms2_init( LaunchParams* params, const gchar* phone,
const SMSProcs* procs, void* procClosure )
{
XP_ASSERT( !!params->connInfo.sms.phone );
XP_ASSERT( !!phone );
LinSMS2Data* data = getStorage( params );
XP_ASSERT( !!data );
data->myPhone = phone;
data->procs = procs;
data->procClosure = procClosure;
makeQueuePath( params->connInfo.sms.phone, params->connInfo.sms.port,
makeQueuePath( phone, params->connInfo.sms.port,
data->myQueue, sizeof(data->myQueue) );
data->port = params->connInfo.sms.port;
@ -416,14 +512,27 @@ linux_sms2_init( LaunchParams* params, const SMSProcs* procs,
} /* linux_sms2_init */
void
linux_sms2_invite( LaunchParams* params, const CurGameInfo* info, const gchar* phone,
linux_sms2_invite( LaunchParams* params, const CurGameInfo* gi,
const gchar* gameName, XP_U16 nMissing, const gchar* phone,
int port )
{
LOG_FUNC();
XP_USE( params );
XP_USE( info );
XP_USE( phone );
XP_USE( port );
XWStreamCtxt* stream;
stream = mem_stream_make( MPPARM(params->mpool) params->vtMgr,
NULL, CHANNEL_NONE, NULL );
stream_putU8( stream, SMS_PROTO_VERSION );
stream_putU8( stream, INVITE );
stream_putU32( stream, gi->gameID );
stringToStream( stream, gameName );
stream_putU32( stream, gi->dictLang );
stringToStream( stream, gi->dictName );
stream_putU8( stream, nMissing );
stream_putU8( stream, gi->nPlayers );
linux_sms_send2( params, stream_getPtr( stream ),
stream_getSize( stream ), phone, port );
stream_destroy( stream );
}
void

View file

@ -33,6 +33,10 @@ XP_S16 linux_sms_receive( CommonGlobals* globals, int sock,
XP_U8* buf, XP_U16 buflen, CommsAddrRec* addr );
typedef struct _SMSProcs {
void (*inviteReceived)( void* closure, const XP_UCHAR* gameName,
XP_U32 gameID, XP_U16 dictLang,
const XP_UCHAR* dictName, XP_U16 nPlayers,
XP_U16 nHere );
void (*msgReceived)( void* closure, const XP_U8* buf, XP_U16 len );
void (*msgNoticeReceived)( void* closure );
void (*devIDReceived)( void* closure, const XP_UCHAR* devID,
@ -44,10 +48,11 @@ typedef struct _SMSProcs {
} SMSProcs;
void linux_sms2_init( LaunchParams* params, const SMSProcs* procs,
void* procClosure );
void linux_sms2_invite( LaunchParams* params, const CurGameInfo* info, const gchar* phone,
int port );
void linux_sms2_init( LaunchParams* params, const gchar* phone,
const SMSProcs* procs, void* procClosure );
void linux_sms2_invite( LaunchParams* params, const CurGameInfo* info,
const gchar* gameName, XP_U16 nMissing,
const gchar* phone, int port );
void linux_sms2_cleanup( LaunchParams* params );
#endif /* XWFEATURE_SMS */