mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-30 08:34:16 +01:00
add client version and a desc string to devices DB and to reg info sent to relay
This commit is contained in:
parent
a8d67830d8
commit
2157706dcd
6 changed files with 85 additions and 33 deletions
|
@ -114,6 +114,7 @@ DEFINES += -DXWFEATURE_CHANGEDICT
|
||||||
DEFINES += -DXWFEATURE_DEVID
|
DEFINES += -DXWFEATURE_DEVID
|
||||||
DEFINES += -DXWFEATURE_COMMSACK
|
DEFINES += -DXWFEATURE_COMMSACK
|
||||||
DEFINES += -DCOMMS_XPORT_FLAGSPROC
|
DEFINES += -DCOMMS_XPORT_FLAGSPROC
|
||||||
|
DEFINES += -DINITIAL_CLIENT_VERS=2
|
||||||
|
|
||||||
# MAX_ROWS controls STREAM_VERS_BIGBOARD and with it move hashing
|
# MAX_ROWS controls STREAM_VERS_BIGBOARD and with it move hashing
|
||||||
DEFINES += -DMAX_ROWS=32
|
DEFINES += -DMAX_ROWS=32
|
||||||
|
|
|
@ -48,7 +48,7 @@ static XP_U32 getNetLong( const XP_U8** ptr );
|
||||||
static int writeHeader( RelayConStorage* storage, XP_U8* dest, XWRelayReg cmd );
|
static int writeHeader( RelayConStorage* storage, XP_U8* dest, XWRelayReg cmd );
|
||||||
static bool readHeader( const XP_U8** buf, MsgHeader* header );
|
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 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
|
void
|
||||||
relaycon_init( LaunchParams* params, const RelayConnProcs* procs,
|
relaycon_init( LaunchParams* params, const RelayConnProcs* procs,
|
||||||
|
@ -73,7 +73,7 @@ void
|
||||||
relaycon_reg( LaunchParams* params, const XP_UCHAR* devID, DevIDType typ )
|
relaycon_reg( LaunchParams* params, const XP_UCHAR* devID, DevIDType typ )
|
||||||
{
|
{
|
||||||
LOG_FUNC();
|
LOG_FUNC();
|
||||||
XP_U8 tmpbuf[32];
|
XP_U8 tmpbuf[256];
|
||||||
int indx = 0;
|
int indx = 0;
|
||||||
|
|
||||||
RelayConStorage* storage = getStorage( params );
|
RelayConStorage* storage = getStorage( params );
|
||||||
|
@ -81,6 +81,9 @@ relaycon_reg( LaunchParams* params, const XP_UCHAR* devID, DevIDType typ )
|
||||||
indx += writeHeader( storage, tmpbuf, XWPDEV_REG );
|
indx += writeHeader( storage, tmpbuf, XWPDEV_REG );
|
||||||
tmpbuf[indx++] = typ;
|
tmpbuf[indx++] = typ;
|
||||||
indx += writeDevID( &tmpbuf[indx], sizeof(tmpbuf) - indx, devID );
|
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 );
|
sendIt( storage, tmpbuf, indx );
|
||||||
}
|
}
|
||||||
|
@ -230,7 +233,9 @@ relaycon_receive( void* closure, int socket )
|
||||||
(*storage->procs.msgNoticeReceived)( storage->procsClosure );
|
(*storage->procs.msgNoticeReceived)( storage->procsClosure );
|
||||||
break;
|
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_U16 len = getNetShort( &ptr );
|
||||||
XP_UCHAR buf[len+1];
|
XP_UCHAR buf[len+1];
|
||||||
getNetString( &ptr, len, buf );
|
getNetString( &ptr, len, buf );
|
||||||
|
@ -243,7 +248,7 @@ relaycon_receive( void* closure, int socket )
|
||||||
XP_LOGF( "got ack for packetID %ld", packetID );
|
XP_LOGF( "got ack for packetID %ld", packetID );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case XWPDEV_METAMSG: {
|
case XWPDEV_ALERT: {
|
||||||
XP_U16 len = getNetShort( &ptr );
|
XP_U16 len = getNetShort( &ptr );
|
||||||
unsigned char buf[len + 1];
|
unsigned char buf[len + 1];
|
||||||
memcpy( buf, ptr, len );
|
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 );
|
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
|
static XP_U16
|
||||||
getNetShort( const XP_U8** ptr )
|
getNetShort( const XP_U8** ptr )
|
||||||
{
|
{
|
||||||
|
|
|
@ -339,7 +339,8 @@ DBMgr::AllDevsAckd( const char* const connName )
|
||||||
// Return DevIDRelay for device, adding it to devices table IFF it's not
|
// Return DevIDRelay for device, adding it to devices table IFF it's not
|
||||||
// already there.
|
// already there.
|
||||||
DevIDRelay
|
DevIDRelay
|
||||||
DBMgr::RegisterDevice( const DevID* host )
|
DBMgr::RegisterDevice( const DevID* host, int clientVersion,
|
||||||
|
const char* const desc )
|
||||||
{
|
{
|
||||||
DevIDRelay devID;
|
DevIDRelay devID;
|
||||||
assert( host->m_devIDType != ID_TYPE_NONE );
|
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.
|
// If it's not present *and* of type ID_TYPE_RELAY, we can do nothing.
|
||||||
// Otherwise proceed.
|
// Otherwise proceed.
|
||||||
if ( DEVID_NONE != devID ) {
|
if ( DEVID_NONE != devID ) {
|
||||||
(void)updateDevice( devID, false );
|
(void)UpdateDevice( devID );
|
||||||
} else if ( ID_TYPE_RELAY < host->m_devIDType ) {
|
} else if ( ID_TYPE_RELAY < host->m_devIDType ) {
|
||||||
// loop until we're successful inserting the unique key. Ship with this
|
// loop until we're successful inserting the unique key. Ship with this
|
||||||
// coming from random, but test with increasing values initially to make
|
// coming from random, but test with increasing values initially to make
|
||||||
|
@ -365,15 +366,16 @@ DBMgr::RegisterDevice( const DevID* host )
|
||||||
} while ( DEVID_NONE == devID );
|
} while ( DEVID_NONE == devID );
|
||||||
|
|
||||||
const char* command = "INSERT INTO " DEVICES_TABLE
|
const char* command = "INSERT INTO " DEVICES_TABLE
|
||||||
" (id, devType, devid)"
|
" (id, devType, devid, clntVers, versDesc)"
|
||||||
" VALUES( $1, $2, $3 )";
|
" VALUES( $1, $2, $3, $4, $5 )";
|
||||||
int nParams = 3;
|
int nParams = 5;
|
||||||
char* paramValues[nParams];
|
char* paramValues[nParams];
|
||||||
char buf[512];
|
char buf[1024];
|
||||||
formatParams( paramValues, nParams,
|
formatParams( paramValues, nParams,
|
||||||
"%d"DELIM"%d"DELIM"%s",
|
"%d"DELIM"%d"DELIM"%s"DELIM"%d"DELIM"%s",
|
||||||
buf, sizeof(buf), devID, host->m_devIDType,
|
buf, sizeof(buf), devID, host->m_devIDType,
|
||||||
host->m_devIDString.c_str() );
|
host->m_devIDString.c_str(), clientVersion,
|
||||||
|
desc );
|
||||||
|
|
||||||
PGresult* result = PQexecParams( getThreadConn(), command,
|
PGresult* result = PQexecParams( getThreadConn(), command,
|
||||||
nParams, NULL,
|
nParams, NULL,
|
||||||
|
@ -391,8 +393,15 @@ DBMgr::RegisterDevice( const DevID* host )
|
||||||
return devID;
|
return devID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DevIDRelay
|
||||||
|
DBMgr::RegisterDevice( const DevID* host )
|
||||||
|
{
|
||||||
|
return RegisterDevice( host, 0, "" );
|
||||||
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
DBMgr::updateDevice( DevIDRelay relayID, bool check )
|
DBMgr::UpdateDevice( DevIDRelay relayID, int clientVersion,
|
||||||
|
const char* const desc, bool check )
|
||||||
{
|
{
|
||||||
bool exists = !check;
|
bool exists = !check;
|
||||||
if ( !exists ) {
|
if ( !exists ) {
|
||||||
|
@ -402,15 +411,24 @@ DBMgr::updateDevice( DevIDRelay relayID, bool check )
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( exists ) {
|
if ( exists ) {
|
||||||
const char* fmt =
|
|
||||||
"UPDATE " DEVICES_TABLE " SET mtime='now' WHERE id = %d";
|
|
||||||
string query;
|
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 );
|
execSql( query );
|
||||||
}
|
}
|
||||||
return exists;
|
return exists;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
DBMgr::UpdateDevice( DevIDRelay relayID )
|
||||||
|
{
|
||||||
|
return UpdateDevice( relayID, 0, NULL, false );
|
||||||
|
}
|
||||||
|
|
||||||
HostID
|
HostID
|
||||||
DBMgr::AddDevice( const char* connName, HostID curID, int clientVersion,
|
DBMgr::AddDevice( const char* connName, HostID curID, int clientVersion,
|
||||||
int nToAdd, unsigned short seed, const AddrInfo* addr,
|
int nToAdd, unsigned short seed, const AddrInfo* addr,
|
||||||
|
|
|
@ -81,7 +81,10 @@ class DBMgr {
|
||||||
bool AllDevsAckd( const char* const connName );
|
bool AllDevsAckd( const char* const connName );
|
||||||
|
|
||||||
DevIDRelay RegisterDevice( const DevID* host );
|
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,
|
HostID AddDevice( const char* const connName, HostID curID, int clientVersion,
|
||||||
int nToAdd, unsigned short seed, const AddrInfo* addr,
|
int nToAdd, unsigned short seed, const AddrInfo* addr,
|
||||||
|
@ -139,6 +142,7 @@ class DBMgr {
|
||||||
int byteaIndex, unsigned char* buf, size_t* buflen );
|
int byteaIndex, unsigned char* buf, size_t* buflen );
|
||||||
void storedMessagesImpl( string query, vector<DBMgr::MsgInfo>& msgs );
|
void storedMessagesImpl( string query, vector<DBMgr::MsgInfo>& msgs );
|
||||||
int CountStoredMessages( const char* const connName, int hid );
|
int CountStoredMessages( const char* const connName, int hid );
|
||||||
|
bool UpdateDevice( DevIDRelay relayID );
|
||||||
|
|
||||||
PGconn* getThreadConn( void );
|
PGconn* getThreadConn( void );
|
||||||
void clearThreadConn();
|
void clearThreadConn();
|
||||||
|
|
|
@ -252,7 +252,7 @@ getNetLong( const unsigned char** bufpp, const unsigned char* end,
|
||||||
*out = ntohl( tmp );
|
*out = ntohl( tmp );
|
||||||
}
|
}
|
||||||
return ok;
|
return ok;
|
||||||
} /* getNetShort */
|
} /* getNetLong */
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
getNetShort( const unsigned char** bufpp, const unsigned char* end,
|
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
|
static bool
|
||||||
getRelayDevID( const unsigned char** bufpp, const unsigned char* end,
|
getShortInitString( const unsigned char** bufpp, const unsigned char* end,
|
||||||
DevID& devID )
|
string& out )
|
||||||
{
|
{
|
||||||
bool success = false;
|
bool success;
|
||||||
unsigned short idLen;
|
uint16_t len;
|
||||||
if ( getNetShort( bufpp, end, &idLen ) ) {
|
success = getNetShort( bufpp, end, &len );
|
||||||
if ( end - *bufpp < idLen/* && ID_TYPE_ANON != typ*/ ) {
|
if ( success ) {
|
||||||
logf( XW_LOGERROR, "full devID not received" );
|
success = *bufpp + len <= end;
|
||||||
} else {
|
if ( success ) {
|
||||||
devID.m_devIDString.append( (const char*)*bufpp, idLen );
|
out.append( (const char*)*bufpp, len );
|
||||||
*bufpp += idLen;
|
*bufpp += len;
|
||||||
success = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
getRelayDevID( const unsigned char** bufpp, const unsigned char* end,
|
||||||
|
DevID& devID )
|
||||||
|
{
|
||||||
|
return getShortInitString( bufpp, end, devID.m_devIDString );
|
||||||
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
getHeader( const unsigned char** bufpp, const unsigned char* end,
|
getHeader( const unsigned char** bufpp, const unsigned char* end,
|
||||||
UDPHeader* header )
|
UDPHeader* header )
|
||||||
|
@ -1362,7 +1368,8 @@ addRegID( unsigned char* ptr, DevIDRelay relayID )
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
registerDevice( const DevID* devID, const AddrInfo* addr )
|
registerDevice( const DevID* devID, const AddrInfo* addr, int clientVers,
|
||||||
|
string devDesc )
|
||||||
{
|
{
|
||||||
DevIDRelay relayID;
|
DevIDRelay relayID;
|
||||||
DBMgr* dbMgr = DBMgr::Get();
|
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
|
if ( ID_TYPE_RELAY == devID->m_devIDType ) { // known to us; just update the time
|
||||||
relayID = devID->asRelayID();
|
relayID = devID->asRelayID();
|
||||||
if ( dbMgr->updateDevice( relayID, true ) ) {
|
if ( dbMgr->UpdateDevice( relayID, clientVers, devDesc.c_str(),
|
||||||
|
true ) ) {
|
||||||
int nMsgs = dbMgr->CountStoredMessages( relayID );
|
int nMsgs = dbMgr->CountStoredMessages( relayID );
|
||||||
if ( 0 < nMsgs ) {
|
if ( 0 < nMsgs ) {
|
||||||
send_havemsgs( addr );
|
send_havemsgs( addr );
|
||||||
|
@ -1383,7 +1391,7 @@ registerDevice( const DevID* devID, const AddrInfo* addr )
|
||||||
relayID = DBMgr::DEVID_NONE;
|
relayID = DBMgr::DEVID_NONE;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
relayID = dbMgr->RegisterDevice( devID );
|
relayID = dbMgr->RegisterDevice( devID, clientVers, devDesc.c_str() );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( DBMgr::DEVID_NONE != relayID ) {
|
if ( DBMgr::DEVID_NONE != relayID ) {
|
||||||
|
@ -1488,7 +1496,12 @@ handle_udp_packet( UdpThreadClosure* utc )
|
||||||
DevIDType typ = (DevIDType)*ptr++;
|
DevIDType typ = (DevIDType)*ptr++;
|
||||||
DevID devID( typ );
|
DevID devID( typ );
|
||||||
if ( getRelayDevID( &ptr, end, devID ) ) {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,6 +82,8 @@ EOF
|
||||||
CREATE TABLE devices (
|
CREATE TABLE devices (
|
||||||
id INTEGER UNIQUE PRIMARY KEY
|
id INTEGER UNIQUE PRIMARY KEY
|
||||||
,devType INTEGER
|
,devType INTEGER
|
||||||
|
,clntVers INTEGER
|
||||||
|
,versDesc TEXT
|
||||||
,devid TEXT
|
,devid TEXT
|
||||||
,ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
,ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
,mtime TIMESTAMP
|
,mtime TIMESTAMP
|
||||||
|
|
Loading…
Add table
Reference in a new issue