make comms choose channel for invitations

Since server will ultimately assign them as registrations come in
there's no point in trying to force an order earlier.
This commit is contained in:
Eric House 2024-07-27 08:06:31 -07:00
parent 7d4cfa5e26
commit d61b4ce485
12 changed files with 81 additions and 38 deletions

View file

@ -1187,8 +1187,7 @@ class BoardDelegate(delegator: Delegator) :
InviteMeans.SMS_USER, InviteMeans.EMAIL, InviteMeans.CLIPBOARD -> {
val nli = NetLaunchInfo(
mActivity, mSummary!!, mGi!!,
1, // nPlayers
1 + m_mySIS!!.nGuestDevs
1 // nPlayers
) // fc
when (means) {
InviteMeans.EMAIL -> GameUtils.launchEmailInviteActivity(mActivity, nli)
@ -2348,17 +2347,15 @@ class BoardDelegate(delegator: Delegator) :
}
private fun tryInvites() {
if (null != mMissingDevs) {
mMissingDevs?.let {
Assert.assertNotNull(mMissingMeans)
val gameName = GameUtils.getName(mActivity, mRowid)
for (ii in mMissingDevs!!.indices) {
val dev = mMissingDevs!![ii]
for (ii in it.indices) {
val dev = it[ii]
val nPlayers = mMissingCounts!![ii]
Assert.assertTrue(0 <= m_mySIS!!.nGuestDevs)
val forceChannel = ii + m_mySIS!!.nGuestDevs + 1
val nli = NetLaunchInfo(
mActivity, mSummary!!, mGi!!,
nPlayers, forceChannel
mActivity, mSummary!!, mGi!!, nPlayers
)
.setRemotesAreRobots(mRemotesAreRobots)
var destAddr: CommsAddrRec? = null
@ -2371,7 +2368,7 @@ class BoardDelegate(delegator: Delegator) :
InviteMeans.WIFIDIRECT -> WiDirService.inviteRemote(mActivity, dev, nli)
InviteMeans.MQTT -> destAddr = CommsAddrRec(CommsConnType.COMMS_CONN_MQTT)
.setMQTTParams(mMissingDevs!![ii])
.setMQTTParams(it[ii])
InviteMeans.RELAY -> Assert.failDbg() // not getting here, right?
else -> Assert.failDbg()
@ -2533,11 +2530,9 @@ class BoardDelegate(delegator: Delegator) :
private fun nliForMe(): NetLaunchInfo {
val numHere = 1
// This is too simple. Need to know if it's a replacement
val forceChannel = 1 + m_mySIS!!.nGuestDevs
// Log.d( TAG, "nliForMe() => %s", nli );
return NetLaunchInfo(
mActivity, mSummary!!, mGi!!,
numHere, forceChannel
mActivity, mSummary!!, mGi!!, numHere
)
}

View file

@ -74,7 +74,7 @@ class NetLaunchInfo : Serializable {
constructor() {
_conTypes = EMPTY_SET
inviteID = GameUtils.formatGameID(Utils.nextRandomInt())
forceChannel = 1
forceChannel = 0 // 0 means ANY/comms picks
}
private constructor(context: Context, data: String) {
@ -234,11 +234,11 @@ class NetLaunchInfo : Serializable {
}
constructor(
context: Context, summary: GameSummary, gi: CurGameInfo,
numHere: Int, fc: Int
context: Context, summary: GameSummary,
gi: CurGameInfo, numHere: Int
) : this(context, summary, gi) {
nPlayersH = numHere
forceChannel = fc
Assert.assertTrueNR( 0 == forceChannel )
}
constructor(gi: CurGameInfo) : this(

View file

@ -1708,13 +1708,66 @@ haveRealChannel( const CommsCtxt* comms, XP_PlayerAddr channelNo )
return found;
}
typedef struct _GetInviteChannelsData {
XP_U16 mask;
} GetInviteChannelsData;
static ForEachAct
getInviteChannels( MsgQueueElem* elem, void* closure )
{
if ( IS_INVITE(elem) ) {
GetInviteChannelsData* gicdp = (GetInviteChannelsData*)closure;
XP_ASSERT( 0 == (gicdp->mask & (1 << elem->channelNo)) );
gicdp->mask |= 1 << elem->channelNo;
}
return FEA_OK;
}
/* Choose a channel IFF nli doesn't already specify one. */
static XP_PlayerAddr
pickChannel( const CommsCtxt* comms, const NetLaunchInfo* nli,
const CommsAddrRec* destAddr )
{
XP_PlayerAddr result = nli->forceChannel;
if ( 0 == result ) {
/* First, do we already have an invitation for this address */
for ( AddressRecord* rec = comms->recs; !!rec; rec = rec->next ) {
if ( addrs_same( destAddr, &rec->addr ) ) {
result = rec->channelNo;
XP_LOGFF( "addrs match; reusing channel" );
break;
}
}
}
if ( 0 == result ) {
/* Now find the first channelNo that doesn't have an invitation on it
already */
GetInviteChannelsData gicd = {0};
forEachElem( (CommsCtxt*)comms, getInviteChannels, &gicd );
const XP_U16 nPlayers = comms->util->gameInfo->nPlayers;
for ( XP_PlayerAddr chan = 1; chan <= nPlayers; ++chan ) {
if ( 0 == (gicd.mask & (1 << chan)) ) {
result = chan;
break;
}
}
}
LOG_RETURNF( "%d", result );
return result;
}
void
comms_invite( CommsCtxt* comms, XWEnv xwe, const NetLaunchInfo* nli,
const CommsAddrRec* destAddr, XP_Bool sendNow )
{
COMMS_LOGFF("(sendNow=%s)", boolToStr(sendNow));
LOGNLI(nli);
XP_PlayerAddr forceChannel = nli->forceChannel;
COMMS_MUTEX_LOCK(comms);
XP_PlayerAddr forceChannel = pickChannel(comms, nli, destAddr);
XP_LOGFF( "forceChannel: %d", forceChannel );
XP_ASSERT( 0 < forceChannel && (forceChannel & CHANNEL_MASK) == forceChannel );
if ( !haveRealChannel( comms, forceChannel ) ) {
/* See if we have a channel for this address. Then see if we have an
@ -1744,6 +1797,7 @@ comms_invite( CommsCtxt* comms, XWEnv xwe, const NetLaunchInfo* nli,
}
}
}
COMMS_MUTEX_UNLOCK();
LOG_RETURN_VOID();
}

View file

@ -687,12 +687,12 @@ cb_feedGame( CursesBoardState* cbState, XP_U32 gameID,
void
cb_addInvites( CursesBoardState* cbState, XP_U32 gameID, XP_U16 nRemotes,
XP_U16 forceChannels[], const CommsAddrRec destAddrs[] )
const CommsAddrRec destAddrs[] )
{
CursesBoardGlobals* bGlobals = findOrOpenForGameID( cbState, gameID,
NULL, NULL );
CommonGlobals* cGlobals = &bGlobals->cGlobals;
linux_addInvites( cGlobals, nRemotes, forceChannels, destAddrs );
linux_addInvites( cGlobals, nRemotes, destAddrs );
}
XP_Bool

View file

@ -52,7 +52,7 @@ bool cb_feedRow( CursesBoardState* cbState, sqlite3_int64 rowid,
void cb_feedGame( CursesBoardState* cbState, XP_U32 gameID,
const XP_U8* buf, XP_U16 len, const CommsAddrRec* from );
void cb_addInvites( CursesBoardState* cbState, XP_U32 gameID, XP_U16 nRemotes,
XP_U16 forceChannels[], const CommsAddrRec destAddrs[] );
const CommsAddrRec destAddrs[] );
XP_Bool cb_makeRematch( CursesBoardState* cbState, XP_U32 gameID,
RematchOrder ro, XP_U32* newGameID );
XP_Bool cb_makeMoveIf( CursesBoardState* cbState, XP_U32 gameID, XP_Bool tryTrade );

View file

@ -1467,10 +1467,10 @@ makeMoveIfWrapper( void* closure, XP_U32 gameID, XP_Bool tryTrade )
static void
addInvitesWrapper( void* closure, XP_U32 gameID, XP_U16 nRemotes,
XP_U16 forceChannels[], const CommsAddrRec destAddrs[] )
const CommsAddrRec destAddrs[] )
{
CursesAppGlobals* aGlobals = (CursesAppGlobals*)closure;
cb_addInvites( aGlobals->cbState, gameID, nRemotes, forceChannels, destAddrs );
cb_addInvites( aGlobals->cbState, gameID, nRemotes, destAddrs );
}
static const CommonGlobals*

View file

@ -91,19 +91,13 @@ inviteFromArgs( CmdWrapper* wr, cJSON* args )
int nRemotes = cJSON_GetArraySize(remotes);
CommsAddrRec destAddrs[nRemotes];
XP_MEMSET( destAddrs, 0, sizeof(destAddrs) );
XP_U16 channels[nRemotes];
XP_MEMSET( channels, 0, sizeof(channels) );
for ( int ii = 0; ii < nRemotes; ++ii ) {
cJSON* item = cJSON_GetArrayItem( remotes, ii );
cJSON* tmp = cJSON_GetObjectItem( item, "channel" );
XP_ASSERT( !!tmp );
channels[ii] = tmp->valueint;
XP_LOGFF( "read channel: %X", channels[ii] );
cJSON* addr = cJSON_GetObjectItem( item, "addr" );
XP_ASSERT( !!addr );
tmp = cJSON_GetObjectItem( addr, "mqtt" );
cJSON* tmp = cJSON_GetObjectItem( addr, "mqtt" );
if ( !!tmp ) {
XP_LOGFF( "parsing mqtt: %s", tmp->valuestring );
addr_addType( &destAddrs[ii], COMMS_CONN_MQTT );
@ -122,7 +116,7 @@ inviteFromArgs( CmdWrapper* wr, cJSON* args )
}
}
(*wr->procs.addInvites)( wr->closure, gameID, nRemotes, channels, destAddrs );
(*wr->procs.addInvites)( wr->closure, gameID, nRemotes, destAddrs );
LOG_RETURN_VOID();
return XP_TRUE;

View file

@ -29,7 +29,7 @@ typedef struct _CmdWrapper {
void (*quit)( void* closure );
XP_Bool (*newGame)( void* closure, CurGameInfo* gi, XP_U32* newGameID );
void (*addInvites)( void* closure, XP_U32 gameID, XP_U16 nRemotes,
XP_U16 forceChannels[], const CommsAddrRec destAddrs[] );
const CommsAddrRec destAddrs[] );
XP_Bool (*makeMoveIf)( void* closure, XP_U32 gameID, XP_Bool tryTrade );
const CommonGlobals* (*getForGameID)( void* closure, XP_U32 gameID );
XP_Bool (*makeRematch)( void* closure, XP_U32 gameID, RematchOrder ro,

View file

@ -1050,13 +1050,13 @@ sendChatWrapper( void* closure, XP_U32 gameID, const char* msg )
static void
addInvitesWrapper( void* closure, XP_U32 gameID, XP_U16 nRemotes,
XP_U16 forceChannels[], const CommsAddrRec destAddrs[] )
const CommsAddrRec destAddrs[] )
{
GtkAppGlobals* apg = (GtkAppGlobals*)closure;
sqlite3_int64 rowid = 0;
GtkGameGlobals* globals = findOpenGame( apg, &rowid, &gameID );
if ( !!globals ) {
linux_addInvites( &globals->cGlobals, nRemotes, forceChannels, destAddrs );
linux_addInvites( &globals->cGlobals, nRemotes, destAddrs );
} else {
int nRowIDs = 1;
gdb_getRowsForGameID( apg->cag.params->pDb, gameID, &rowid, &nRowIDs );
@ -1064,7 +1064,7 @@ addInvitesWrapper( void* closure, XP_U32 gameID, XP_U16 nRemotes,
GtkGameGlobals tmpGlobals = {0};
if ( 1 == nRowIDs
&& loadGameNoDraw( &tmpGlobals, apg->cag.params, rowid ) ) {
linux_addInvites( &tmpGlobals.cGlobals, nRemotes, forceChannels, destAddrs );
linux_addInvites( &tmpGlobals.cGlobals, nRemotes, destAddrs );
}
freeGlobals( &tmpGlobals );
}

View file

@ -172,7 +172,7 @@ linux_makeMoveIf( CommonGlobals* cGlobals, XP_Bool tryTrade )
void
linux_addInvites( CommonGlobals* cGlobals, XP_U16 nRemotes,
XP_U16 forceChannels[], const CommsAddrRec destAddrs[] )
const CommsAddrRec destAddrs[] )
{
CommsCtxt* comms = cGlobals->game.comms;
@ -181,7 +181,7 @@ linux_addInvites( CommonGlobals* cGlobals, XP_U16 nRemotes,
for ( int ii = 0; ii < nRemotes; ++ii ) {
NetLaunchInfo nli;
nli_init( &nli, cGlobals->gi, &selfAddr, 1, forceChannels[ii] );
nli_init( &nli, cGlobals->gi, &selfAddr, 1, 0 );
comms_invite( comms, NULL_XWE, &nli, &destAddrs[ii], XP_TRUE );
}

View file

@ -76,7 +76,7 @@ void linux_close_socket( CommonGlobals* cGlobals );
XP_Bool linux_makeMoveIf( CommonGlobals* cGlobals, XP_Bool tryTrade );
void linux_addInvites( CommonGlobals* cGlobals, XP_U16 nRemotes,
XP_U16 forceChannels[], const CommsAddrRec destAddrs[] );
const CommsAddrRec destAddrs[] );
#ifdef KEYBOARD_NAV
XP_Bool linShiftFocus( CommonGlobals* cGlobals, XP_Key key,

View file

@ -420,7 +420,7 @@ class Device():
addr = {}
if self.args.WITH_MQTT: addr['mqtt'] = guestDev.mqttDevID
if self.args.WITH_SMS: addr['sms'] = guestDev.smsNumber
remotes.append({'channel': ii+1, 'addr': addr})
remotes.append({'addr': addr})
response = self._sendWaitReply('invite', gid=game.gid,
remotes=remotes)