From 74b6ccbd521c2a35a78871f9731546341f96bfe3 Mon Sep 17 00:00:00 2001 From: ehouse Date: Sun, 16 Oct 2005 01:19:25 +0000 Subject: [PATCH] cleanup ctrl printing; repeat most recent command; cleanup internal names. --- xwords4/relay/cref.cpp | 21 +++++++++------ xwords4/relay/cref.h | 4 +-- xwords4/relay/crefmgr.cpp | 4 +-- xwords4/relay/crefmgr.h | 11 ++++++-- xwords4/relay/ctrl.cpp | 55 +++++++++++++++++++++++---------------- xwords4/relay/tpool.cpp | 4 +-- 6 files changed, 61 insertions(+), 38 deletions(-) diff --git a/xwords4/relay/cref.cpp b/xwords4/relay/cref.cpp index d77c54203..894e79a33 100644 --- a/xwords4/relay/cref.cpp +++ b/xwords4/relay/cref.cpp @@ -70,7 +70,7 @@ SocketsIterator::Next() CookieRef::CookieRef( const char* cookie, const char* connName, CookieID id ) : m_heatbeat(RelayConfigs::GetConfigs()->GetHeartbeatInterval()) - , m_name(cookie==NULL?"":cookie) + , m_cookie(cookie==NULL?"":cookie) , m_connName(connName) , m_cookieID(id) , m_totalSent(0) @@ -780,19 +780,24 @@ CookieRef::_CheckAllConnected() void CookieRef::_PrintCookieInfo( string& out ) { - out += "Name: "; - out += Name(); + out += "Cookie="; + out += Cookie(); out += "\n"; - out += "ID: "; + out += "connName="; char buf[MAX_CONNNAME_LEN+MAX_COOKIE_LEN]; - snprintf( buf, sizeof(buf), "%s\n", Name() ); - out += buf; - snprintf( buf, sizeof(buf), "%s\n", ConnName() ); out += buf; - snprintf( buf, sizeof(buf), "Bytes sent: %d\n", m_totalSent ); + snprintf( buf, sizeof(buf), "id=%d\n", GetCookieID() ); + out += buf; + + snprintf( buf, sizeof(buf), "Bytes sent=%d\n", m_totalSent ); + out += buf; + + snprintf( buf, sizeof(buf), "Total players=%d\n", m_nPlayersTotal ); + out += buf; + snprintf( buf, sizeof(buf), "Players here=%d\n", m_nPlayersHere ); out += buf; /* n messages */ diff --git a/xwords4/relay/cref.h b/xwords4/relay/cref.h index 0dc38e9b0..9539be268 100644 --- a/xwords4/relay/cref.h +++ b/xwords4/relay/cref.h @@ -69,7 +69,7 @@ class CookieRef { int HostKnown( HostID host ) { return -1 != SocketForHost( host ); } int CountSockets() { return m_sockets.size(); } int HasSocket( int socket ); - const char* Name() { return m_name.c_str(); } + const char* Cookie() { return m_cookie.c_str(); } const char* ConnName() { return m_connName.c_str(); } short GetHeartbeat() { return m_heatbeat; } @@ -185,7 +185,7 @@ class CookieRef { /* pthread_rwlock_t m_sockets_rwlock; */ short m_heatbeat; /* might change per carrier or something. */ - string m_name; /* cookie used for initial connections */ + string m_cookie; /* cookie used for initial connections */ string m_connName; /* globally unique name */ CookieID m_cookieID; /* Unique among current games on this server */ int m_totalSent; diff --git a/xwords4/relay/crefmgr.cpp b/xwords4/relay/crefmgr.cpp index 76363669b..0afd32ef5 100644 --- a/xwords4/relay/crefmgr.cpp +++ b/xwords4/relay/crefmgr.cpp @@ -74,7 +74,7 @@ CRefMgr::FindOpenGameFor( const char* cORn, int isCookie, while ( iter != m_cookieMap.end() ) { cref = iter->second; if ( isCookie ) { - if ( 0 == strcmp( cref->Name(), cORn ) ) { + if ( 0 == strcmp( cref->Cookie(), cORn ) ) { if ( cref->NeverFullyConnected() ) { break; } @@ -216,7 +216,7 @@ void CRefMgr::PrintSocketInfo( int socket, string& out ) { SafeCref scr( socket ); - const char* name = scr.Name(); + const char* name = scr.Cookie(); if ( name != NULL && name[0] != '\0' ) { char buf[64]; diff --git a/xwords4/relay/crefmgr.h b/xwords4/relay/crefmgr.h index b58da77a2..d2f62e227 100644 --- a/xwords4/relay/crefmgr.h +++ b/xwords4/relay/crefmgr.h @@ -181,9 +181,16 @@ class SafeCref { m_cref->_CheckAllConnected(); } } - const char* Name() { + const char* Cookie() { if ( IsValid() ) { - return m_cref->Name(); + return m_cref->Cookie(); + } else { + return ""; /* so don't crash.... */ + } + } + const char* ConnName() { + if ( IsValid() ) { + return m_cref->ConnName(); } else { return ""; /* so don't crash.... */ } diff --git a/xwords4/relay/ctrl.cpp b/xwords4/relay/ctrl.cpp index db1b23e81..744bbf0e3 100644 --- a/xwords4/relay/ctrl.cpp +++ b/xwords4/relay/ctrl.cpp @@ -148,7 +148,7 @@ cmd_kill_eject( int socket, const char** args ) killSocket( victim, "ctrl command" ); found = 1; } - } else if ( 0 == strcmp( args[1], "cookie" ) ) { + } else if ( 0 == strcmp( args[1], "cref" ) ) { const char* idhow = args[2]; const char* id = args[3]; if ( idhow != NULL && id != NULL ) { @@ -170,8 +170,8 @@ cmd_kill_eject( int socket, const char** args ) if ( !found ) { char* msg = "* %s socket -- %s\n" - " %s cookie name \n" - " %s cookie id " + " %s cref connName \n" + " %s cref id " ; print_to_sock( socket, 1, msg, args[0], expl, args[0], args[0] ); } @@ -205,19 +205,25 @@ cmd_shutdown( int socket, const char** args ) } static void -print_cookies( int socket, const char* name ) +print_cookies( int socket, const char* cookie, const char* connName ) { CookieMapIterator iter = CRefMgr::Get()->GetCookieIterator(); CookieID id; for ( id = iter.Next(); id != 0; id = iter.Next() ) { SafeCref scr( id ); - if ( scr.Name() == name ) { - string s; - scr.PrintCookieInfo( s ); - - print_to_sock( socket, 1, s.c_str() ); + if ( cookie != NULL && 0 == strcmp( scr.Cookie(), cookie ) ) { + /* print this one */ + } else if ( connName != NULL && + 0 == strcmp( scr.ConnName(), connName ) ) { + /* print this one */ + } else { + continue; } + string s; + scr.PrintCookieInfo( s ); + + print_to_sock( socket, 1, s.c_str() ); } } @@ -246,12 +252,15 @@ cmd_print( int socket, const char** args ) { logf( XW_LOGINFO, "cmd_print called" ); int found = 0; - if ( 0 == strcmp( "cookie", args[1] ) ) { + if ( 0 == strcmp( "cref", args[1] ) ) { if ( 0 == strcmp( "all", args[2] ) ) { print_cookies( socket, (CookieID)0 ); found = 1; - } else if ( 0 == strcmp( "name", args[2] ) ) { - print_cookies( socket, args[3] ); + } else if ( 0 == strcmp( "cookie", args[2] ) ) { + print_cookies( socket, args[3], NULL ); + found = 1; + } else if ( 0 == strcmp( "connName", args[2] ) ) { + print_cookies( socket, NULL, args[3] ); found = 1; } else if ( 0 == strcmp( "id", args[2] ) ) { print_cookies( socket, atoi(args[3]) ); @@ -269,11 +278,12 @@ cmd_print( int socket, const char** args ) if ( !found ) { char* str = - "* %s cookie all\n" - " %s cookie name \n" - " %s cookie id \n" + "* %s cref all\n" + " %s cref name \n" + " %s cref connName \n" + " %s cref id \n" " %s socket all\n" - " %s socket -- print info about cookies and sockets"; + " %s socket -- print info about crefs and sockets"; print_to_sock( socket, 1, str, args[0], args[0], args[0], args[0], args[0] ); } @@ -346,22 +356,23 @@ static void* ctrl_thread_main( void* arg ) { int socket = (int)arg; + string arg0, arg1, arg2, arg3; for ( ; ; ) { print_prompt( socket ); char buf[512]; ssize_t nGot = recv( socket, buf, sizeof(buf)-1, 0 ); + logf( XW_LOGINFO, "nGot=%d", nGot ); if ( nGot <= 1 ) { /* break when just \n comes in */ break; + } else if ( nGot > 2 ) { + /* if nGot is 2, reuse prev string */ + buf[nGot] = '\0'; + istringstream cmd( buf ); + cmd >> arg0 >> arg1 >> arg2 >> arg3; } - buf[nGot] = '\0'; - - string arg0, arg1, arg2, arg3; - istringstream cmd( buf ); - cmd >> arg0 >> arg1 >> arg2 >> arg3; - const char* args[] = { arg0.c_str(), arg1.c_str(), diff --git a/xwords4/relay/tpool.cpp b/xwords4/relay/tpool.cpp index a485d010a..951190547 100644 --- a/xwords4/relay/tpool.cpp +++ b/xwords4/relay/tpool.cpp @@ -244,8 +244,8 @@ XWThreadPool::real_listener() int nMillis = tmgr->GetPollTimeout(); - logf( XW_LOGINFO, "polling %s", log ); - int nEvents = poll( fds, nSockets, nMillis ); /* -1: infinite timeout */ + logf( XW_LOGINFO, "polling %s nmillis=%d", log, nMillis ); + int nEvents = poll( fds, nSockets, nMillis ); logf( XW_LOGINFO, "back from poll: %d", nEvents ); if ( nEvents == 0 ) { tmgr->FireElapsedTimers();