mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-01 19:57:11 +01:00
remove ackMQTTMsg() in favor of sendViaWeb()
Linux still doesn't work. I need to figure out curl :-(
This commit is contained in:
parent
ab8d3d168c
commit
ee26816420
8 changed files with 108 additions and 124 deletions
|
@ -588,25 +588,6 @@ public class MQTTUtils extends Thread
|
|||
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
|
||||
@Override
|
||||
public void connectComplete( boolean reconnect, String serverURI )
|
||||
|
|
|
@ -145,6 +145,22 @@ public class NetUtils {
|
|||
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,
|
||||
String proc )
|
||||
{
|
||||
|
@ -159,8 +175,8 @@ public class NetUtils {
|
|||
return makeHttpConn( context, url, proc );
|
||||
}
|
||||
|
||||
private static HttpURLConnection makeHttpConn( Context context,
|
||||
String path, String proc )
|
||||
private static HttpURLConnection makeHttpConn( Context context, String path,
|
||||
String proc )
|
||||
{
|
||||
HttpURLConnection result = null;
|
||||
try {
|
||||
|
|
|
@ -38,6 +38,7 @@ import org.eehouse.android.xw4.GamesListDelegate;
|
|||
import org.eehouse.android.xw4.Log;
|
||||
import org.eehouse.android.xw4.MQTTUtils;
|
||||
import org.eehouse.android.xw4.NetLaunchInfo;
|
||||
import org.eehouse.android.xw4.NetUtils;
|
||||
import org.eehouse.android.xw4.R;
|
||||
import org.eehouse.android.xw4.Utils;
|
||||
import org.eehouse.android.xw4.XWApp;
|
||||
|
@ -333,6 +334,11 @@ public class DUtilCtxt {
|
|||
DupeModeTimer.timerChanged( m_context, gameID, newVal );
|
||||
}
|
||||
|
||||
public void sendViaWeb( String api, String jsonParams )
|
||||
{
|
||||
NetUtils.sendViaWeb( m_context, api, jsonParams );
|
||||
}
|
||||
|
||||
public void onInviteReceived( NetLaunchInfo nli )
|
||||
{
|
||||
// Log.d( TAG, "onInviteReceived(%s)", nli );
|
||||
|
@ -358,9 +364,4 @@ public class DUtilCtxt {
|
|||
{
|
||||
MQTTUtils.handleCtrlReceived( m_context, msg );
|
||||
}
|
||||
|
||||
public void ackMQTTMsg( String topic, int gameID, String senderID, byte[] msg )
|
||||
{
|
||||
MQTTUtils.ackMessage( m_context, topic, gameID, senderID, msg );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,7 +95,9 @@ COMMON_SRC_FILES += \
|
|||
$(COMMON_PATH)/dutil.c \
|
||||
$(COMMON_PATH)/device.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_SRC_FILES := $(linux_SRC_FILES) $(LOCAL_SRC_FILES) $(COMMON_SRC_FILES)
|
||||
|
|
|
@ -970,22 +970,16 @@ and_dutil_onGameGoneReceived( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID,
|
|||
}
|
||||
|
||||
static void
|
||||
and_dutil_ackMQTTMsg( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* topic,
|
||||
XP_U32 gameID, const MQTTDevID* senderID, const XP_U8* msg,
|
||||
XP_U16 len )
|
||||
and_dutil_sendViaWeb( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* api,
|
||||
const cJSON* params )
|
||||
{
|
||||
DUTIL_CBK_HEADER( "ackMQTTMsg", "(Ljava/lang/String;ILjava/lang/String;[B)V" );
|
||||
|
||||
XP_UCHAR tmp[32];
|
||||
formatMQTTDevID( senderID, tmp, VSIZE(tmp) );
|
||||
jstring jTopic = (*env)->NewStringUTF( env, topic );
|
||||
jstring jdevid = (*env)->NewStringUTF( env, tmp );
|
||||
jbyteArray jmsg = makeByteArray( env, len, (const jbyte*)msg );
|
||||
|
||||
(*env)->CallVoidMethod( env, dutil->jdutil, mid, jTopic, gameID,
|
||||
jdevid, jmsg );
|
||||
|
||||
deleteLocalRefs( env, jdevid, jmsg, DELETE_NO_REF );
|
||||
DUTIL_CBK_HEADER( "sendViaWeb", "(Ljava/lang/String;Ljava/lang/String;)V" );
|
||||
char* pstr = cJSON_PrintUnformatted( params );
|
||||
jstring jParams = (*env)->NewStringUTF( env, pstr );
|
||||
jstring jApi = (*env)->NewStringUTF( env, api );
|
||||
(*env)->CallVoidMethod( env, dutil->jdutil, mid, jApi, jParams );
|
||||
deleteLocalRefs( env, jApi, jParams, DELETE_NO_REF );
|
||||
free( pstr );
|
||||
DUTIL_CBK_TAIL();
|
||||
}
|
||||
|
||||
|
@ -1145,7 +1139,7 @@ makeDUtil( MPFORMAL JNIEnv* env,
|
|||
SET_DPROC(onCtrlReceived);
|
||||
|
||||
SET_DPROC(onGameGoneReceived);
|
||||
SET_DPROC(ackMQTTMsg);
|
||||
SET_DPROC(sendViaWeb);
|
||||
|
||||
#undef SET_DPROC
|
||||
|
||||
|
|
|
@ -507,9 +507,7 @@ ackMQTTMsg( XW_DUtilCtxt* dutil, XWEnv xwe, const XP_UCHAR* topic,
|
|||
dutil_md5sum( dutil, xwe, buf, len, &sb );
|
||||
cJSON_AddStringToObject( params, "sum", sb.buf );
|
||||
|
||||
XP_UCHAR gid16[16];
|
||||
XP_SNPRINTF( gid16, VSIZE(gid16), "%08X", gameID );
|
||||
cJSON_AddStringToObject( params, "gid16", gid16 );
|
||||
cJSON_AddNumberToObject( params, "gid", gameID );
|
||||
|
||||
dutil_sendViaWeb( dutil, xwe, "ack", params );
|
||||
cJSON_Delete( params );
|
||||
|
|
|
@ -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_onGameGoneReceived)( XW_DUtilCtxt* duc, XWEnv xwe, XP_U32 gameID,
|
||||
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,
|
||||
const cJSON* params );
|
||||
} DUtilVtable;
|
||||
|
@ -185,9 +181,6 @@ void dutil_super_init( MPFORMAL XW_DUtilCtxt* dutil );
|
|||
(duc)->vtable.m_dutil_onCtrlReceived((duc),(xwe),(buf),(len))
|
||||
#define 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 ) \
|
||||
(duc)->vtable.m_dutil_sendViaWeb((duc), (xwe), (api), (params))
|
||||
#endif
|
||||
|
|
|
@ -202,59 +202,59 @@ linux_dutil_onGameGoneReceived( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe),
|
|||
}
|
||||
}
|
||||
|
||||
typedef struct _AckData {
|
||||
XW_DUtilCtxt* duc;
|
||||
XP_U8* msg;
|
||||
XP_U16 len;
|
||||
gchar* topic;
|
||||
XP_U32 gameID;
|
||||
} AckData;
|
||||
/* typedef struct _AckData { */
|
||||
/* XW_DUtilCtxt* duc; */
|
||||
/* XP_U8* msg; */
|
||||
/* XP_U16 len; */
|
||||
/* gchar* topic; */
|
||||
/* XP_U32 gameID; */
|
||||
/* } AckData; */
|
||||
|
||||
static void
|
||||
sendViaCurl( LinDUtilCtxt* lduc, AckData* adp )
|
||||
{
|
||||
LaunchParams* params = (LaunchParams*)lduc->super.closure;
|
||||
/* static void */
|
||||
/* sendViaCurl( LinDUtilCtxt* lduc, AckData* adp ) */
|
||||
/* { */
|
||||
/* LaunchParams* params = (LaunchParams*)lduc->super.closure; */
|
||||
|
||||
CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
XP_ASSERT(res == CURLE_OK);
|
||||
CURL* curl = curl_easy_init();
|
||||
/* CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT); */
|
||||
/* XP_ASSERT(res == CURLE_OK); */
|
||||
/* CURL* curl = curl_easy_init(); */
|
||||
|
||||
char url[128];
|
||||
snprintf( url, sizeof(url), "https://%s/xw4/api/v1/ack",
|
||||
params->connInfo.mqtt.hostName );
|
||||
curl_easy_setopt( curl, CURLOPT_URL, url );
|
||||
/* char url[128]; */
|
||||
/* snprintf( url, sizeof(url), "https://%s/xw4/api/v1/ack", */
|
||||
/* params->connInfo.mqtt.hostName ); */
|
||||
/* curl_easy_setopt( curl, CURLOPT_URL, url ); */
|
||||
|
||||
gchar* sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, adp->msg, adp->len );
|
||||
gchar* json
|
||||
= g_strdup_printf("{\"topic\": \"%s\", \"gid\": %u, \"sum\": \"%s\"}",
|
||||
adp->topic, adp->gameID, sum );
|
||||
// XP_LOGFF( "json: %s", json );
|
||||
g_free( sum );
|
||||
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, json );
|
||||
curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, -1L );
|
||||
/* gchar* sum = g_compute_checksum_for_data( G_CHECKSUM_MD5, adp->msg, adp->len ); */
|
||||
/* gchar* json */
|
||||
/* = g_strdup_printf("{\"topic\": \"%s\", \"gid\": %u, \"sum\": \"%s\"}", */
|
||||
/* adp->topic, adp->gameID, sum ); */
|
||||
/* // XP_LOGFF( "json: %s", json ); */
|
||||
/* g_free( sum ); */
|
||||
/* curl_easy_setopt( curl, CURLOPT_POSTFIELDS, json ); */
|
||||
/* curl_easy_setopt( curl, CURLOPT_POSTFIELDSIZE, -1L ); */
|
||||
|
||||
struct curl_slist *headers = NULL;
|
||||
headers = curl_slist_append(headers, "Expect:");
|
||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||||
/* struct curl_slist *headers = NULL; */
|
||||
/* headers = curl_slist_append(headers, "Expect:"); */
|
||||
/* headers = curl_slist_append(headers, "Content-Type: application/json"); */
|
||||
/* curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); */
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
XP_Bool success = res == CURLE_OK;
|
||||
/* XP_LOGFF( "curl_easy_perform() => %d", res ); */
|
||||
if ( ! success ) {
|
||||
XP_LOGFF( "curl_easy_perform() failed: %s", curl_easy_strerror(res));
|
||||
}
|
||||
/* res = curl_easy_perform(curl); */
|
||||
/* XP_Bool success = res == CURLE_OK; */
|
||||
/* /\* XP_LOGFF( "curl_easy_perform() => %d", res ); *\/ */
|
||||
/* if ( ! success ) { */
|
||||
/* XP_LOGFF( "curl_easy_perform() failed: %s", curl_easy_strerror(res)); */
|
||||
/* } */
|
||||
|
||||
curl_slist_free_all( headers );
|
||||
curl_easy_cleanup( curl );
|
||||
curl_global_cleanup();
|
||||
g_free( json );
|
||||
/* curl_slist_free_all( headers ); */
|
||||
/* curl_easy_cleanup( curl ); */
|
||||
/* curl_global_cleanup(); */
|
||||
/* g_free( json ); */
|
||||
|
||||
/* g_idle_add( nuke_ack_data, ad ); */
|
||||
g_free( adp->topic );
|
||||
g_free( adp->msg );
|
||||
g_free( adp );
|
||||
}
|
||||
/* /\* g_idle_add( nuke_ack_data, ad ); *\/ */
|
||||
/* g_free( adp->topic ); */
|
||||
/* g_free( adp->msg ); */
|
||||
/* g_free( adp ); */
|
||||
/* } */
|
||||
|
||||
#ifdef ACK_IN_BACKGROUND
|
||||
static void*
|
||||
|
@ -284,32 +284,32 @@ sendAckThreadProc( void* arg )
|
|||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
linux_dutil_ackMQTTMsg( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe),
|
||||
const XP_UCHAR* topic, XP_U32 gameID,
|
||||
const MQTTDevID* XP_UNUSED(senderID),
|
||||
const XP_U8* msg, XP_U16 len )
|
||||
{
|
||||
AckData ad = {
|
||||
.duc = duc,
|
||||
.topic = g_strdup( topic ),
|
||||
.gameID = gameID,
|
||||
.len = len,
|
||||
.msg = g_memdup2( msg, len ),
|
||||
};
|
||||
AckData* adp = g_memdup2( &ad, sizeof(ad) );
|
||||
/* static void */
|
||||
/* linux_dutil_ackMQTTMsg( XW_DUtilCtxt* duc, XWEnv XP_UNUSED(xwe), */
|
||||
/* const XP_UCHAR* topic, XP_U32 gameID, */
|
||||
/* const MQTTDevID* XP_UNUSED(senderID), */
|
||||
/* const XP_U8* msg, XP_U16 len ) */
|
||||
/* { */
|
||||
/* AckData ad = { */
|
||||
/* .duc = duc, */
|
||||
/* .topic = g_strdup( topic ), */
|
||||
/* .gameID = gameID, */
|
||||
/* .len = len, */
|
||||
/* .msg = g_memdup2( msg, len ), */
|
||||
/* }; */
|
||||
/* AckData* adp = g_memdup2( &ad, sizeof(ad) ); */
|
||||
|
||||
LinDUtilCtxt* lduc = (LinDUtilCtxt*)duc;
|
||||
#ifdef ACK_IN_BACKGROUND
|
||||
pthread_mutex_lock( &lduc->ackMutex );
|
||||
lduc->ackList = g_list_append( lduc->ackList, adp );
|
||||
pthread_cond_signal( &lduc->ackCondVar );
|
||||
pthread_mutex_unlock( &lduc->ackMutex );
|
||||
#else
|
||||
sendViaCurl( lduc, adp );
|
||||
#endif
|
||||
/* LOG_RETURN_VOID(); */
|
||||
}
|
||||
/* LinDUtilCtxt* lduc = (LinDUtilCtxt*)duc; */
|
||||
/* #ifdef ACK_IN_BACKGROUND */
|
||||
/* pthread_mutex_lock( &lduc->ackMutex ); */
|
||||
/* lduc->ackList = g_list_append( lduc->ackList, adp ); */
|
||||
/* pthread_cond_signal( &lduc->ackCondVar ); */
|
||||
/* pthread_mutex_unlock( &lduc->ackMutex ); */
|
||||
/* #else */
|
||||
/* sendViaCurl( lduc, adp ); */
|
||||
/* #endif */
|
||||
/* /\* LOG_RETURN_VOID(); *\/ */
|
||||
/* } */
|
||||
|
||||
static void
|
||||
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(onCtrlReceived);
|
||||
SET_PROC(onGameGoneReceived);
|
||||
SET_PROC(ackMQTTMsg);
|
||||
SET_PROC(sendViaWeb);
|
||||
|
||||
# undef SET_PROC
|
||||
|
|
Loading…
Add table
Reference in a new issue