From 2157706dcd43ea23c42315baa8273d2a6167fb9d Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 20 Aug 2013 08:09:45 -0700 Subject: [PATCH] add client version and a desc string to devices DB and to reg info sent to relay --- xwords4/linux/Makefile | 1 + xwords4/linux/relaycon.c | 22 +++++++++++++++---- xwords4/relay/dbmgr.cpp | 42 +++++++++++++++++++++++++----------- xwords4/relay/dbmgr.h | 6 +++++- xwords4/relay/xwrelay.cpp | 45 +++++++++++++++++++++++++-------------- xwords4/relay/xwrelay.sh | 2 ++ 6 files changed, 85 insertions(+), 33 deletions(-) diff --git a/xwords4/linux/Makefile b/xwords4/linux/Makefile index 1c1d4fd3e..74f954b65 100644 --- a/xwords4/linux/Makefile +++ b/xwords4/linux/Makefile @@ -114,6 +114,7 @@ DEFINES += -DXWFEATURE_CHANGEDICT DEFINES += -DXWFEATURE_DEVID DEFINES += -DXWFEATURE_COMMSACK DEFINES += -DCOMMS_XPORT_FLAGSPROC +DEFINES += -DINITIAL_CLIENT_VERS=2 # MAX_ROWS controls STREAM_VERS_BIGBOARD and with it move hashing DEFINES += -DMAX_ROWS=32 diff --git a/xwords4/linux/relaycon.c b/xwords4/linux/relaycon.c index 18eedac9b..b9a50233c 100644 --- a/xwords4/linux/relaycon.c +++ b/xwords4/linux/relaycon.c @@ -48,7 +48,7 @@ static XP_U32 getNetLong( const XP_U8** ptr ); static int writeHeader( RelayConStorage* storage, XP_U8* dest, XWRelayReg cmd ); static bool readHeader( const XP_U8** buf, MsgHeader* header ); static size_t writeDevID( XP_U8* buf, size_t len, const XP_UCHAR* str ); - +static size_t writeShort( XP_U8* buf, size_t len, XP_U16 shrt ); void relaycon_init( LaunchParams* params, const RelayConnProcs* procs, @@ -73,7 +73,7 @@ void relaycon_reg( LaunchParams* params, const XP_UCHAR* devID, DevIDType typ ) { LOG_FUNC(); - XP_U8 tmpbuf[32]; + XP_U8 tmpbuf[256]; int indx = 0; RelayConStorage* storage = getStorage( params ); @@ -81,6 +81,9 @@ relaycon_reg( LaunchParams* params, const XP_UCHAR* devID, DevIDType typ ) indx += writeHeader( storage, tmpbuf, XWPDEV_REG ); tmpbuf[indx++] = typ; indx += writeDevID( &tmpbuf[indx], sizeof(tmpbuf) - indx, devID ); + indx += writeShort( &tmpbuf[indx], sizeof(tmpbuf) - indx, + INITIAL_CLIENT_VERS ); + indx += addStrWithLength( &tmpbuf[indx], tmpbuf + sizeof(tmpbuf), SVN_REV ); sendIt( storage, tmpbuf, indx ); } @@ -230,7 +233,9 @@ relaycon_receive( void* closure, int socket ) (*storage->procs.msgNoticeReceived)( storage->procsClosure ); break; } - case XWPDEV_ALERT: { + case XWPDEV_UNAVAIL: { + XP_U32 unavail = getNetLong( &ptr ); + XP_LOGF( "%s: unavail = %lu", __func__, unavail ); XP_U16 len = getNetShort( &ptr ); XP_UCHAR buf[len+1]; getNetString( &ptr, len, buf ); @@ -243,7 +248,7 @@ relaycon_receive( void* closure, int socket ) XP_LOGF( "got ack for packetID %ld", packetID ); break; } - case XWPDEV_METAMSG: { + case XWPDEV_ALERT: { XP_U16 len = getNetShort( &ptr ); unsigned char buf[len + 1]; memcpy( buf, ptr, len ); @@ -328,6 +333,15 @@ writeDevID( XP_U8* buf, size_t len, const XP_UCHAR* str ) return addStrWithLength( buf, buf + len, str ); } +static size_t +writeShort( XP_U8* buf, size_t len, XP_U16 shrt ) +{ + shrt = htons( shrt ); + assert( sizeof( shrt ) <= len ); + memcpy( buf, &shrt, sizeof(shrt) ); + return sizeof(shrt); +} + static XP_U16 getNetShort( const XP_U8** ptr ) { diff --git a/xwords4/relay/dbmgr.cpp b/xwords4/relay/dbmgr.cpp index 1c5ce67c3..f44e06446 100644 --- a/xwords4/relay/dbmgr.cpp +++ b/xwords4/relay/dbmgr.cpp @@ -339,7 +339,8 @@ DBMgr::AllDevsAckd( const char* const connName ) // Return DevIDRelay for device, adding it to devices table IFF it's not // already there. DevIDRelay -DBMgr::RegisterDevice( const DevID* host ) +DBMgr::RegisterDevice( const DevID* host, int clientVersion, + const char* const desc ) { DevIDRelay devID; assert( host->m_devIDType != ID_TYPE_NONE ); @@ -352,7 +353,7 @@ DBMgr::RegisterDevice( const DevID* host ) // If it's not present *and* of type ID_TYPE_RELAY, we can do nothing. // Otherwise proceed. if ( DEVID_NONE != devID ) { - (void)updateDevice( devID, false ); + (void)UpdateDevice( devID ); } else if ( ID_TYPE_RELAY < host->m_devIDType ) { // loop until we're successful inserting the unique key. Ship with this // coming from random, but test with increasing values initially to make @@ -365,15 +366,16 @@ DBMgr::RegisterDevice( const DevID* host ) } while ( DEVID_NONE == devID ); const char* command = "INSERT INTO " DEVICES_TABLE - " (id, devType, devid)" - " VALUES( $1, $2, $3 )"; - int nParams = 3; + " (id, devType, devid, clntVers, versDesc)" + " VALUES( $1, $2, $3, $4, $5 )"; + int nParams = 5; char* paramValues[nParams]; - char buf[512]; + char buf[1024]; formatParams( paramValues, nParams, - "%d"DELIM"%d"DELIM"%s", + "%d"DELIM"%d"DELIM"%s"DELIM"%d"DELIM"%s", buf, sizeof(buf), devID, host->m_devIDType, - host->m_devIDString.c_str() ); + host->m_devIDString.c_str(), clientVersion, + desc ); PGresult* result = PQexecParams( getThreadConn(), command, nParams, NULL, @@ -391,8 +393,15 @@ DBMgr::RegisterDevice( const DevID* host ) return devID; } +DevIDRelay +DBMgr::RegisterDevice( const DevID* host ) +{ + return RegisterDevice( host, 0, "" ); +} + bool -DBMgr::updateDevice( DevIDRelay relayID, bool check ) +DBMgr::UpdateDevice( DevIDRelay relayID, int clientVersion, + const char* const desc, bool check ) { bool exists = !check; if ( !exists ) { @@ -402,15 +411,24 @@ DBMgr::updateDevice( DevIDRelay relayID, bool check ) } if ( exists ) { - const char* fmt = - "UPDATE " DEVICES_TABLE " SET mtime='now' WHERE id = %d"; string query; - string_printf( query, fmt, relayID ); + string_printf( query, "UPDATE " DEVICES_TABLE " SET mtime='now'" ); + if ( NULL != desc && '\0' != desc[0] ) { + string_printf( query, ", clntVers=%d, versDesc='%s'", + clientVersion, desc ); + } + string_printf( query, " WHERE id = %d", relayID ); execSql( query ); } return exists; } +bool +DBMgr::UpdateDevice( DevIDRelay relayID ) +{ + return UpdateDevice( relayID, 0, NULL, false ); +} + HostID DBMgr::AddDevice( const char* connName, HostID curID, int clientVersion, int nToAdd, unsigned short seed, const AddrInfo* addr, diff --git a/xwords4/relay/dbmgr.h b/xwords4/relay/dbmgr.h index 692f507f8..33a31db09 100644 --- a/xwords4/relay/dbmgr.h +++ b/xwords4/relay/dbmgr.h @@ -81,7 +81,10 @@ class DBMgr { bool AllDevsAckd( const char* const connName ); DevIDRelay RegisterDevice( const DevID* host ); - bool updateDevice( DevIDRelay relayID, bool check ); + DevIDRelay RegisterDevice( const DevID* host, int clientVersion, + const char* const desc ); + bool UpdateDevice( DevIDRelay relayID, int clientVersion, + const char* const desc, bool check ); HostID AddDevice( const char* const connName, HostID curID, int clientVersion, int nToAdd, unsigned short seed, const AddrInfo* addr, @@ -139,6 +142,7 @@ class DBMgr { int byteaIndex, unsigned char* buf, size_t* buflen ); void storedMessagesImpl( string query, vector& msgs ); int CountStoredMessages( const char* const connName, int hid ); + bool UpdateDevice( DevIDRelay relayID ); PGconn* getThreadConn( void ); void clearThreadConn(); diff --git a/xwords4/relay/xwrelay.cpp b/xwords4/relay/xwrelay.cpp index f27a5740b..f54516120 100644 --- a/xwords4/relay/xwrelay.cpp +++ b/xwords4/relay/xwrelay.cpp @@ -252,7 +252,7 @@ getNetLong( const unsigned char** bufpp, const unsigned char* end, *out = ntohl( tmp ); } return ok; -} /* getNetShort */ +} /* getNetLong */ static bool getNetShort( const unsigned char** bufpp, const unsigned char* end, @@ -295,23 +295,29 @@ getNetString( const unsigned char** bufpp, const unsigned char* end, string& out } static bool -getRelayDevID( const unsigned char** bufpp, const unsigned char* end, - DevID& devID ) +getShortInitString( const unsigned char** bufpp, const unsigned char* end, + string& out ) { - bool success = false; - unsigned short idLen; - if ( getNetShort( bufpp, end, &idLen ) ) { - if ( end - *bufpp < idLen/* && ID_TYPE_ANON != typ*/ ) { - logf( XW_LOGERROR, "full devID not received" ); - } else { - devID.m_devIDString.append( (const char*)*bufpp, idLen ); - *bufpp += idLen; - success = true; + bool success; + uint16_t len; + success = getNetShort( bufpp, end, &len ); + if ( success ) { + success = *bufpp + len <= end; + if ( success ) { + out.append( (const char*)*bufpp, len ); + *bufpp += len; } } return success; } +static bool +getRelayDevID( const unsigned char** bufpp, const unsigned char* end, + DevID& devID ) +{ + return getShortInitString( bufpp, end, devID.m_devIDString ); +} + static bool getHeader( const unsigned char** bufpp, const unsigned char* end, UDPHeader* header ) @@ -1362,7 +1368,8 @@ addRegID( unsigned char* ptr, DevIDRelay relayID ) } static void -registerDevice( const DevID* devID, const AddrInfo* addr ) +registerDevice( const DevID* devID, const AddrInfo* addr, int clientVers, + string devDesc ) { DevIDRelay relayID; DBMgr* dbMgr = DBMgr::Get(); @@ -1371,7 +1378,8 @@ registerDevice( const DevID* devID, const AddrInfo* addr ) if ( ID_TYPE_RELAY == devID->m_devIDType ) { // known to us; just update the time relayID = devID->asRelayID(); - if ( dbMgr->updateDevice( relayID, true ) ) { + if ( dbMgr->UpdateDevice( relayID, clientVers, devDesc.c_str(), + true ) ) { int nMsgs = dbMgr->CountStoredMessages( relayID ); if ( 0 < nMsgs ) { send_havemsgs( addr ); @@ -1383,7 +1391,7 @@ registerDevice( const DevID* devID, const AddrInfo* addr ) relayID = DBMgr::DEVID_NONE; } } else { - relayID = dbMgr->RegisterDevice( devID ); + relayID = dbMgr->RegisterDevice( devID, clientVers, devDesc.c_str() ); } if ( DBMgr::DEVID_NONE != relayID ) { @@ -1488,7 +1496,12 @@ handle_udp_packet( UdpThreadClosure* utc ) DevIDType typ = (DevIDType)*ptr++; DevID devID( typ ); if ( getRelayDevID( &ptr, end, devID ) ) { - registerDevice( &devID, utc->addr() ); + uint16_t clientVers; + string devDesc; + if ( getNetShort( &ptr, end, &clientVers ) + && getShortInitString( &ptr, end, devDesc ) ) { + registerDevice( &devID, utc->addr(), clientVers, devDesc ); + } } break; } diff --git a/xwords4/relay/xwrelay.sh b/xwords4/relay/xwrelay.sh index af28d46ca..169a59a5c 100755 --- a/xwords4/relay/xwrelay.sh +++ b/xwords4/relay/xwrelay.sh @@ -82,6 +82,8 @@ EOF CREATE TABLE devices ( id INTEGER UNIQUE PRIMARY KEY ,devType INTEGER +,clntVers INTEGER +,versDesc TEXT ,devid TEXT ,ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,mtime TIMESTAMP