ping remote BT hosts on game open and on radio on in order to update

the status of the connection arrows.
This commit is contained in:
Eric House 2014-09-21 13:06:12 -07:00
parent 4695619c63
commit 0d04fd138d
3 changed files with 113 additions and 46 deletions

View file

@ -66,6 +66,7 @@ public class BTService extends XWService {
private static final int CLEAR = 5; private static final int CLEAR = 5;
private static final int REMOVE = 6; private static final int REMOVE = 6;
private static final int NFCINVITE = 7; private static final int NFCINVITE = 7;
private static final int PINGHOST = 8;
private static final String CMD_STR = "CMD"; private static final String CMD_STR = "CMD";
private static final String MSG_STR = "MSG"; private static final String MSG_STR = "MSG";
@ -128,6 +129,10 @@ public class BTService extends XWService {
m_msg = buf; m_recipient = targetName; m_msg = buf; m_recipient = targetName;
m_addr = targetAddr; m_gameID = gameID; m_addr = targetAddr; m_gameID = gameID;
} }
public BTQueueElem( BTCmd cmd, String targetName ) {
this( cmd );
m_recipient = targetName;
}
public int incrFailCount() { return ++m_failCount; } public int incrFailCount() { return ++m_failCount; }
public boolean failCountExceeded() { return m_failCount >= MAX_SEND_FAIL; } public boolean failCountExceeded() { return m_failCount >= MAX_SEND_FAIL; }
@ -180,6 +185,14 @@ public class BTService extends XWService {
context.startService( intent ); context.startService( intent );
} }
public static void pingHost( Context context, String hostName, String hostAddr )
{
Intent intent = getIntentTo( context, PINGHOST );
intent.putExtra( TARGET_STR, hostName );
intent.putExtra( ADDR_STR, hostAddr );
context.startService( intent );
}
public static void inviteRemote( Context context, String hostName, public static void inviteRemote( Context context, String hostName,
int gameID, String initialName, int gameID, String initialName,
int lang, String dict, int nPlayersT, int lang, String dict, int nPlayersT,
@ -296,6 +309,11 @@ public class BTService extends XWService {
dict, nPlayersT, nPlayersH ) ); dict, nPlayersT, nPlayersH ) );
break; break;
case PINGHOST:
target = intent.getStringExtra( TARGET_STR );
m_sender.add( new BTQueueElem( BTCmd.PING, target ) );
break;
case NFCINVITE: case NFCINVITE:
gameID = intent.getIntExtra( GAMEID_STR, -1 ); gameID = intent.getIntExtra( GAMEID_STR, -1 );
lang = intent.getIntExtra( LANG_STR, -1 ); lang = intent.getIntExtra( LANG_STR, -1 );
@ -439,6 +457,7 @@ public class BTService extends XWService {
os.flush(); os.flush();
socket.close(); socket.close();
updateStatusOut( true );
} }
private void receiveInvitation( Context context, private void receiveInvitation( Context context,
@ -627,7 +646,11 @@ public class BTService extends XWService {
switch( elem.m_cmd ) { switch( elem.m_cmd ) {
case PING: case PING:
if ( null == elem.m_recipient ) {
sendPings( MultiEvent.HOST_PONGED ); sendPings( MultiEvent.HOST_PONGED );
} else {
sendPing( elem.m_recipient );
}
break; break;
case SCAN: case SCAN:
sendPings( null ); sendPings( null );
@ -662,7 +685,21 @@ public class BTService extends XWService {
if ( null != addrFor( name ) ) { if ( null != addrFor( name ) ) {
continue; continue;
} }
if ( sendPing( dev ) ) {
addAddr( dev );
if ( null != event ) {
sendResult( event, dev.getName() );
}
}
}
}
private boolean sendPing( BluetoothDevice dev )
{
boolean success = false; boolean success = false;
boolean sendWorking = false;
boolean receiveWorking = false;
try { try {
DbgUtils.logf( "PingThread: got socket to device %s", DbgUtils.logf( "PingThread: got socket to device %s",
dev.getName() ); dev.getName() );
@ -677,23 +714,32 @@ public class BTService extends XWService {
DataInputStream is = DataInputStream is =
new DataInputStream( socket.getInputStream() ); new DataInputStream( socket.getInputStream() );
success = BTCmd.PONG == BTCmd.values()[is.readByte()]; success = BTCmd.PONG == BTCmd.values()[is.readByte()];
receiveWorking = true;
killer.interrupt(); killer.interrupt();
sendWorking = true;
} }
socket.close(); socket.close();
} }
} catch ( IOException ioe ) { } catch ( IOException ioe ) {
logIOE( ioe ); logIOE( ioe );
} }
updateStatusOut( sendWorking );
updateStatusIn( receiveWorking );
return success;
} // sendPing
if ( success ) { private boolean sendPing( String hostName )
DbgUtils.logf( "got PONG from %s", dev.getName() ); {
addAddr( dev ); boolean success = false;
if ( null != event ) { String hostAddr = addrFor( hostName );
sendResult( event, dev.getName() ); if ( null == hostAddr ) {
DbgUtils.logf( "sendPing: no addr for hostname %s; dropping", hostName );
} else {
BluetoothDevice dev = m_adapter.getRemoteDevice( hostAddr );
success = sendPing( dev );
} }
return success;
} }
}
} // sendPings
private void sendInvite( BTQueueElem elem ) private void sendInvite( BTQueueElem elem )
{ {

View file

@ -1040,6 +1040,10 @@ public class BoardDelegate extends DelegateBase
} }
break; break;
case BT_ENABLED:
pingBTRemotes();
break;
// This can be BT or SMS. In BT case there's a progress // This can be BT or SMS. In BT case there's a progress
// thing going. Not in SMS case. // thing going. Not in SMS case.
case NEWGAME_FAILURE: case NEWGAME_FAILURE:
@ -1634,6 +1638,8 @@ public class BoardDelegate extends DelegateBase
} }
} // userError } // userError
// Called from server_makeFromStream, whether there's something
// missing or not.
@Override @Override
public void informMissing( boolean isServer, CommsConnType connType, public void informMissing( boolean isServer, CommsConnType connType,
final int nMissingPlayers ) final int nMissingPlayers )
@ -1914,7 +1920,7 @@ public class BoardDelegate extends DelegateBase
if ( null != m_xport ) { if ( null != m_xport ) {
warnIfNoTransport(); warnIfNoTransport();
trySendChats(); trySendChats();
m_xport.tickle( m_connType ); tickle();
tryInvites(); tryInvites();
} }
} }
@ -1925,6 +1931,38 @@ public class BoardDelegate extends DelegateBase
} }
} // loadGame } // loadGame
@SuppressWarnings("fallthrough")
private void tickle()
{
switch( m_connType ) {
case COMMS_CONN_BT:
pingBTRemotes();
// fallthrough
case COMMS_CONN_RELAY:
// break; // Try skipping the resend -- later
// fallthrough
case COMMS_CONN_SMS:
// Let other know I'm here
// DbgUtils.logf( "tickle calling comms_resendAll" );
m_jniThread.handle( JNIThread.JNICmd.CMD_RESEND, false, true );
break;
default:
DbgUtils.logf( "tickle: unexpected type %s",
m_connType.toString() );
Assert.fail();
}
}
private void pingBTRemotes()
{
if ( CommsConnType.COMMS_CONN_BT == m_connType ) {
CommsAddrRec[] addrs = XwJNI.comms_getAddrs( m_jniGamePtr );
for ( CommsAddrRec addr : addrs ) {
BTService.pingHost( m_activity, addr.bt_hostName, addr.bt_btAddr );
}
}
}
private void checkAndHandle( JNICmd cmd ) private void checkAndHandle( JNICmd cmd )
{ {
if ( null != m_jniThread ) { if ( null != m_jniThread ) {

View file

@ -228,7 +228,9 @@ public class CommsTransport implements TransportProcs,
NetStateCache.unregister( m_context, this ); NetStateCache.unregister( m_context, this );
} }
//////////////////////////////////////////////////////////////////////
// NetStateCache.StateChangedIf interface // NetStateCache.StateChangedIf interface
//////////////////////////////////////////////////////////////////////
public void netAvail( boolean nowAvailable ) public void netAvail( boolean nowAvailable )
{ {
if ( !nowAvailable ) { if ( !nowAvailable ) {
@ -237,25 +239,6 @@ public class CommsTransport implements TransportProcs,
} }
} }
public void tickle( CommsConnType connType )
{
switch( connType ) {
case COMMS_CONN_RELAY:
// do nothing
// break; // Try skipping the resend -- later
case COMMS_CONN_BT:
case COMMS_CONN_SMS:
// Let other know I'm here
DbgUtils.logf( "tickle calling comms_resendAll" );
m_jniThread.handle( JNIThread.JNICmd.CMD_RESEND, false, true );
break;
default:
DbgUtils.logf( "tickle: unexpected type %s",
connType.toString() );
Assert.fail();
}
}
private synchronized void putOut( final byte[] buf ) private synchronized void putOut( final byte[] buf )
{ {
if ( !XWApp.UDP_ENABLED ) { if ( !XWApp.UDP_ENABLED ) {