mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-03 23:04:08 +01:00
post new msg message from relay2
This is to have a utility back, but mostly to start playing with being able to send keepalives to a device that have nothing to do with moves.
This commit is contained in:
parent
dcd3a4cc8c
commit
2e5f6128f2
7 changed files with 59 additions and 21 deletions
|
@ -21,6 +21,7 @@
|
|||
package org.eehouse.android.xw4;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
|
||||
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
|
||||
|
@ -60,7 +61,7 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba
|
|||
|
||||
private MqttAsyncClient mClient;
|
||||
private String mDevID;
|
||||
private String mTopic;
|
||||
private String[] mTopics = { null, null };
|
||||
private Context mContext;
|
||||
private MsgThread mMsgThread;
|
||||
private LinkedBlockingQueue<MessagePair> mOutboundQueue = new LinkedBlockingQueue<>();
|
||||
|
@ -166,7 +167,7 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba
|
|||
me.printStackTrace();
|
||||
break;
|
||||
} catch ( InterruptedException ie ) {
|
||||
ie.printStackTrace();
|
||||
// ie.printStackTrace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -225,10 +226,8 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba
|
|||
{
|
||||
Log.d( TAG, "%H.<init>()", this );
|
||||
mContext = context;
|
||||
String[] topic = {null};
|
||||
mDevID = XwJNI.dvc_getMQTTDevID( topic );
|
||||
mDevID = XwJNI.dvc_getMQTTDevID( mTopics );
|
||||
Assert.assertTrueNR( 16 == mDevID.length() );
|
||||
mTopic = topic[0];
|
||||
mMsgThread = new MsgThread();
|
||||
|
||||
String host = XWPrefs.getPrefsString( context, R.string.key_mqtt_host );
|
||||
|
@ -522,8 +521,7 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba
|
|||
public void messageArrived( String topic, MqttMessage message ) throws Exception
|
||||
{
|
||||
Log.d( TAG, "%H.messageArrived(topic=%s)", this, topic );
|
||||
Assert.assertTrueNR( topic.equals(mTopic) );
|
||||
mMsgThread.add( message.getPayload() );
|
||||
mMsgThread.add( topic, message.getPayload() );
|
||||
ConnStatusHandler
|
||||
.updateStatusIn( mContext, CommsConnType.COMMS_CONN_MQTT, true );
|
||||
|
||||
|
@ -541,10 +539,13 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba
|
|||
|
||||
private void subscribe()
|
||||
{
|
||||
Assert.assertTrueNR( null != mTopics && 2 == mTopics.length );
|
||||
final int qos = XWPrefs.getPrefsInt( mContext, R.string.key_mqtt_qos, 2 );
|
||||
int qoss[] = { qos, qos };
|
||||
|
||||
setState( State.SUBSCRIBING );
|
||||
try {
|
||||
mClient.subscribe( mTopic, qos, null, this );
|
||||
mClient.subscribe( mTopics, qoss, null, this );
|
||||
// Log.d( TAG, "subscribed to %s", mTopic );
|
||||
} catch ( MqttException ex ) {
|
||||
ex.printStackTrace();
|
||||
|
@ -581,10 +582,10 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba
|
|||
}
|
||||
|
||||
private class MsgThread extends Thread {
|
||||
private LinkedBlockingQueue<byte[]> mQueue = new LinkedBlockingQueue<>();
|
||||
private LinkedBlockingQueue<MessagePair> mQueue = new LinkedBlockingQueue<>();
|
||||
|
||||
void add( byte[] msg ) {
|
||||
mQueue.add( msg );
|
||||
void add( String topic, byte[] msg ) {
|
||||
mQueue.add( new MessagePair( topic, msg ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -594,17 +595,39 @@ public class MQTTUtils extends Thread implements IMqttActionListener, MqttCallba
|
|||
Log.d( TAG, "%H.MsgThread.run() starting", MQTTUtils.this );
|
||||
for ( ; ; ) {
|
||||
try {
|
||||
byte[] packet = mQueue.take();
|
||||
XwJNI.dvc_parseMQTTPacket( packet );
|
||||
MessagePair pair = mQueue.take();
|
||||
String topic = pair.mTopic;
|
||||
if ( topic.equals( mTopics[0] ) ) {
|
||||
XwJNI.dvc_parseMQTTPacket( pair.mPacket );
|
||||
} else if ( topic.equals( mTopics[1] ) ) {
|
||||
postNotification( pair );
|
||||
}
|
||||
} catch ( InterruptedException ie ) {
|
||||
// Assert.failDbg();
|
||||
break;
|
||||
} catch ( JSONException je ) {
|
||||
Log.e( TAG, "run() ex: %s", je );
|
||||
}
|
||||
}
|
||||
long now = Utils.getCurSeconds();
|
||||
Log.d( TAG, "%H.MsgThread.run() exiting after %d seconds", MQTTUtils.this,
|
||||
now - startTime );
|
||||
}
|
||||
|
||||
private void postNotification( MessagePair pair ) throws JSONException
|
||||
{
|
||||
JSONObject obj = new JSONObject( new String(pair.mPacket) );
|
||||
String msg = obj.optString( "msg" );
|
||||
if ( null != msg ) {
|
||||
String title = obj.optString( "title" );
|
||||
if ( null == title ) {
|
||||
title = LocUtils.getString( mContext, R.string.remote_msg_title );
|
||||
}
|
||||
Intent alertIntent = GamesListDelegate.makeAlertIntent( mContext, msg );
|
||||
int code = msg.hashCode() ^ title.hashCode();
|
||||
Utils.postNotification( mContext, alertIntent, title, msg, code );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void handleMessage( Context context, CommsAddrRec from,
|
||||
|
|
|
@ -150,8 +150,7 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
|
|||
params.put( k_APP, appParams );
|
||||
params.put( k_DEVID, XWPrefs.getDevID( context ) );
|
||||
|
||||
String[] topic = {null};
|
||||
String devID = XwJNI.dvc_getMQTTDevID( topic );
|
||||
String devID = XwJNI.dvc_getMQTTDevID( null );
|
||||
params.put( k_MQTTDEVID, devID );
|
||||
} catch ( org.json.JSONException jse ) {
|
||||
Log.ex( TAG, jse );
|
||||
|
|
|
@ -136,9 +136,10 @@ public class XwJNI {
|
|||
cleanGlobals();
|
||||
}
|
||||
|
||||
public static String dvc_getMQTTDevID( String[] topic )
|
||||
public static String dvc_getMQTTDevID( String[] topics )
|
||||
{
|
||||
return dvc_getMQTTDevID( getJNI().m_ptrGlobals, topic );
|
||||
Assert.assertTrueNR( null == topics || 2 == topics.length );
|
||||
return dvc_getMQTTDevID( getJNI().m_ptrGlobals, topics );
|
||||
}
|
||||
|
||||
public static void dvc_resetMQTTDevID()
|
||||
|
|
|
@ -629,7 +629,7 @@ streamFromJStream( MPFORMAL JNIEnv* env, VTableMgr* vtMgr, jbyteArray jstream )
|
|||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_org_eehouse_android_xw4_jni_XwJNI_dvc_1getMQTTDevID
|
||||
( JNIEnv* env, jclass C, jlong jniGlobalPtr, jobjectArray jTopicOut )
|
||||
( JNIEnv* env, jclass C, jlong jniGlobalPtr, jobjectArray jTopicsOut )
|
||||
{
|
||||
jstring result;
|
||||
DVC_HEADER(jniGlobalPtr);
|
||||
|
@ -638,12 +638,18 @@ Java_org_eehouse_android_xw4_jni_XwJNI_dvc_1getMQTTDevID
|
|||
|
||||
XP_UCHAR buf[64];
|
||||
|
||||
if ( !!jTopicOut ) {
|
||||
if ( !!jTopicsOut ) {
|
||||
formatMQTTTopic( &devID, buf, VSIZE(buf) );
|
||||
jstring jtopic = (*env)->NewStringUTF( env, buf );
|
||||
XP_ASSERT( 1 == (*env)->GetArrayLength( env, jTopicOut ) ); /* fired */
|
||||
(*env)->SetObjectArrayElement( env, jTopicOut, 0, jtopic );
|
||||
(*env)->SetObjectArrayElement( env, jTopicsOut, 0, jtopic );
|
||||
deleteLocalRef( env, jtopic );
|
||||
|
||||
if ( 1 < (*env)->GetArrayLength( env, jTopicsOut ) ) {
|
||||
formatMQTTCtrlTopic( &devID, buf, VSIZE(buf) );
|
||||
jstring jtopic = (*env)->NewStringUTF( env, buf );
|
||||
(*env)->SetObjectArrayElement( env, jTopicsOut, 1, jtopic );
|
||||
deleteLocalRef( env, jtopic );
|
||||
}
|
||||
}
|
||||
|
||||
formatMQTTDevID( &devID, buf, VSIZE(buf) );
|
||||
|
|
|
@ -285,6 +285,7 @@ typedef uint64_t MQTTDevID;
|
|||
# define MQTTDevID_FMT "%016llX"
|
||||
#endif
|
||||
# define MQTTTopic_FMT "xw4/device/" MQTTDevID_FMT
|
||||
# define MQTTCtrlTopic_FMT "xw4/msg/" MQTTDevID_FMT
|
||||
|
||||
/* Used by scoring code and engine as fast representation of moves. */
|
||||
typedef struct _MoveInfoTile {
|
||||
|
|
|
@ -621,6 +621,13 @@ formatMQTTTopic( const MQTTDevID* devid, XP_UCHAR* buf, XP_U16 bufLen )
|
|||
return buf;
|
||||
}
|
||||
|
||||
const XP_UCHAR*
|
||||
formatMQTTCtrlTopic( const MQTTDevID* devid, XP_UCHAR* buf, XP_U16 bufLen )
|
||||
{
|
||||
XP_SNPRINTF( buf, bufLen, MQTTCtrlTopic_FMT, *devid );
|
||||
return buf;
|
||||
}
|
||||
|
||||
XP_Bool
|
||||
strToMQTTCDevID( const XP_UCHAR* str, MQTTDevID* result )
|
||||
{
|
||||
|
|
|
@ -115,6 +115,7 @@ XP_Bool smsToBin( XP_U8* out, XP_U16* outlen, const XP_UCHAR* in, XP_U16 inlen )
|
|||
#endif
|
||||
|
||||
const XP_UCHAR* formatMQTTTopic( const MQTTDevID* devid, XP_UCHAR* buf, XP_U16 bufLen );
|
||||
const XP_UCHAR* formatMQTTCtrlTopic( const MQTTDevID* devid, XP_UCHAR* buf, XP_U16 bufLen );
|
||||
const XP_UCHAR* formatMQTTDevID( const MQTTDevID* devid, XP_UCHAR* buf, XP_U16 bufLen );
|
||||
XP_Bool strToMQTTCDevID( const XP_UCHAR* str, MQTTDevID* result );
|
||||
|
||||
|
|
Loading…
Reference in a new issue