mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-01 19:57:11 +01:00
snapshot: reg works from Android and Linux
This commit is contained in:
parent
7200833d27
commit
47f06d4cb3
7 changed files with 140 additions and 70 deletions
|
@ -388,72 +388,72 @@ public class MQTTUtils extends Thread
|
|||
clearInstance();
|
||||
}
|
||||
|
||||
registerOnce();
|
||||
// registerOnce();
|
||||
}
|
||||
|
||||
private void registerOnce()
|
||||
{
|
||||
if ( 0 == sNextReg ) {
|
||||
sNextReg = DBUtils.getLongFor( mContext, KEY_NEXT_REG, 1 );
|
||||
sLastRev = DBUtils.getStringFor( mContext, KEY_LAST_WRITE, "" );
|
||||
}
|
||||
long now = Utils.getCurSeconds();
|
||||
Log.d( TAG, "registerOnce(): now: %d; nextReg: %d", now, sNextReg );
|
||||
String revString = BuildConfig.GIT_REV + ':' + BuildConfig.VARIANT_NAME;
|
||||
if ( now > sNextReg || ! revString.equals(sLastRev) ) {
|
||||
try {
|
||||
JSONObject params = new JSONObject();
|
||||
params.put( "devid", mDevID );
|
||||
params.put( "gitrev", BuildConfig.GIT_REV );
|
||||
params.put( "os", Build.MODEL );
|
||||
// PENDING remove me in favor of SDK_INT
|
||||
params.put( "vers", Build.VERSION.RELEASE );
|
||||
params.put( "versI", Build.VERSION.SDK_INT );
|
||||
params.put( "vrntCode", BuildConfig.VARIANT_CODE );
|
||||
params.put( "vrntName", BuildConfig.VARIANT_NAME );
|
||||
if ( BuildConfig.DEBUG ) {
|
||||
params.put( "dbg", true );
|
||||
}
|
||||
params.put( "myNow", now );
|
||||
params.put( "loc", LocUtils.getCurLocale( mContext ) );
|
||||
params.put( "tmpKey", getTmpKey(mContext) );
|
||||
params.put( "frstV", Utils.getFirstVersion( mContext ) );
|
||||
// private void registerOnce()
|
||||
// {
|
||||
// if ( 0 == sNextReg ) {
|
||||
// sNextReg = DBUtils.getLongFor( mContext, KEY_NEXT_REG, 1 );
|
||||
// sLastRev = DBUtils.getStringFor( mContext, KEY_LAST_WRITE, "" );
|
||||
// }
|
||||
// long now = Utils.getCurSeconds();
|
||||
// Log.d( TAG, "registerOnce(): now: %d; nextReg: %d", now, sNextReg );
|
||||
// String revString = BuildConfig.GIT_REV + ':' + BuildConfig.VARIANT_NAME;
|
||||
// if ( now > sNextReg || ! revString.equals(sLastRev) ) {
|
||||
// try {
|
||||
// JSONObject params = new JSONObject();
|
||||
// params.put( "devid", mDevID );
|
||||
// params.put( "gitrev", BuildConfig.GIT_REV );
|
||||
// params.put( "os", Build.MODEL );
|
||||
// // PENDING remove me in favor of SDK_INT
|
||||
// params.put( "vers", Build.VERSION.RELEASE );
|
||||
// params.put( "versI", Build.VERSION.SDK_INT );
|
||||
// params.put( "vrntCode", BuildConfig.VARIANT_CODE );
|
||||
// params.put( "vrntName", BuildConfig.VARIANT_NAME );
|
||||
// if ( BuildConfig.DEBUG ) {
|
||||
// params.put( "dbg", true );
|
||||
// }
|
||||
// params.put( "myNow", now );
|
||||
// params.put( "loc", LocUtils.getCurLocale( mContext ) );
|
||||
// params.put( "tmpKey", getTmpKey(mContext) );
|
||||
// params.put( "frstV", Utils.getFirstVersion( mContext ) );
|
||||
|
||||
Log.d( TAG, "registerOnce(): sending %s", params );
|
||||
HttpURLConnection conn
|
||||
= NetUtils.makeHttpMQTTConn( mContext, "register" );
|
||||
String resStr = NetUtils.runConn( conn, params, true );
|
||||
if ( null != resStr ) {
|
||||
JSONObject response = new JSONObject( resStr );
|
||||
Log.d( TAG, "registerOnce(): got %s", response );
|
||||
// Log.d( TAG, "registerOnce(): sending %s", params );
|
||||
// HttpURLConnection conn
|
||||
// = NetUtils.makeHttpMQTTConn( mContext, "register" );
|
||||
// String resStr = NetUtils.runConn( conn, params, true );
|
||||
// if ( null != resStr ) {
|
||||
// JSONObject response = new JSONObject( resStr );
|
||||
// Log.d( TAG, "registerOnce(): got %s", response );
|
||||
|
||||
if ( response.optBoolean( "success", true ) ) {
|
||||
long atNext = response.optLong( "atNext", 0 );
|
||||
if ( 0 < atNext ) {
|
||||
DBUtils.setLongFor( mContext, KEY_NEXT_REG, atNext );
|
||||
sNextReg = atNext;
|
||||
DBUtils.setStringFor( mContext, KEY_LAST_WRITE, revString );
|
||||
sLastRev = revString;
|
||||
}
|
||||
// if ( response.optBoolean( "success", true ) ) {
|
||||
// long atNext = response.optLong( "atNext", 0 );
|
||||
// if ( 0 < atNext ) {
|
||||
// DBUtils.setLongFor( mContext, KEY_NEXT_REG, atNext );
|
||||
// sNextReg = atNext;
|
||||
// DBUtils.setStringFor( mContext, KEY_LAST_WRITE, revString );
|
||||
// sLastRev = revString;
|
||||
// }
|
||||
|
||||
String dupID = response.optString( "dupID", "" );
|
||||
if ( dupID.equals( mDevID ) ) {
|
||||
Log.e( TAG, "********** %s bad; need new devID!!! **********", dupID );
|
||||
XwJNI.dvc_resetMQTTDevID();
|
||||
// Force a reconnect asap
|
||||
DBUtils.setLongFor( mContext, KEY_NEXT_REG, 0 );
|
||||
sNextReg = 0;
|
||||
clearInstance();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log.e( TAG, "registerOnce(): null back from runConn()" );
|
||||
}
|
||||
} catch ( JSONException je ) {
|
||||
Log.e( TAG, "registerOnce() ex: %s", je );
|
||||
}
|
||||
}
|
||||
}
|
||||
// String dupID = response.optString( "dupID", "" );
|
||||
// if ( dupID.equals( mDevID ) ) {
|
||||
// Log.e( TAG, "********** %s bad; need new devID!!! **********", dupID );
|
||||
// XwJNI.dvc_resetMQTTDevID();
|
||||
// // Force a reconnect asap
|
||||
// DBUtils.setLongFor( mContext, KEY_NEXT_REG, 0 );
|
||||
// sNextReg = 0;
|
||||
// clearInstance();
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// Log.e( TAG, "registerOnce(): null back from runConn()" );
|
||||
// }
|
||||
// } catch ( JSONException je ) {
|
||||
// Log.e( TAG, "registerOnce() ex: %s", je );
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
private static int sTmpKey;
|
||||
private static int getTmpKey( Context context )
|
||||
|
|
|
@ -22,10 +22,14 @@ package org.eehouse.android.xw4.jni;
|
|||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import org.eehouse.android.xw4.Assert;
|
||||
import org.eehouse.android.xw4.BuildConfig;
|
||||
import org.eehouse.android.xw4.Channels;
|
||||
|
@ -339,6 +343,25 @@ public class DUtilCtxt {
|
|||
NetUtils.sendViaWeb( m_context, api, jsonParams );
|
||||
}
|
||||
|
||||
public String getRegValues()
|
||||
{
|
||||
String result;
|
||||
try {
|
||||
JSONObject params = new JSONObject();
|
||||
params.put( "os", Build.MODEL );
|
||||
params.put( "vers", Build.VERSION.RELEASE );
|
||||
params.put( "versI", Build.VERSION.SDK_INT );
|
||||
params.put( "vrntCode", BuildConfig.VARIANT_CODE );
|
||||
params.put( "vrntName", BuildConfig.VARIANT_NAME );
|
||||
params.put( "loc", LocUtils.getCurLocale( m_context ) );
|
||||
result = params.toString();
|
||||
} catch ( JSONException je ) {
|
||||
Log.e( TAG, "getRegValues() ex: %s", je );
|
||||
result = "{}";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void onInviteReceived( NetLaunchInfo nli )
|
||||
{
|
||||
// Log.d( TAG, "onInviteReceived(%s)", nli );
|
||||
|
|
|
@ -50,6 +50,7 @@ LOCAL_DEFINES += \
|
|||
-D__LITTLE_ENDIAN \
|
||||
-DMQTT_DEV_TOPICS \
|
||||
-DMQTT_GAMEID_TOPICS \
|
||||
-DGITREV_SHORT="\"$(shell git rev-parse --short HEAD)\"" \
|
||||
|
||||
# XWFEATURE_RAISETILE: first, fix to not use timer
|
||||
# -DXWFEATURE_RAISETILE \
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <jni.h>
|
||||
|
||||
#include "comtypes.h"
|
||||
#include "device.h"
|
||||
#include "utilwrapper.h"
|
||||
#include "anddict.h"
|
||||
#include "andutils.h"
|
||||
|
@ -983,6 +984,20 @@ and_dutil_sendViaWeb( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* api,
|
|||
DUTIL_CBK_TAIL();
|
||||
}
|
||||
|
||||
static cJSON*
|
||||
and_dutil_getRegValues( XW_DUtilCtxt* duc, XWEnv xwe )
|
||||
{
|
||||
cJSON* result = NULL;
|
||||
DUTIL_CBK_HEADER( "getRegValues", "()Ljava/lang/String;" );
|
||||
jstring jresult = (*env)->CallObjectMethod( env, dutil->jdutil, mid );
|
||||
const char* jchars = (*env)->GetStringUTFChars( env, jresult, NULL );
|
||||
result = cJSON_Parse( jchars );
|
||||
(*env)->ReleaseStringUTFChars( env, jresult, jchars );
|
||||
deleteLocalRef( env, jresult );
|
||||
DUTIL_CBK_TAIL();
|
||||
return result;
|
||||
}
|
||||
|
||||
XW_UtilCtxt*
|
||||
makeUtil( MPFORMAL JNIEnv* env,
|
||||
#ifdef MAP_THREAD_TO_ENV
|
||||
|
@ -1094,7 +1109,8 @@ makeDUtil( MPFORMAL JNIEnv* env,
|
|||
void* closure )
|
||||
{
|
||||
AndDUtil* dutil = (AndDUtil*)XP_CALLOC( mpool, sizeof(*dutil) );
|
||||
dutil_super_init( MPPARM(mpool) &dutil->dutil );
|
||||
XW_DUtilCtxt* super = &dutil->dutil;
|
||||
dutil_super_init( MPPARM(mpool) super );
|
||||
#ifdef MAP_THREAD_TO_ENV
|
||||
dutil->ti = ti;
|
||||
#endif
|
||||
|
@ -1140,11 +1156,15 @@ makeDUtil( MPFORMAL JNIEnv* env,
|
|||
|
||||
SET_DPROC(onGameGoneReceived);
|
||||
SET_DPROC(sendViaWeb);
|
||||
SET_DPROC(getRegValues);
|
||||
|
||||
#undef SET_DPROC
|
||||
|
||||
assertTableFull( vtable, sizeof(*vtable), "dutil" );
|
||||
return &dutil->dutil;
|
||||
|
||||
dvc_init( super, env );
|
||||
|
||||
return super;
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -55,6 +55,7 @@ typedef struct _DevCtxt {
|
|||
static DevCtxt*
|
||||
load( XW_DUtilCtxt* dutil, XWEnv xwe )
|
||||
{
|
||||
ASSERT_MAGIC();
|
||||
DevCtxt* state = (DevCtxt*)dutil->devCtxt;
|
||||
if ( NULL == state ) {
|
||||
XWStreamCtxt* stream = mkStream( dutil );
|
||||
|
@ -80,6 +81,7 @@ load( XW_DUtilCtxt* dutil, XWEnv xwe )
|
|||
void
|
||||
dvc_store( XW_DUtilCtxt* dutil, XWEnv xwe )
|
||||
{
|
||||
ASSERT_MAGIC();
|
||||
DevCtxt* state = load( dutil, xwe );
|
||||
XWStreamCtxt* stream = mkStream( dutil );
|
||||
stream_putU16( stream, state->devCount );
|
||||
|
@ -162,6 +164,7 @@ getMQTTDevID( XW_DUtilCtxt* dutil, XWEnv xwe, XP_Bool forceNew, MQTTDevID* devID
|
|||
void
|
||||
dvc_getMQTTDevID( XW_DUtilCtxt* dutil, XWEnv xwe, MQTTDevID* devID )
|
||||
{
|
||||
ASSERT_MAGIC();
|
||||
getMQTTDevID( dutil, xwe, XP_FALSE, devID );
|
||||
}
|
||||
|
||||
|
@ -213,6 +216,7 @@ dvc_getMQTTSubTopics( XW_DUtilCtxt* dutil, XWEnv xwe,
|
|||
XP_UCHAR* storage, XP_U16 XP_UNUSED_DBG(storageLen),
|
||||
XP_U16* nTopics, XP_UCHAR* topics[] )
|
||||
{
|
||||
ASSERT_MAGIC();
|
||||
LOG_FUNC();
|
||||
int offset = 0;
|
||||
XP_U16 count = 0;
|
||||
|
@ -311,6 +315,7 @@ dvc_makeMQTTInvites( XW_DUtilCtxt* dutil, XWEnv xwe,
|
|||
const MQTTDevID* addressee,
|
||||
const NetLaunchInfo* nli )
|
||||
{
|
||||
ASSERT_MAGIC();
|
||||
XP_UCHAR devTopic[64]; /* used by two below */
|
||||
formatMQTTDevTopic( addressee, devTopic, VSIZE(devTopic) );
|
||||
/* Stream format is identical for both topics */
|
||||
|
@ -339,7 +344,7 @@ dvc_makeMQTTNukeInvite( XW_DUtilCtxt* dutil, XWEnv xwe,
|
|||
MsgAndTopicProc proc, void* closure,
|
||||
const NetLaunchInfo* nli )
|
||||
{
|
||||
LOG_FUNC();
|
||||
ASSERT_MAGIC();
|
||||
#ifdef MQTT_GAMEID_TOPICS
|
||||
MQTTDevID myID;
|
||||
dvc_getMQTTDevID( dutil, xwe, &myID );
|
||||
|
@ -361,6 +366,7 @@ dvc_makeMQTTMessages( XW_DUtilCtxt* dutil, XWEnv xwe,
|
|||
const MQTTDevID* addressee,
|
||||
XP_U32 gameID, XP_U16 streamVersion )
|
||||
{
|
||||
ASSERT_MAGIC();
|
||||
XP_S16 nSent0 = 0;
|
||||
XP_S16 nSent1 = 0;
|
||||
XP_U8 nBufs = 0;
|
||||
|
@ -429,6 +435,7 @@ dvc_makeMQTTNoSuchGames( XW_DUtilCtxt* dutil, XWEnv xwe,
|
|||
const MQTTDevID* addressee,
|
||||
XP_U32 gameID )
|
||||
{
|
||||
ASSERT_MAGIC();
|
||||
XP_LOGFF( "(gameID: %X)", gameID );
|
||||
XP_UCHAR devTopic[64]; /* used by two below */
|
||||
formatMQTTDevTopic( addressee, devTopic, VSIZE(devTopic) );
|
||||
|
@ -528,6 +535,7 @@ dvc_parseMQTTPacket( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_UCHAR* topic,
|
|||
const XP_U8* buf, XP_U16 len )
|
||||
{
|
||||
XP_LOGFF( "(topic=%s, len=%d)", topic, len );
|
||||
ASSERT_MAGIC();
|
||||
|
||||
MQTTDevID myID;
|
||||
dvc_getMQTTDevID( dutil, xwe, &myID );
|
||||
|
@ -558,7 +566,6 @@ dvc_parseMQTTPacket( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_UCHAR* topic,
|
|||
MQTTCmd cmd = stream_getU8( stream );
|
||||
|
||||
/* Need to ack even if discarded/malformed */
|
||||
// dutil_ackMQTTMsg( dutil, xwe, topic, gameID, &senderID, buf, len );
|
||||
ackMQTTMsg( dutil, xwe, topic, gameID, buf, len );
|
||||
|
||||
switch ( cmd ) {
|
||||
|
@ -605,8 +612,9 @@ registerIf( XW_DUtilCtxt* dutil, XWEnv xwe )
|
|||
|
||||
XP_U32 now = dutil_getCurSeconds( dutil, xwe );
|
||||
if ( prevNow + REG_INTERVAL_SECS < now ) {
|
||||
XP_LOGFF( "been long enough; trying to register" );
|
||||
cJSON* params = cJSON_CreateObject();
|
||||
|
||||
/* Start with the platform's values */
|
||||
cJSON* params = dutil_getRegValues( dutil, xwe );
|
||||
|
||||
MQTTDevID myID;
|
||||
dvc_getMQTTDevID( dutil, xwe, &myID );
|
||||
|
@ -615,9 +623,7 @@ registerIf( XW_DUtilCtxt* dutil, XWEnv xwe )
|
|||
cJSON_AddStringToObject( params, "devid", tmp );
|
||||
|
||||
cJSON_AddStringToObject( params, "gitrev", GITREV_SHORT );
|
||||
cJSON_AddStringToObject( params, "os", "Linux" );
|
||||
/* // PENDING remove me in favor of SDK_INT */
|
||||
cJSON_AddStringToObject( params, "vers", "DEBUG" );
|
||||
/* params.put( "versI", Build.VERSION.SDK_INT ); */
|
||||
/* params.put( "vrntCode", BuildConfig.VARIANT_CODE ); */
|
||||
/* params.put( "vrntName", BuildConfig.VARIANT_NAME ); */
|
||||
|
@ -642,7 +648,7 @@ void
|
|||
dvc_init( XW_DUtilCtxt* dutil, XWEnv xwe )
|
||||
{
|
||||
LOG_FUNC();
|
||||
XP_ASSERT( 00 == dutil->magic );
|
||||
XP_ASSERT( 0 == dutil->magic );
|
||||
dutil->magic = MAGIC_INITED;
|
||||
registerIf( dutil, xwe );
|
||||
}
|
||||
|
|
|
@ -102,6 +102,8 @@ typedef struct _DUtilVtable {
|
|||
void (*m_dutil_onCtrlReceived)( XW_DUtilCtxt* duc, XWEnv xwe, const XP_U8* buf, XP_U16 len );
|
||||
void (*m_dutil_onGameGoneReceived)( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID,
|
||||
const CommsAddrRec* from );
|
||||
/* Return platform-specific registration keys->values */
|
||||
cJSON* (*m_dutil_getRegValues)( XW_DUtilCtxt* duc, XWEnv xwe );
|
||||
void (*m_dutil_sendViaWeb)( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* api,
|
||||
const cJSON* params );
|
||||
} DUtilVtable;
|
||||
|
@ -186,4 +188,8 @@ void dutil_super_init( MPFORMAL XW_DUtilCtxt* dutil );
|
|||
(duc)->vtable.m_dutil_onGameGoneReceived((duc),(xwe),(gameID),(from))
|
||||
#define dutil_sendViaWeb( duc, xwe, api, params ) \
|
||||
(duc)->vtable.m_dutil_sendViaWeb((duc), (xwe), (api), (params))
|
||||
|
||||
#define dutil_getRegValues( duc, xwe ) \
|
||||
(duc)->vtable.m_dutil_getRegValues( (duc), (xwe) )
|
||||
|
||||
#endif
|
||||
|
|
|
@ -289,6 +289,19 @@ linux_dutil_sendViaWeb( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe),
|
|||
pthread_detach( thrd );
|
||||
}
|
||||
|
||||
static cJSON*
|
||||
linux_dutil_getRegValues( XW_DUtilCtxt* duc, XWEnv xwe )
|
||||
{
|
||||
XP_USE(duc);
|
||||
XP_USE(xwe);
|
||||
|
||||
cJSON* results = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject( results, "os", "Linux" );
|
||||
cJSON_AddStringToObject( results, "vers", "DEBUG" );
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
XW_DUtilCtxt*
|
||||
linux_dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure )
|
||||
{
|
||||
|
@ -339,6 +352,7 @@ linux_dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure )
|
|||
SET_PROC(onCtrlReceived);
|
||||
SET_PROC(onGameGoneReceived);
|
||||
SET_PROC(sendViaWeb);
|
||||
SET_PROC(getRegValues);
|
||||
|
||||
# undef SET_PROC
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue