remove ackMQTTMsg() in favor of sendViaWeb()

Linux still doesn't work. I need to figure out curl :-(
This commit is contained in:
Eric House 2024-02-12 17:37:13 -08:00
parent ab8d3d168c
commit ee26816420
8 changed files with 108 additions and 124 deletions

View file

@ -588,25 +588,6 @@ public class MQTTUtils extends Thread
addToSendQueue( context, tap ); addToSendQueue( context, tap );
} }
public static void ackMessage( Context context, String topic, int gameID,
String senderDevID, byte[] payload )
{
String sum = Utils.getMD5SumFor( payload );
JSONObject params = new JSONObject();
try {
params.put( "topic", topic );
params.put( "gid", gameID );
params.put( "sum", sum );
HttpURLConnection conn
= NetUtils.makeHttpMQTTConn( 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 // MqttCallbackExtended
@Override @Override
public void connectComplete( boolean reconnect, String serverURI ) public void connectComplete( boolean reconnect, String serverURI )

View file

@ -145,6 +145,22 @@ public class NetUtils {
context.startActivity( intent ); context.startActivity( intent );
} }
public static void sendViaWeb( final Context context, final String api,
final String jsonParams )
{
Log.d( TAG, "sendViaWeb(api: %s, params: %s)", api, jsonParams );
new Thread( new Runnable() {
@Override
public void run() {
HttpURLConnection conn = makeHttpMQTTConn( context, api );
boolean directJson = true;
String result = runConn( conn, jsonParams, directJson );
Log.d( TAG, "sendViaWeb(api: %s): got result '%s'", api, result );
}
} ).start();
}
public static HttpURLConnection makeHttpMQTTConn( Context context, public static HttpURLConnection makeHttpMQTTConn( Context context,
String proc ) String proc )
{ {
@ -159,8 +175,8 @@ public class NetUtils {
return makeHttpConn( context, url, proc ); return makeHttpConn( context, url, proc );
} }
private static HttpURLConnection makeHttpConn( Context context, private static HttpURLConnection makeHttpConn( Context context, String path,
String path, String proc ) String proc )
{ {
HttpURLConnection result = null; HttpURLConnection result = null;
try { try {

View file

@ -38,6 +38,7 @@ import org.eehouse.android.xw4.GamesListDelegate;
import org.eehouse.android.xw4.Log; import org.eehouse.android.xw4.Log;
import org.eehouse.android.xw4.MQTTUtils; import org.eehouse.android.xw4.MQTTUtils;
import org.eehouse.android.xw4.NetLaunchInfo; import org.eehouse.android.xw4.NetLaunchInfo;
import org.eehouse.android.xw4.NetUtils;
import org.eehouse.android.xw4.R; import org.eehouse.android.xw4.R;
import org.eehouse.android.xw4.Utils; import org.eehouse.android.xw4.Utils;
import org.eehouse.android.xw4.XWApp; import org.eehouse.android.xw4.XWApp;
@ -333,6 +334,11 @@ public class DUtilCtxt {
DupeModeTimer.timerChanged( m_context, gameID, newVal ); DupeModeTimer.timerChanged( m_context, gameID, newVal );
} }
public void sendViaWeb( String api, String jsonParams )
{
NetUtils.sendViaWeb( m_context, api, jsonParams );
}
public void onInviteReceived( NetLaunchInfo nli ) public void onInviteReceived( NetLaunchInfo nli )
{ {
// Log.d( TAG, "onInviteReceived(%s)", nli ); // Log.d( TAG, "onInviteReceived(%s)", nli );
@ -358,9 +364,4 @@ public class DUtilCtxt {
{ {
MQTTUtils.handleCtrlReceived( m_context, msg ); MQTTUtils.handleCtrlReceived( m_context, msg );
} }
public void ackMQTTMsg( String topic, int gameID, String senderID, byte[] msg )
{
MQTTUtils.ackMessage( m_context, topic, gameID, senderID, msg );
}
} }

View file

@ -96,6 +96,8 @@ COMMON_SRC_FILES += \
$(COMMON_PATH)/device.c \ $(COMMON_PATH)/device.c \
$(COMMON_PATH)/knownplyr.c \ $(COMMON_PATH)/knownplyr.c \
$(COMMON_PATH)/dllist.c \ $(COMMON_PATH)/dllist.c \
$(COMMON_PATH)/cJSON.c \
$(COMMON_PATH)/cJSON_Utils.c\
LOCAL_CFLAGS+=$(LOCAL_C_INCLUDES) $(LOCAL_DEFINES) -Wall -std=c99 LOCAL_CFLAGS+=$(LOCAL_C_INCLUDES) $(LOCAL_DEFINES) -Wall -std=c99
LOCAL_SRC_FILES := $(linux_SRC_FILES) $(LOCAL_SRC_FILES) $(COMMON_SRC_FILES) LOCAL_SRC_FILES := $(linux_SRC_FILES) $(LOCAL_SRC_FILES) $(COMMON_SRC_FILES)

View file

@ -970,22 +970,16 @@ and_dutil_onGameGoneReceived( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID,
} }
static void static void
and_dutil_ackMQTTMsg( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* topic, and_dutil_sendViaWeb( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* api,
XP_U32 gameID, const MQTTDevID* senderID, const XP_U8* msg, const cJSON* params )
XP_U16 len )
{ {
DUTIL_CBK_HEADER( "ackMQTTMsg", "(Ljava/lang/String;ILjava/lang/String;[B)V" ); DUTIL_CBK_HEADER( "sendViaWeb", "(Ljava/lang/String;Ljava/lang/String;)V" );
char* pstr = cJSON_PrintUnformatted( params );
XP_UCHAR tmp[32]; jstring jParams = (*env)->NewStringUTF( env, pstr );
formatMQTTDevID( senderID, tmp, VSIZE(tmp) ); jstring jApi = (*env)->NewStringUTF( env, api );
jstring jTopic = (*env)->NewStringUTF( env, topic ); (*env)->CallVoidMethod( env, dutil->jdutil, mid, jApi, jParams );
jstring jdevid = (*env)->NewStringUTF( env, tmp ); deleteLocalRefs( env, jApi, jParams, DELETE_NO_REF );
jbyteArray jmsg = makeByteArray( env, len, (const jbyte*)msg ); free( pstr );
(*env)->CallVoidMethod( env, dutil->jdutil, mid, jTopic, gameID,
jdevid, jmsg );
deleteLocalRefs( env, jdevid, jmsg, DELETE_NO_REF );
DUTIL_CBK_TAIL(); DUTIL_CBK_TAIL();
} }
@ -1145,7 +1139,7 @@ makeDUtil( MPFORMAL JNIEnv* env,
SET_DPROC(onCtrlReceived); SET_DPROC(onCtrlReceived);
SET_DPROC(onGameGoneReceived); SET_DPROC(onGameGoneReceived);
SET_DPROC(ackMQTTMsg); SET_DPROC(sendViaWeb);
#undef SET_DPROC #undef SET_DPROC

View file

@ -507,9 +507,7 @@ ackMQTTMsg( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_UCHAR* topic,
dutil_md5sum( dutil, xwe, buf, len, &sb ); dutil_md5sum( dutil, xwe, buf, len, &sb );
cJSON_AddStringToObject( params, "sum", sb.buf ); cJSON_AddStringToObject( params, "sum", sb.buf );
XP_UCHAR gid16[16]; cJSON_AddNumberToObject( params, "gid", gameID );
XP_SNPRINTF( gid16, VSIZE(gid16), "%08X", gameID );
cJSON_AddStringToObject( params, "gid16", gid16 );
dutil_sendViaWeb( dutil, xwe, "ack", params ); dutil_sendViaWeb( dutil, xwe, "ack", params );
cJSON_Delete( params ); cJSON_Delete( params );

View file

@ -102,10 +102,6 @@ typedef struct _DUtilVtable {
void (*m_dutil_onCtrlReceived)( XW_DUtilCtxt* duc, XWEnv xwe, const XP_U8* buf, XP_U16 len ); 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, void (*m_dutil_onGameGoneReceived)( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID,
const CommsAddrRec* from ); const CommsAddrRec* from );
void (*m_dutil_ackMQTTMsg)( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* topic,
XP_U32 gameID, const MQTTDevID* senderID,
const XP_U8* msg, XP_U16 len );
void (*m_dutil_sendViaWeb)( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* api, void (*m_dutil_sendViaWeb)( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* api,
const cJSON* params ); const cJSON* params );
} DUtilVtable; } DUtilVtable;
@ -185,9 +181,6 @@ void dutil_super_init( MPFORMAL XW_DUtilCtxt* dutil );
(duc)->vtable.m_dutil_onCtrlReceived((duc),(xwe),(buf),(len)) (duc)->vtable.m_dutil_onCtrlReceived((duc),(xwe),(buf),(len))
#define dutil_onGameGoneReceived(duc, xwe, gameID, from) \ #define dutil_onGameGoneReceived(duc, xwe, gameID, from) \
(duc)->vtable.m_dutil_onGameGoneReceived((duc),(xwe),(gameID),(from)) (duc)->vtable.m_dutil_onGameGoneReceived((duc),(xwe),(gameID),(from))
#define dutil_ackMQTTMsg( duc, xwe, topic, gameID, senderID, msg, len ) \
(duc)->vtable.m_dutil_ackMQTTMsg( (duc), (xwe), (topic), (gameID), \
(senderID), (msg), (len) )
#define dutil_sendViaWeb( duc, xwe, api, params ) \ #define dutil_sendViaWeb( duc, xwe, api, params ) \
(duc)->vtable.m_dutil_sendViaWeb((duc), (xwe), (api), (params)) (duc)->vtable.m_dutil_sendViaWeb((duc), (xwe), (api), (params))
#endif #endif

View file

@ -202,59 +202,59 @@ linux_dutil_onGameGoneReceived( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe),
} }
} }
typedef struct _AckData { /* typedef struct _AckData { */
XW_DUtilCtxt* duc; /* XW_DUtilCtxt* duc; */
XP_U8* msg; /* XP_U8* msg; */
XP_U16 len; /* XP_U16 len; */
gchar* topic; /* gchar* topic; */
XP_U32 gameID; /* XP_U32 gameID; */
} AckData; /* } AckData; */
static void /* static void */
sendViaCurl( LinDUtilCtxt* lduc, AckData* adp ) /* sendViaCurl( LinDUtilCtxt* lduc, AckData* adp ) */
{ /* { */
LaunchParams* params = (LaunchParams*)lduc->super.closure; /* LaunchParams* params = (LaunchParams*)lduc->super.closure; */
CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT); /* CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT); */
XP_ASSERT(res == CURLE_OK); /* XP_ASSERT(res == CURLE_OK); */
CURL* curl = curl_easy_init(); /* CURL* curl = curl_easy_init(); */
char url[128]; /* char url[128]; */
snprintf( url, sizeof(url), "https://%s/xw4/api/v1/ack", /* snprintf( url, sizeof(url), "https://%s/xw4/api/v1/ack", */
params->connInfo.mqtt.hostName ); /* params->connInfo.mqtt.hostName ); */
curl_easy_setopt( curl, CURLOPT_URL, url ); /* curl_easy_setopt( curl, CURLOPT_URL, url ); */
gchar* sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, adp->msg, adp->len ); /* gchar* sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, adp->msg, adp->len ); */
gchar* json /* gchar* json */
= g_strdup_printf("{\"topic\": \"%s\", \"gid\": %u, \"sum\": \"%s\"}", /* = g_strdup_printf("{\"topic\": \"%s\", \"gid\": %u, \"sum\": \"%s\"}", */
adp->topic, adp->gameID, sum ); /* adp->topic, adp->gameID, sum ); */
// XP_LOGFF( "json: %s", json ); /* // XP_LOGFF( "json: %s", json ); */
g_free( sum ); /* g_free( sum ); */
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, json ); /* curl_easy_setopt( curl, CURLOPT_POSTFIELDS, json ); */
curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, -1L ); /* curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, -1L ); */
struct curl_slist *headers = NULL; /* struct curl_slist *headers = NULL; */
headers = curl_slist_append(headers, "Expect:"); /* headers = curl_slist_append(headers, "Expect:"); */
headers = curl_slist_append(headers, "Content-Type: application/json"); /* headers = curl_slist_append(headers, "Content-Type: application/json"); */
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); /* curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); */
res = curl_easy_perform(curl); /* res = curl_easy_perform(curl); */
XP_Bool success = res == CURLE_OK; /* XP_Bool success = res == CURLE_OK; */
/* XP_LOGFF( "curl_easy_perform() => %d", res ); */ /* /\* XP_LOGFF( "curl_easy_perform() => %d", res ); *\/ */
if ( ! success ) { /* if ( ! success ) { */
XP_LOGFF( "curl_easy_perform() failed: %s", curl_easy_strerror(res)); /* XP_LOGFF( "curl_easy_perform() failed: %s", curl_easy_strerror(res)); */
} /* } */
curl_slist_free_all( headers ); /* curl_slist_free_all( headers ); */
curl_easy_cleanup( curl ); /* curl_easy_cleanup( curl ); */
curl_global_cleanup(); /* curl_global_cleanup(); */
g_free( json ); /* g_free( json ); */
/* g_idle_add( nuke_ack_data, ad ); */ /* /\* g_idle_add( nuke_ack_data, ad ); *\/ */
g_free( adp->topic ); /* g_free( adp->topic ); */
g_free( adp->msg ); /* g_free( adp->msg ); */
g_free( adp ); /* g_free( adp ); */
} /* } */
#ifdef ACK_IN_BACKGROUND #ifdef ACK_IN_BACKGROUND
static void* static void*
@ -284,32 +284,32 @@ sendAckThreadProc( void* arg )
} }
#endif #endif
static void /* static void */
linux_dutil_ackMQTTMsg( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe), /* linux_dutil_ackMQTTMsg( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe), */
const XP_UCHAR* topic, XP_U32 gameID, /* const XP_UCHAR* topic, XP_U32 gameID, */
const MQTTDevID* XP_UNUSED(senderID), /* const MQTTDevID* XP_UNUSED(senderID), */
const XP_U8* msg, XP_U16 len ) /* const XP_U8* msg, XP_U16 len ) */
{ /* { */
AckData ad = { /* AckData ad = { */
.duc = duc, /* .duc = duc, */
.topic = g_strdup( topic ), /* .topic = g_strdup( topic ), */
.gameID = gameID, /* .gameID = gameID, */
.len = len, /* .len = len, */
.msg = g_memdup2( msg, len ), /* .msg = g_memdup2( msg, len ), */
}; /* }; */
AckData* adp = g_memdup2( &ad, sizeof(ad) ); /* AckData* adp = g_memdup2( &ad, sizeof(ad) ); */
LinDUtilCtxt* lduc = (LinDUtilCtxt*)duc; /* LinDUtilCtxt* lduc = (LinDUtilCtxt*)duc; */
#ifdef ACK_IN_BACKGROUND /* #ifdef ACK_IN_BACKGROUND */
pthread_mutex_lock( &lduc->ackMutex ); /* pthread_mutex_lock( &lduc->ackMutex ); */
lduc->ackList = g_list_append( lduc->ackList, adp ); /* lduc->ackList = g_list_append( lduc->ackList, adp ); */
pthread_cond_signal( &lduc->ackCondVar ); /* pthread_cond_signal( &lduc->ackCondVar ); */
pthread_mutex_unlock( &lduc->ackMutex ); /* pthread_mutex_unlock( &lduc->ackMutex ); */
#else /* #else */
sendViaCurl( lduc, adp ); /* sendViaCurl( lduc, adp ); */
#endif /* #endif */
/* LOG_RETURN_VOID(); */ /* /\* LOG_RETURN_VOID(); *\/ */
} /* } */
static void static void
linux_dutil_sendViaWeb( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* api, linux_dutil_sendViaWeb( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* api,
@ -371,7 +371,6 @@ linux_dutils_init( MPFORMAL VTableMgr* vtMgr, void* closure )
SET_PROC(onMessageReceived); SET_PROC(onMessageReceived);
SET_PROC(onCtrlReceived); SET_PROC(onCtrlReceived);
SET_PROC(onGameGoneReceived); SET_PROC(onGameGoneReceived);
SET_PROC(ackMQTTMsg);
SET_PROC(sendViaWeb); SET_PROC(sendViaWeb);
# undef SET_PROC # undef SET_PROC