offer to rematch via relay too, but leave the server communication

part stubbed out. I'm disabling rematch for now because there are
several issues to be worked out, chief among them how to handle
simultaneous receipt of identical invitations (via different
channels.)
This commit is contained in:
Eric House 2015-03-02 06:28:42 -08:00
parent cdf7a88302
commit a003cf686f
3 changed files with 92 additions and 34 deletions

View file

@ -2391,7 +2391,8 @@ public class BoardDelegate extends DelegateBase
supported = m_gi.serverRole == DeviceRole.SERVER_STANDALONE; supported = m_gi.serverRole == DeviceRole.SERVER_STANDALONE;
if ( !supported && 2 == m_gi.nPlayers ) { if ( !supported && 2 == m_gi.nPlayers ) {
supported = m_connTypes.contains( CommsConnType.COMMS_CONN_BT ) supported = m_connTypes.contains( CommsConnType.COMMS_CONN_BT )
|| m_connTypes.contains( CommsConnType.COMMS_CONN_SMS ); || m_connTypes.contains( CommsConnType.COMMS_CONN_SMS )
|| m_connTypes.contains( CommsConnType.COMMS_CONN_RELAY );
} }
} }
return supported; return supported;
@ -2401,6 +2402,7 @@ public class BoardDelegate extends DelegateBase
{ {
String phone = null; String phone = null;
String btAddr = null; String btAddr = null;
String relayID = null;
if ( m_gi.serverRole != DeviceRole.SERVER_STANDALONE ) { if ( m_gi.serverRole != DeviceRole.SERVER_STANDALONE ) {
CommsAddrRec[] addrs = XwJNI.comms_getAddrs( m_jniGamePtr ); CommsAddrRec[] addrs = XwJNI.comms_getAddrs( m_jniGamePtr );
for ( CommsAddrRec addr : addrs ) { for ( CommsAddrRec addr : addrs ) {
@ -2412,12 +2414,16 @@ public class BoardDelegate extends DelegateBase
Assert.assertNull( phone ); Assert.assertNull( phone );
phone = addr.sms_phone; phone = addr.sms_phone;
} }
if ( addr.contains( CommsConnType.COMMS_CONN_RELAY ) ) {
Assert.assertNull( relayID );
relayID = m_summary.relayID;
}
} }
} }
Intent intent = GamesListDelegate Intent intent = GamesListDelegate
.makeRematchIntent( m_activity, m_rowid, m_connTypes, btAddr, .makeRematchIntent( m_activity, m_rowid, m_connTypes, btAddr,
phone ); phone, relayID );
if ( null != intent ) { if ( null != intent ) {
startActivity( intent ); startActivity( intent );
m_delegator.finish(); m_delegator.finish();
@ -2446,6 +2452,11 @@ public class BoardDelegate extends DelegateBase
if ( null != value ) { if ( null != value ) {
BTService.inviteRemote( m_activity, value, nli ); BTService.inviteRemote( m_activity, value, nli );
} }
value = m_summary.getStringExtra( GameSummary.EXTRA_REMATCH_RELAY );
if ( null != value ) {
RelayService.inviteRemote( m_activity, value, nli );
}
} }
} }

View file

@ -87,6 +87,7 @@ public class GamesListDelegate extends ListDelegateBase
private static final String REMATCH_ADDRS_EXTRA = "rm_addrs"; private static final String REMATCH_ADDRS_EXTRA = "rm_addrs";
private static final String REMATCH_BTADDR_EXTRA = "rm_btaddr"; private static final String REMATCH_BTADDR_EXTRA = "rm_btaddr";
private static final String REMATCH_PHONE_EXTRA = "rm_phone"; private static final String REMATCH_PHONE_EXTRA = "rm_phone";
private static final String REMATCH_RELAYID_EXTRA = "rm_relayid";
private static final String ALERT_MSG = "alert_msg"; private static final String ALERT_MSG = "alert_msg";
@ -1140,6 +1141,7 @@ public class GamesListDelegate extends ListDelegateBase
if ( AlertDialog.BUTTON_POSITIVE == which ) { if ( AlertDialog.BUTTON_POSITIVE == which ) {
switch( action ) { switch( action ) {
case NEW_NET_GAME: case NEW_NET_GAME:
m_netLaunchInfo = (NetLaunchInfo)params[0];
if ( checkWarnNoDict( m_netLaunchInfo ) ) { if ( checkWarnNoDict( m_netLaunchInfo ) ) {
makeNewNetGameIf(); makeNewNetGameIf();
} }
@ -1811,8 +1813,9 @@ public class GamesListDelegate extends ListDelegateBase
if ( -1 != rowid ) { if ( -1 != rowid ) {
String btAddr = intent.getStringExtra( REMATCH_BTADDR_EXTRA ); String btAddr = intent.getStringExtra( REMATCH_BTADDR_EXTRA );
String phone = intent.getStringExtra( REMATCH_PHONE_EXTRA ); String phone = intent.getStringExtra( REMATCH_PHONE_EXTRA );
String relayID = intent.getStringExtra( REMATCH_RELAYID_EXTRA );
long newid; long newid;
if ( null == btAddr && null == phone ) { if ( null == btAddr && null == phone && null == relayID ) {
// this will juggle if the preference is set // this will juggle if the preference is set
newid = GameUtils.dupeGame( m_activity, rowid ); newid = GameUtils.dupeGame( m_activity, rowid );
} else { } else {
@ -1821,7 +1824,7 @@ public class GamesListDelegate extends ListDelegateBase
newid = GameUtils.makeNewMultiGame( m_activity, addrs ); newid = GameUtils.makeNewMultiGame( m_activity, addrs );
DBUtils.addRematchInfo( m_activity, newid, btAddr, phone, DBUtils.addRematchInfo( m_activity, newid, btAddr, phone,
null ); relayID );
} }
launchGame( newid ); launchGame( newid );
} }
@ -2169,7 +2172,8 @@ public class GamesListDelegate extends ListDelegateBase
public static Intent makeRematchIntent( Context context, long rowid, public static Intent makeRematchIntent( Context context, long rowid,
CommsConnTypeSet addrTypes, CommsConnTypeSet addrTypes,
String btAddr, String phone ) String btAddr, String phone,
String relayID )
{ {
DbgUtils.logf( "makeRematchIntent(btAddr=%s; phone=%s)", btAddr, phone ); DbgUtils.logf( "makeRematchIntent(btAddr=%s; phone=%s)", btAddr, phone );
Intent intent = makeSelfIntent( context ); Intent intent = makeSelfIntent( context );
@ -2181,6 +2185,9 @@ public class GamesListDelegate extends ListDelegateBase
if ( null != phone ) { if ( null != phone ) {
intent.putExtra( REMATCH_PHONE_EXTRA, phone ); intent.putExtra( REMATCH_PHONE_EXTRA, phone );
} }
if ( null != relayID ) {
intent.putExtra( REMATCH_RELAYID_EXTRA, relayID );
}
return intent; return intent;
} }

View file

@ -66,20 +66,22 @@ public class RelayService extends XWService
private static final String CMD_STR = "CMD"; private static final String CMD_STR = "CMD";
private static enum MsgCmds { INVALID private static enum MsgCmds { INVALID,
, PROCESS_GAME_MSGS PROCESS_GAME_MSGS,
, PROCESS_DEV_MSGS PROCESS_DEV_MSGS,
, UDP_CHANGED UDP_CHANGED,
, SEND SEND,
, SENDNOCONN SENDNOCONN,
, RECEIVE RECEIVE,
, TIMER_FIRED TIMER_FIRED,
, RESET RESET,
, UPGRADE UPGRADE,
INVITE,
} }
private static final String MSGS_ARR = "MSGS_ARR"; private static final String MSGS_ARR = "MSGS_ARR";
private static final String RELAY_ID = "RELAY_ID"; private static final String RELAY_ID = "RELAY_ID";
private static final String NLI_DATA = "NLI_DATA";
private static final String ROWID = "ROWID"; private static final String ROWID = "ROWID";
private static final String BINBUFFER = "BINBUFFER"; private static final String BINBUFFER = "BINBUFFER";
@ -111,23 +113,23 @@ public class RelayService extends XWService
// private static final int XWPDEV_NONE = 0; // private static final int XWPDEV_NONE = 0;
// Must be kept in sync with eponymous enum in xwrelay.h // Must be kept in sync with eponymous enum in xwrelay.h
private enum XWRelayReg { private enum XWRelayReg { XWPDEV_NONE,
XWPDEV_NONE XWPDEV_UNAVAIL,
,XWPDEV_UNAVAIL XWPDEV_REG,
,XWPDEV_REG XWPDEV_REGRSP,
,XWPDEV_REGRSP XWPDEV_KEEPALIVE,
,XWPDEV_KEEPALIVE XWPDEV_HAVEMSGS,
,XWPDEV_HAVEMSGS XWPDEV_RQSTMSGS,
,XWPDEV_RQSTMSGS XWPDEV_MSG,
,XWPDEV_MSG XWPDEV_MSGNOCONN,
,XWPDEV_MSGNOCONN XWPDEV_MSGRSP,
,XWPDEV_MSGRSP XWPDEV_BADREG,
,XWPDEV_BADREG XWPDEV_ACK,
,XWPDEV_ACK XWPDEV_DELGAME,
,XWPDEV_DELGAME XWPDEV_ALERT,
,XWPDEV_ALERT XWPDEV_UPGRADE,
,XWPDEV_UPGRADE XWPDEV_MSGFWDOTHERS,
}; };
public static void gcmConfirmed( Context context, boolean confirmed ) public static void gcmConfirmed( Context context, boolean confirmed )
{ {
@ -152,6 +154,14 @@ public class RelayService extends XWService
context.startService( intent ); context.startService( intent );
} }
public static void inviteRemote( Context context, String relayID,
NetLaunchInfo nli )
{
context.startService( getIntentTo( context, MsgCmds.INVITE )
.putExtra( RELAY_ID, relayID )
.putExtra( NLI_DATA, nli.toString() ) );
}
public static void reset( Context context ) public static void reset( Context context )
{ {
Intent intent = getIntentTo( context, MsgCmds.RESET ); Intent intent = getIntentTo( context, MsgCmds.RESET );
@ -315,15 +325,21 @@ public class RelayService extends XWService
startUDPThreadsIfNot(); startUDPThreadsIfNot();
long rowid = intent.getLongExtra( ROWID, -1 ); long rowid = intent.getLongExtra( ROWID, -1 );
byte[] msg = intent.getByteArrayExtra( BINBUFFER ); byte[] msg = intent.getByteArrayExtra( BINBUFFER );
if ( MsgCmds.SEND.equals( cmd ) ) { if ( MsgCmds.SEND == cmd ) {
sendMessage( rowid, msg ); sendMessage( rowid, msg );
} else if ( MsgCmds.SENDNOCONN.equals( cmd ) ) { } else if ( MsgCmds.SENDNOCONN == cmd ) {
String relayID = intent.getStringExtra( RELAY_ID ); String relayID = intent.getStringExtra( RELAY_ID );
sendNoConnMessage( rowid, relayID, msg ); sendNoConnMessage( rowid, relayID, msg );
} else { } else {
feedMessage( rowid, msg ); feedMessage( rowid, msg );
} }
break; break;
case INVITE:
startUDPThreadsIfNot();
String relayID = intent.getStringExtra( RELAY_ID );
String nliData = intent.getStringExtra( NLI_DATA );
sendForwardOthersMessage( relayID, nliData );
break;
case TIMER_FIRED: case TIMER_FIRED:
if ( !NetStateCache.netAvail( this ) ) { if ( !NetStateCache.netAvail( this ) ) {
DbgUtils.logf( "not connecting: no network" ); DbgUtils.logf( "not connecting: no network" );
@ -618,6 +634,11 @@ public class RelayService extends XWService
case XWPDEV_ACK: case XWPDEV_ACK:
noteAck( vli2un( dis ) ); noteAck( vli2un( dis ) );
break; break;
// case XWPDEV_MSGFWDOTHERS:
// Assert.assertTrue( 0 == dis.readByte() ); // protocol; means "invite", I guess.
// String nliData = dis.readUTF();
// DbgUtils.logf( "RelayService: got invite: %s", nliData );
// break;
default: default:
DbgUtils.logf( "RelayService.gotPacket(): Unhandled cmd" ); DbgUtils.logf( "RelayService.gotPacket(): Unhandled cmd" );
break; break;
@ -766,6 +787,23 @@ public class RelayService extends XWService
} }
} }
private void sendForwardOthersMessage( String relayID, String nliData )
{
DbgUtils.logf( "sendForwardOthersMessage(relayID=%s); NOT IMPLEMENTED",
relayID );
// ByteArrayOutputStream bas = new ByteArrayOutputStream();
// try {
// DataOutputStream out = new DataOutputStream( bas );
// out.writeBytes( relayID );
// out.write( '\n' ); // terminator
// out.writeByte( 0 ); // protocol; means "invite", I guess.
// out.writeUTF( nliData );
// postPacket( bas, XWRelayReg.XWPDEV_MSGFWDOTHERS );
// } catch ( java.io.IOException ioe ) {
// DbgUtils.loge( ioe );
// }
}
private void sendAckIf( PacketHeader header ) private void sendAckIf( PacketHeader header )
{ {
if ( 0 != header.m_packetID ) { if ( 0 != header.m_packetID ) {
@ -958,6 +996,8 @@ public class RelayService extends XWService
if ( msgLen + thisLen > MAX_BUF ) { if ( msgLen + thisLen > MAX_BUF ) {
// Need to deal with this case by sending multiple // Need to deal with this case by sending multiple
// packets. It WILL happen. // packets. It WILL happen.
DbgUtils.logf( "dropping send for lack of space; FIX ME!!" );
Assert.fail();
break; break;
} }
// got space; now write it // got space; now write it