Make cooperation in a heartbeat scheme a compile-time option. This is

meant to allow comms to take over heartbeats.
This commit is contained in:
ehouse 2007-11-18 23:38:56 +00:00
parent f752eace94
commit a91056bed2
8 changed files with 40 additions and 11 deletions

View file

@ -262,6 +262,7 @@ CookieRef::HasSocket( int socket )
return found;
} /* HasSocket */
#ifdef RELAY_HEARTBEAT
void
CookieRef::_HandleHeartbeat( HostID id, int socket )
{
@ -286,6 +287,7 @@ CookieRef::_CheckHeartbeats( time_t now )
handleEvents();
} /* CheckHeartbeats */
#endif
void
CookieRef::_Forward( HostID src, HostID dest, unsigned char* buf, int buflen )
@ -327,6 +329,7 @@ CookieRef::pushReconnectEvent( int socket, HostID srcID,
m_eventQueue.push_back( evt );
} /* pushReconnectEvent */
#ifdef RELAY_HEARTBEAT
void
CookieRef::pushHeartbeatEvent( HostID id, int socket )
{
@ -345,6 +348,7 @@ CookieRef::pushHeartFailedEvent( int socket )
evt.u.heart.socket = socket;
m_eventQueue.push_back( evt );
}
#endif
void
CookieRef::pushForwardEvent( HostID src, HostID dest,
@ -649,7 +653,6 @@ CookieRef::forward( const CRefEvent* evt )
{
unsigned char* buf = evt->u.fwd.buf;
int buflen = evt->u.fwd.buflen;
HostID src = evt->u.fwd.src;
HostID dest = evt->u.fwd.dest;
int destSocket = SocketForHost( dest );
@ -660,7 +663,10 @@ CookieRef::forward( const CRefEvent* evt )
send_with_length( destSocket, buf, buflen );
/* also note that we've heard from src recently */
#ifdef RELAY_HEARTBEAT
HostID src = evt->u.fwd.src;
pushHeartbeatEvent( src, SocketForHost(src) );
#endif
} else {
/* We're not really connected yet! */
}

View file

@ -392,12 +392,14 @@ CRefMgr::UnlockCref( CookieRef* cref )
pthread_mutex_unlock( cref_mutex );
}
#ifdef RELAY_HEARTBEAT
/* static */ void
CRefMgr::heartbeatProc( void* closure )
{
CRefMgr* self = (CRefMgr*)closure;
self->checkHeartbeats( now() );
} /* heartbeatProc */
#endif
CookieRef*
CRefMgr::AddNew( const char* cookie, const char* connName, CookieID id )
@ -412,12 +414,14 @@ CRefMgr::AddNew( const char* cookie, const char* connName, CookieID id )
logf( XW_LOGINFO, "paired cookie %s/connName %s with id %d",
(cookie?cookie:"NULL"), connName, ref->GetCookieID() );
#ifdef RELAY_HEARTBEAT
if ( m_cookieMap.size() == 1 ) {
RelayConfigs* cfg = RelayConfigs::GetConfigs();
short heartbeat = cfg->GetHeartbeatInterval();
TimerMgr::GetTimerMgr()->SetTimer( heartbeat, heartbeatProc, this,
heartbeat );
}
#endif
return ref;
} /* AddNew */
@ -452,9 +456,11 @@ CRefMgr::Delete( CookieRef* cref )
delete cref;
#ifdef RELAY_HEARTBEAT
if ( m_cookieMap.size() == 0 ) {
TimerMgr::GetTimerMgr()->ClearTimer( heartbeatProc, this );
}
#endif
logf( XW_LOGINFO, "CRefMgr::Delete done" );
}
@ -493,6 +499,7 @@ CRefMgr::getCookieRef_impl( CookieID cookieID )
return ref;
}
#ifdef RELAY_HEARTBEAT
void
CRefMgr::checkHeartbeats( time_t now )
{
@ -513,6 +520,7 @@ CRefMgr::checkHeartbeats( time_t now )
scr.CheckHeartbeats( now );
}
} /* checkHeartbeats */
#endif
/* static */ CookieMapIterator
CRefMgr::GetCookieIterator()

View file

@ -1,7 +1,7 @@
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
/*
* Copyright 2005 by Eric House (xwords@eehouse.org). All rights reserved.
* Copyright 2005-2007 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
@ -165,6 +165,8 @@ class SafeCref {
m_cref->_Remove( socket );
}
}
#ifdef RELAY_HEARTBEAT
int HandleHeartbeat( HostID id, int socket ) {
if ( IsValid() ) {
m_cref->_HandleHeartbeat( id, socket );
@ -178,6 +180,8 @@ class SafeCref {
m_cref->_CheckHeartbeats( now );
}
}
#endif
void PrintCookieInfo( string& out ) {
if ( IsValid() ) {
m_cref->_PrintCookieInfo( out );

View file

@ -97,6 +97,7 @@ StateTable g_stateTable[] = {
{ XWS_ALLCONNECTED, XWE_REMOVESOCKET, XWA_REMOVESOCKET, XWS_MISSING },
{ XWS_MISSING, XWE_REMOVESOCKET, XWA_REMOVESOCKET, XWS_MISSING },
#ifdef RELAY_HEARTBEAT
{ XWS_ALLCONNECTED, XWE_HEARTFAILED, XWA_HEARTDISCONN, XWS_MISSING },
{ XWS_CONNECTING, XWE_HEARTFAILED, XWA_HEARTDISCONN, XWS_CONNECTING },
{ XWS_MISSING, XWE_HEARTFAILED, XWA_HEARTDISCONN, XWS_MISSING },
@ -105,6 +106,7 @@ StateTable g_stateTable[] = {
{ XWS_CONNECTING, XWE_HEARTRCVD, XWA_NOTEHEART, XWS_CONNECTING },
{ XWS_ALLCONNECTED, XWE_HEARTRCVD, XWA_NOTEHEART, XWS_ALLCONNECTED },
{ XWS_MISSING, XWE_HEARTRCVD, XWA_NOTEHEART, XWS_MISSING },
#endif
/* Connect timer */
{ XWS_CONNECTING, XWE_CONNTIMER, XWA_TIMERDISCONN, XWS_DEAD },
@ -187,9 +189,11 @@ eventString( XW_RELAY_EVENT evt )
CASESTR(XWE_RECONNECTMSG);
CASESTR(XWE_DISCONNMSG);
CASESTR(XWE_FORWARDMSG);
#ifdef RELAY_HEARTBEAT
CASESTR(XWE_HEARTRCVD);
CASESTR(XWE_CONNTIMER);
CASESTR(XWE_HEARTFAILED);
#endif
CASESTR(XWE_CONNTIMER);
CASESTR(XWE_ANY);
CASESTR(XWE_REMOVESOCKET);
CASESTR(XWE_NOMORESOCKETS);

View file

@ -87,21 +87,21 @@ typedef enum {
,XWE_SOMEMISSING /* notify that some expected players are still missing */
,XWE_CONNECTMSG /* A device is connecting using the cookie for
this object */
this object */
,XWE_RECONNECTMSG /* A device is re-connecting using the
connID for this object */
connID for this object */
,XWE_DISCONNMSG /* disconnect socket from this game/cref */
,XWE_FORWARDMSG /* A message needs forwarding */
#ifdef RELAY_HEARTBEAT
,XWE_HEARTRCVD /* A heartbeat message arrived */
,XWE_CONNTIMER /* timer for did we get all players hooked
up */
,XWE_HEARTFAILED
#endif
,XWE_CONNTIMER /* timer for did we get all players hooked
up */
,XWE_REMOVESOCKET /* Need to remove socket from this cref */
,XWE_NOTIFYDISCON /* Send a discon */

View file

@ -30,4 +30,4 @@ IDFILE=/home/eehouse/xwrelay_id.txt
# Initial level of logging. See xwrelay_priv.h for values. Currently
# 0 means errors only, 1 info, 2 verbose and 3 very verbose.
LOGLEVEL=0
LOGLEVEL=2

View file

@ -137,6 +137,7 @@ getNetByte( unsigned char** bufpp, unsigned char* end, unsigned char* out )
return ok;
} /* getNetByte */
#ifdef RELAY_HEARTBEAT
static int
processHeartbeat( unsigned char* buf, int bufLen, int socket )
{
@ -158,6 +159,7 @@ processHeartbeat( unsigned char* buf, int bufLen, int socket )
}
return success;
} /* processHeartbeat */
#endif
static int
readStr( unsigned char** bufp, const unsigned char* end,
@ -370,10 +372,12 @@ processMessage( unsigned char* buf, int bufLen, int socket )
case XWRELAY_GAME_DISCONNECT:
success = processDisconnect( buf+1, bufLen-1, socket );
break;
#ifdef RELAY_HEARTBEAT
case XWRELAY_HEARTBEAT:
logf( XW_LOGINFO, "processMessage got XWRELAY_HEARTBEAT" );
success = processHeartbeat( buf + 1, bufLen - 1, socket );
break;
#endif
case XWRELAY_MSG_TORELAY:
logf( XW_LOGINFO, "processMessage got XWRELAY_MSG_TORELAY" );
success = forwardMessage( buf, bufLen, socket );

View file

@ -74,9 +74,12 @@ enum { XWRELAY_NONE /* 0 is an illegal value */
, XWRELAY_CONNECTDENIED
/* The relay says go away. Format: reason code: 1 */
#ifdef RELAY_HEARTBEAT
, XWRELAY_HEARTBEAT
/* Sent in either direction. Format: cookieID: 2; srcID: 1 */
#else
, _XWRELAY_HEARTBEAT /* don't use this */
#endif
, XWRELAY_MSG_FROMRELAY
/* Sent from relay to device. Format: cookieID: 2; src_hostID: 1;
dest_hostID: 1; data <len-headerLen> */