turn net state listening back on for MQTT

I'd removed it when the relay went away, but MQTT apparently doesn't
listen for network changes on its own and so there was no attempt to
resend messages when the device regained connectivity. Now there
is. Old logic to avoid thrashing remains and is not tested with MQTT
-- shouldn't need to be....
This commit is contained in:
Eric House 2022-08-14 11:38:42 -07:00
parent e46fd52e00
commit 2638533e6f
3 changed files with 56 additions and 33 deletions

View file

@ -1,7 +1,7 @@
/* -*- compile-command: "find-and-gradle.sh inXw4dDeb"; -*- */
/*
* Copyright 2009 - 2020 by Eric House (xwords@eehouse.org). All
* rights reserved.
* Copyright 2009 - 2022 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@ -97,9 +97,22 @@ public class MQTTUtils extends Thread
}
};
private static NetStateCache.StateChangedIf sStateChangedIf =
new NetStateCache.StateChangedIf() {
@Override
public void onNetAvail( Context context, boolean nowAvailable ) {
Log.d( TAG, "onNetAvail(avail=%b)", nowAvailable );
DbgUtils.assertOnUIThread();
if ( nowAvailable ) {
GameUtils.resendAllIf( context, CommsConnType.COMMS_CONN_MQTT );
}
}
};
public static void init( Context context )
{
Log.d( TAG, "init()" );
NetStateCache.register( context, sStateChangedIf );
getOrStart( context );
}
@ -107,6 +120,12 @@ public class MQTTUtils extends Thread
{
Log.d( TAG, "onResume()" );
getOrStart( context );
NetStateCache.register( context, sStateChangedIf );
}
public static void onDestroy( Context context )
{
NetStateCache.unregister( context, sStateChangedIf );
}
public static void setEnabled( Context context, boolean enabled )

View file

@ -1,6 +1,6 @@
/* -*- compile-command: "find-and-gradle.sh inXw4dDeb"; -*- */
/*
* Copyright 2010 by Eric House (xwords@eehouse.org). All rights
* Copyright 2010 - 2022 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
@ -33,6 +33,7 @@ import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
public class NetStateCache {
@ -41,10 +42,10 @@ public class NetStateCache {
// I'm leaving this stuff commented out because MQTT might want to use it
// to try resending when the net comes back.
// public interface StateChangedIf {
// public void onNetAvail( boolean nowAvailable );
// }
// private static HashSet<StateChangedIf> s_ifs;
public interface StateChangedIf {
public void onNetAvail( Context context, boolean nowAvailable );
}
private static Set<StateChangedIf> s_ifs = new HashSet<>();
private static AtomicBoolean s_haveReceiver = new AtomicBoolean( false );
private static boolean s_netAvail = false;
@ -52,25 +53,26 @@ public class NetStateCache {
private static PvtBroadcastReceiver s_receiver;
private static final boolean s_onSDKSim = Build.PRODUCT.contains("sdk"); // not genymotion
// public static void register( Context context, StateChangedIf proc )
// {
// if ( Utils.isOnUIThread() ) {
// initIfNot( context );
// synchronized( s_ifs ) {
// s_ifs.add( proc );
// }
// }
// }
public static void register( Context context, StateChangedIf proc )
{
DbgUtils.assertOnUIThread();
if ( Utils.isOnUIThread() ) {
initIfNot( context );
synchronized( s_ifs ) {
s_ifs.add( proc );
}
}
}
// public static void unregister( Context context, StateChangedIf proc )
// {
// if ( Utils.isOnUIThread() ) {
// initIfNot( context );
// synchronized( s_ifs ) {
// s_ifs.remove( proc );
// }
// }
// }
public static void unregister( Context context, StateChangedIf proc )
{
DbgUtils.assertOnUIThread();
if ( Utils.isOnUIThread() ) {
synchronized( s_ifs ) {
s_ifs.remove( proc );
}
}
}
static long s_lastNetCheck = 0;
public static boolean netAvail( Context context )
@ -144,8 +146,7 @@ public class NetStateCache {
NetworkInfo ni = connMgr.getActiveNetworkInfo();
s_netAvail = ni != null && ni.isAvailable() && ni.isConnected();
// DbgUtils.logf( "NetStateCache.initIfNot(): set s_netAvail = %b",
// s_netAvail );
// Log.d( TAG, "set s_netAvail = %b", s_netAvail );
s_receiver = new PvtBroadcastReceiver();
IntentFilter filter = new IntentFilter();
@ -252,12 +253,12 @@ public class NetStateCache {
Log.i( TAG, "notifyStateChanged(%b)", s_netAvail );
// synchronized( s_ifs ) {
// Iterator<StateChangedIf> iter = s_ifs.iterator();
// while ( iter.hasNext() ) {
// iter.next().onNetAvail( s_netAvail );
// }
// }
synchronized( s_ifs ) {
Iterator<StateChangedIf> iter = s_ifs.iterator();
while ( iter.hasNext() ) {
iter.next().onNetAvail( context, s_netAvail );
}
}
if ( s_netAvail ) {
CommsConnType typ = CommsConnType

View file

@ -112,6 +112,9 @@ public class XWApp extends Application
case ON_STOP:
BTUtils.onStop( this );
break;
case ON_DESTROY:
MQTTUtils.onDestroy( this );
break;
}
}