From a003cf686fd0a789a82accd54d42ba8bfc40eb23 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 2 Mar 2015 06:28:42 -0800 Subject: [PATCH] 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.) --- .../eehouse/android/xw4/BoardDelegate.java | 15 ++- .../android/xw4/GamesListDelegate.java | 13 ++- .../org/eehouse/android/xw4/RelayService.java | 98 +++++++++++++------ 3 files changed, 92 insertions(+), 34 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java index 56323b32e..835841087 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java @@ -2391,7 +2391,8 @@ public class BoardDelegate extends DelegateBase supported = m_gi.serverRole == DeviceRole.SERVER_STANDALONE; if ( !supported && 2 == m_gi.nPlayers ) { 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; @@ -2401,6 +2402,7 @@ public class BoardDelegate extends DelegateBase { String phone = null; String btAddr = null; + String relayID = null; if ( m_gi.serverRole != DeviceRole.SERVER_STANDALONE ) { CommsAddrRec[] addrs = XwJNI.comms_getAddrs( m_jniGamePtr ); for ( CommsAddrRec addr : addrs ) { @@ -2412,12 +2414,16 @@ public class BoardDelegate extends DelegateBase Assert.assertNull( phone ); phone = addr.sms_phone; } + if ( addr.contains( CommsConnType.COMMS_CONN_RELAY ) ) { + Assert.assertNull( relayID ); + relayID = m_summary.relayID; + } } } Intent intent = GamesListDelegate .makeRematchIntent( m_activity, m_rowid, m_connTypes, btAddr, - phone ); + phone, relayID ); if ( null != intent ) { startActivity( intent ); m_delegator.finish(); @@ -2446,6 +2452,11 @@ public class BoardDelegate extends DelegateBase if ( null != value ) { BTService.inviteRemote( m_activity, value, nli ); } + + value = m_summary.getStringExtra( GameSummary.EXTRA_REMATCH_RELAY ); + if ( null != value ) { + RelayService.inviteRemote( m_activity, value, nli ); + } } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java index 2064ae184..4a89f649f 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java @@ -87,6 +87,7 @@ public class GamesListDelegate extends ListDelegateBase private static final String REMATCH_ADDRS_EXTRA = "rm_addrs"; private static final String REMATCH_BTADDR_EXTRA = "rm_btaddr"; 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"; @@ -1140,6 +1141,7 @@ public class GamesListDelegate extends ListDelegateBase if ( AlertDialog.BUTTON_POSITIVE == which ) { switch( action ) { case NEW_NET_GAME: + m_netLaunchInfo = (NetLaunchInfo)params[0]; if ( checkWarnNoDict( m_netLaunchInfo ) ) { makeNewNetGameIf(); } @@ -1811,8 +1813,9 @@ public class GamesListDelegate extends ListDelegateBase if ( -1 != rowid ) { String btAddr = intent.getStringExtra( REMATCH_BTADDR_EXTRA ); String phone = intent.getStringExtra( REMATCH_PHONE_EXTRA ); + String relayID = intent.getStringExtra( REMATCH_RELAYID_EXTRA ); long newid; - if ( null == btAddr && null == phone ) { + if ( null == btAddr && null == phone && null == relayID ) { // this will juggle if the preference is set newid = GameUtils.dupeGame( m_activity, rowid ); } else { @@ -1821,7 +1824,7 @@ public class GamesListDelegate extends ListDelegateBase newid = GameUtils.makeNewMultiGame( m_activity, addrs ); DBUtils.addRematchInfo( m_activity, newid, btAddr, phone, - null ); + relayID ); } launchGame( newid ); } @@ -2169,7 +2172,8 @@ public class GamesListDelegate extends ListDelegateBase public static Intent makeRematchIntent( Context context, long rowid, CommsConnTypeSet addrTypes, - String btAddr, String phone ) + String btAddr, String phone, + String relayID ) { DbgUtils.logf( "makeRematchIntent(btAddr=%s; phone=%s)", btAddr, phone ); Intent intent = makeSelfIntent( context ); @@ -2181,6 +2185,9 @@ public class GamesListDelegate extends ListDelegateBase if ( null != phone ) { intent.putExtra( REMATCH_PHONE_EXTRA, phone ); } + if ( null != relayID ) { + intent.putExtra( REMATCH_RELAYID_EXTRA, relayID ); + } return intent; } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java index 109cbc538..0ae29286f 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java @@ -66,20 +66,22 @@ public class RelayService extends XWService private static final String CMD_STR = "CMD"; - private static enum MsgCmds { INVALID - , PROCESS_GAME_MSGS - , PROCESS_DEV_MSGS - , UDP_CHANGED - , SEND - , SENDNOCONN - , RECEIVE - , TIMER_FIRED - , RESET - , UPGRADE + private static enum MsgCmds { INVALID, + PROCESS_GAME_MSGS, + PROCESS_DEV_MSGS, + UDP_CHANGED, + SEND, + SENDNOCONN, + RECEIVE, + TIMER_FIRED, + RESET, + UPGRADE, + INVITE, } private static final String MSGS_ARR = "MSGS_ARR"; 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 BINBUFFER = "BINBUFFER"; @@ -111,23 +113,23 @@ public class RelayService extends XWService // private static final int XWPDEV_NONE = 0; // Must be kept in sync with eponymous enum in xwrelay.h - private enum XWRelayReg { - XWPDEV_NONE - ,XWPDEV_UNAVAIL - ,XWPDEV_REG - ,XWPDEV_REGRSP - ,XWPDEV_KEEPALIVE - ,XWPDEV_HAVEMSGS - ,XWPDEV_RQSTMSGS - ,XWPDEV_MSG - ,XWPDEV_MSGNOCONN - ,XWPDEV_MSGRSP - ,XWPDEV_BADREG - ,XWPDEV_ACK - ,XWPDEV_DELGAME - ,XWPDEV_ALERT - ,XWPDEV_UPGRADE - }; + private enum XWRelayReg { XWPDEV_NONE, + XWPDEV_UNAVAIL, + XWPDEV_REG, + XWPDEV_REGRSP, + XWPDEV_KEEPALIVE, + XWPDEV_HAVEMSGS, + XWPDEV_RQSTMSGS, + XWPDEV_MSG, + XWPDEV_MSGNOCONN, + XWPDEV_MSGRSP, + XWPDEV_BADREG, + XWPDEV_ACK, + XWPDEV_DELGAME, + XWPDEV_ALERT, + XWPDEV_UPGRADE, + XWPDEV_MSGFWDOTHERS, + }; public static void gcmConfirmed( Context context, boolean confirmed ) { @@ -152,6 +154,14 @@ public class RelayService extends XWService 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 ) { Intent intent = getIntentTo( context, MsgCmds.RESET ); @@ -315,15 +325,21 @@ public class RelayService extends XWService startUDPThreadsIfNot(); long rowid = intent.getLongExtra( ROWID, -1 ); byte[] msg = intent.getByteArrayExtra( BINBUFFER ); - if ( MsgCmds.SEND.equals( cmd ) ) { + if ( MsgCmds.SEND == cmd ) { sendMessage( rowid, msg ); - } else if ( MsgCmds.SENDNOCONN.equals( cmd ) ) { + } else if ( MsgCmds.SENDNOCONN == cmd ) { String relayID = intent.getStringExtra( RELAY_ID ); sendNoConnMessage( rowid, relayID, msg ); } else { feedMessage( rowid, msg ); } break; + case INVITE: + startUDPThreadsIfNot(); + String relayID = intent.getStringExtra( RELAY_ID ); + String nliData = intent.getStringExtra( NLI_DATA ); + sendForwardOthersMessage( relayID, nliData ); + break; case TIMER_FIRED: if ( !NetStateCache.netAvail( this ) ) { DbgUtils.logf( "not connecting: no network" ); @@ -618,6 +634,11 @@ public class RelayService extends XWService case XWPDEV_ACK: noteAck( vli2un( dis ) ); 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: DbgUtils.logf( "RelayService.gotPacket(): Unhandled cmd" ); 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 ) { if ( 0 != header.m_packetID ) { @@ -958,6 +996,8 @@ public class RelayService extends XWService if ( msgLen + thisLen > MAX_BUF ) { // Need to deal with this case by sending multiple // packets. It WILL happen. + DbgUtils.logf( "dropping send for lack of space; FIX ME!!" ); + Assert.fail(); break; } // got space; now write it