From a91056bed2fc243994580d63bb4ef66f62a91a34 Mon Sep 17 00:00:00 2001 From: ehouse Date: Sun, 18 Nov 2007 23:38:56 +0000 Subject: [PATCH] Make cooperation in a heartbeat scheme a compile-time option. This is meant to allow comms to take over heartbeats. --- relay/cref.cpp | 8 +++++++- relay/crefmgr.cpp | 8 ++++++++ relay/crefmgr.h | 6 +++++- relay/states.cpp | 6 +++++- relay/states.h | 12 ++++++------ relay/xwrelay.conf | 2 +- relay/xwrelay.cpp | 4 ++++ relay/xwrelay.h | 5 ++++- 8 files changed, 40 insertions(+), 11 deletions(-) diff --git a/relay/cref.cpp b/relay/cref.cpp index 6d3bb121b..39778ca86 100644 --- a/relay/cref.cpp +++ b/relay/cref.cpp @@ -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! */ } diff --git a/relay/crefmgr.cpp b/relay/crefmgr.cpp index 86fc6d6bc..55b3e808f 100644 --- a/relay/crefmgr.cpp +++ b/relay/crefmgr.cpp @@ -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() diff --git a/relay/crefmgr.h b/relay/crefmgr.h index 51a2169f6..5f5b1b33f 100644 --- a/relay/crefmgr.h +++ b/relay/crefmgr.h @@ -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 ); diff --git a/relay/states.cpp b/relay/states.cpp index 55db9b9ce..ceed6eede 100644 --- a/relay/states.cpp +++ b/relay/states.cpp @@ -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); diff --git a/relay/states.h b/relay/states.h index a4111e4f0..cc33fa84a 100644 --- a/relay/states.h +++ b/relay/states.h @@ -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 */ diff --git a/relay/xwrelay.conf b/relay/xwrelay.conf index 2c67dc2da..86f03a650 100644 --- a/relay/xwrelay.conf +++ b/relay/xwrelay.conf @@ -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 diff --git a/relay/xwrelay.cpp b/relay/xwrelay.cpp index 571dbb61e..f456d16a1 100644 --- a/relay/xwrelay.cpp +++ b/relay/xwrelay.cpp @@ -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 ); diff --git a/relay/xwrelay.h b/relay/xwrelay.h index 09d614e5c..296e9ea69 100644 --- a/relay/xwrelay.h +++ b/relay/xwrelay.h @@ -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 */