diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MQTTUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MQTTUtils.java index 183555366..9227c660b 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MQTTUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MQTTUtils.java @@ -483,6 +483,27 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba addToSendQueue( context, topic[0], packet ); } + public static void ackMessage( Context context, int gameID, + String senderDevID, byte[] payload ) + { + String sum = Utils.getMD5SumFor( payload ); + JSONObject params = new JSONObject(); + try { + params.put( "sum", sum ); + params.put( "gid", gameID ); + // params.put( "from", senderDevID ); + // params.put( "to", XwJNI.dvc_getMQTTDevID( null ) ); + + HttpsURLConnection conn + = NetUtils.makeHttpsMQTTConn( context, "ack" ); + String resStr = NetUtils.runConn( conn, params, true ); + Log.d( TAG, "runConn(ack) => %s", resStr ); + } catch ( JSONException je ) { + Log.e( TAG, "ackMessage() ex: %s", je ); + } + } + + // MqttCallbackExtended @Override public void connectComplete(boolean reconnect, String serverURI) { @@ -498,7 +519,7 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba } @Override - public void messageArrived( String topic, MqttMessage message) throws Exception + public void messageArrived( String topic, MqttMessage message ) throws Exception { Log.d( TAG, "%H.messageArrived(topic=%s)", this, topic ); Assert.assertTrueNR( topic.equals(mTopic) ); @@ -533,6 +554,7 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba } } + // IMqttActionListener @Override public void onSuccess( IMqttToken asyncActionToken ) { diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java index 1989dd449..e05292e8d 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/jni/DUtilCtxt.java @@ -372,4 +372,9 @@ public class DUtilCtxt { Assert.assertTrueNR( from.contains( CommsAddrRec.CommsConnType.COMMS_CONN_MQTT ) ); MQTTUtils.handleGameGone( m_context, from, gameID ); } + + public void ackMQTTMsg( int gameID, String senderID, byte[] msg ) + { + MQTTUtils.ackMessage( m_context, gameID, senderID, msg ); + } } diff --git a/xwords4/android/jni/utilwrapper.c b/xwords4/android/jni/utilwrapper.c index 13851506c..11fd07e21 100644 --- a/xwords4/android/jni/utilwrapper.c +++ b/xwords4/android/jni/utilwrapper.c @@ -29,6 +29,7 @@ #include "LocalizedStrIncludes.h" #include "dbgutil.h" #include "nli.h" +#include "strutils.h" #define MAX_QUANTITY_STRS 4 @@ -919,6 +920,23 @@ and_dutil_onGameGoneReceived( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID, DUTIL_CBK_TAIL(); } +static void +and_dutil_ackMQTTMsg( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID, + const MQTTDevID* senderID, const XP_U8* msg, XP_U16 len ) +{ + DUTIL_CBK_HEADER( "ackMQTTMsg", "(ILjava/lang/String;[B)V" ); + + XP_UCHAR tmp[32]; + formatMQTTDevID( senderID, tmp, VSIZE(tmp) ); + jstring jdevid = (*env)->NewStringUTF( env, tmp ); + jbyteArray jmsg = makeByteArray( env, len, (const jbyte*)msg ); + + (*env)->CallVoidMethod( env, dutil->jdutil, mid, gameID, jdevid, jmsg ); + + deleteLocalRefs( env, jdevid, jmsg, DELETE_NO_REF ); + DUTIL_CBK_TAIL(); +} + XW_UtilCtxt* makeUtil( MPFORMAL JNIEnv* env, #ifdef MAP_THREAD_TO_ENV @@ -1073,6 +1091,7 @@ makeDUtil( MPFORMAL JNIEnv* env, SET_DPROC(onInviteReceived); SET_DPROC(onMessageReceived); SET_DPROC(onGameGoneReceived); + SET_DPROC(ackMQTTMsg); #undef SET_DPROC diff --git a/xwords4/common/device.c b/xwords4/common/device.c index 59718d085..effc3c033 100644 --- a/xwords4/common/device.c +++ b/xwords4/common/device.c @@ -223,9 +223,14 @@ dvc_parseMQTTPacket( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_U8* buf, XP_U16 le XP_LOGFF( "read proto %d, expected %d; dropping packet", proto, PROTO_1 ); } else { - MQTTDevID myID; - stream_getBytes( stream, &myID, sizeof(myID) ); - myID = be64toh( myID ); + MQTTDevID senderID; + stream_getBytes( stream, &senderID, sizeof(senderID) ); + senderID = be64toh( senderID ); +#ifdef DEBUG + XP_UCHAR tmp[32]; + formatMQTTDevID( &senderID, tmp, VSIZE(tmp) ); + XP_LOGFF( "senderID: %s", tmp ); +#endif MQTTCmd cmd; XP_U32 gameID = 0; @@ -233,6 +238,9 @@ dvc_parseMQTTPacket( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_U8* buf, XP_U16 le gameID = stream_getU32( stream ); cmd = stream_getU8( stream ); + /* Need to ack even if discarded/malformed */ + dutil_ackMQTTMsg( dutil, xwe, gameID, &senderID, buf, len ); + switch ( cmd ) { case CMD_INVITE: { NetLaunchInfo nli = {0}; @@ -245,7 +253,7 @@ dvc_parseMQTTPacket( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_U8* buf, XP_U16 le case CMD_MSG: { CommsAddrRec from = {0}; addr_addType( &from, COMMS_CONN_MQTT ); - from.u.mqtt.devID = myID; + from.u.mqtt.devID = senderID; if ( CMD_MSG == cmd ) { dutil_onMessageReceived( dutil, xwe, gameID, &from, stream ); } else if ( CMD_DEVGONE == cmd ) { diff --git a/xwords4/common/dutil.h b/xwords4/common/dutil.h index ebb9e8606..4ec1d59a7 100644 --- a/xwords4/common/dutil.h +++ b/xwords4/common/dutil.h @@ -91,6 +91,9 @@ typedef struct _DUtilVtable { const CommsAddrRec* from, XWStreamCtxt* stream ); void (*m_dutil_onGameGoneReceived)( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID, const CommsAddrRec* from ); + + void (*m_dutil_ackMQTTMsg)( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID, + const MQTTDevID* senderID, const XP_U8* msg, XP_U16 len ); } DUtilVtable; struct XW_DUtilCtxt { @@ -160,4 +163,7 @@ void dutil_super_init( MPFORMAL XW_DUtilCtxt* dutil ); #define dutil_onGameGoneReceived(duc, xwe, gameID, from) \ (duc)->vtable.m_dutil_onGameGoneReceived((duc),(xwe),(gameID),(from)) +#define dutil_ackMQTTMsg( duc, xwe, gameID, senderID, msg, len ) \ + (duc)->vtable.m_dutil_ackMQTTMsg((duc), (xwe), (gameID), (senderID), (msg), (len) ) + #endif diff --git a/xwords4/linux/lindutil.c b/xwords4/linux/lindutil.c index e0f913197..98556caea 100644 --- a/xwords4/linux/lindutil.c +++ b/xwords4/linux/lindutil.c @@ -130,6 +130,19 @@ linux_dutil_onGameGoneReceived( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe), } } +static void +linux_dutil_ackMQTTMsg( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID, + const MQTTDevID* senderID, const XP_U8* msg, XP_U16 len ) +{ + XP_USE(duc); + XP_USE(xwe); + XP_USE(gameID); + XP_USE(senderID); + XP_USE(msg); + XP_USE(len); + XP_LOGFF( "doing nothing" ); +} + XW_DUtilCtxt* linux_dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure ) { @@ -168,6 +181,7 @@ linux_dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure ) SET_PROC(onInviteReceived); SET_PROC(onMessageReceived); SET_PROC(onGameGoneReceived); + SET_PROC(ackMQTTMsg); # undef SET_PROC