Toward resurrecting ctrl feature to give runtime info about stuff that

isn't in the DB.  Step one: comment out the stuff that doesn't compile
and add ability to print all registered devices with their ages.
This commit is contained in:
Eric House 2013-08-15 08:44:33 -07:00
parent 06f0b7e411
commit 62f0819a82
6 changed files with 78 additions and 22 deletions

View file

@ -19,22 +19,23 @@
CXX = g++
CC=$(CXX)
SRC = \
addrinfo.cpp \
cidlock.cpp \
configs.cpp \
cref.cpp \
crefmgr.cpp \
ctrl.cpp \
dbmgr.cpp \
devmgr.cpp \
http.cpp \
lstnrmgr.cpp \
permid.cpp \
states.cpp \
timermgr.cpp \
tpool.cpp \
cidlock.cpp \
addrinfo.cpp \
devmgr.cpp \
udpqueue.cpp \
udpack.cpp \
udpager.cpp \
udpqueue.cpp \
xwrelay.cpp \
# STATIC ?= -static

View file

@ -47,6 +47,7 @@
#include "configs.h"
#include "lstnrmgr.h"
#include "tpool.h"
#include "devmgr.h"
#define MAX_ARGS 10
@ -67,11 +68,11 @@ pthread_mutex_t g_ctrlSocksMutex = PTHREAD_MUTEX_INITIALIZER;
static bool cmd_quit( int socket, const char** args );
static bool cmd_print( int socket, const char** args );
static bool cmd_lock( int socket, const char** args );
/* static bool cmd_lock( int socket, const char** args ); */
static bool cmd_help( int socket, const char** args );
static bool cmd_start( int socket, const char** args );
static bool cmd_stop( int socket, const char** args );
static bool cmd_kill_eject( int socket, const char** args );
/* static bool cmd_kill_eject( int socket, const char** args ); */
static bool cmd_get( int socket, const char** args );
static bool cmd_set( int socket, const char** args );
static bool cmd_shutdown( int socket, const char** args );
@ -123,11 +124,11 @@ print_to_sock( int sock, bool addCR, const char* what, ... )
static const FuncRec gFuncs[] = {
{ "?", cmd_help },
{ "crash", cmd_crash },
{ "eject", cmd_kill_eject },
/* { "eject", cmd_kill_eject }, */
{ "get", cmd_get },
{ "help", cmd_help },
{ "kill", cmd_kill_eject },
{ "lock", cmd_lock },
/* { "kill", cmd_kill_eject }, */
/* { "lock", cmd_lock }, */
{ "print", cmd_print },
{ "quit", cmd_quit },
{ "rev", cmd_rev },
@ -182,6 +183,7 @@ cmd_stop( int socket, const char** args )
return false;
}
#if 0
static bool
cmd_kill_eject( int socket, const char** args )
{
@ -226,6 +228,7 @@ cmd_kill_eject( int socket, const char** args )
}
return false;
} /* cmd_kill_eject */
#endif
static bool
cmd_get( int socket, const char** args )
@ -441,6 +444,7 @@ print_cookies( int socket, const char* cookie, const char* connName )
}
}
#if 0
static void
print_socket_info( int out, int which )
{
@ -448,17 +452,18 @@ print_socket_info( int out, int which )
CRefMgr::Get()->PrintSocketInfo( which, s );
print_to_sock( out, 1, s.c_str() );
}
#endif
static void
print_sockets( int out, int sought )
{
SocketsIterator iter = CRefMgr::Get()->MakeSocketsIterator();
int sock;
while ( (sock = iter.Next()) != 0 ) {
if ( sought == 0 || sought == sock ) {
print_socket_info( out, sock );
}
}
/* SocketsIterator iter = CRefMgr::Get()->MakeSocketsIterator(); */
/* int sock; */
/* while ( (sock = iter.Next()) != 0 ) { */
/* if ( sought == 0 || sought == sock ) { */
/* print_socket_info( out, sock ); */
/* } */
/* } */
}
static bool
@ -488,6 +493,13 @@ cmd_print( int socket, const char** args )
print_sockets( socket, atoi(args[3]) );
found = true;
}
} else if ( 0 == strcmp( "dev", args[1] ) ) {
if ( 0 == strcmp( "all", args[2] ) ) {
string str;
DevMgr::Get()->printDevices( str );
print_to_sock( socket, true, str.c_str() );
found = true;
}
}
if ( !found ) {
@ -496,14 +508,16 @@ cmd_print( int socket, const char** args )
" %s cref name <name>\n"
" %s cref connName <name>\n"
" %s cref id <id>\n"
" %s dev all -- list all known devices (by how recently connected)\n"
" %s socket all\n"
" %s socket <num> -- print info about crefs and sockets";
print_to_sock( socket, true, str,
args[0], args[0], args[0], args[0], args[0], args[0] );
print_to_sock( socket, true, str, args[0], args[0], args[0],
args[0], args[0], args[0], args[0] );
}
return false;
} /* cmd_print */
#if 0
static bool
cmd_lock( int socket, const char** args )
{
@ -519,6 +533,7 @@ cmd_lock( int socket, const char** args )
return 0;
} /* cmd_lock */
#endif
static bool
cmd_help( int socket, const char** args )

View file

@ -19,6 +19,7 @@
*/
#include <glib.h>
#include <algorithm>
#include "devmgr.h"
#include "mlock.h"
@ -60,6 +61,7 @@ DevMgr::Remember( DevIDRelay devid, const AddrInfo::AddrUnion* saddr )
result.first->second = rec;
}
// Don't think we need m_addrDevMap anymore
map<AddrInfo::AddrUnion, DevIDRelay>::iterator iter =
m_addrDevMap.find(*saddr);
if ( m_addrDevMap.end() != iter && devid != iter->second ) {
@ -100,3 +102,40 @@ DevMgr::get( DevIDRelay devid )
return result;
}
// Print info about every device, ordered by how old they are (how long since
// last remembered). Build a separate array with the mutex held, then release
// it, so that as much work as possible is done on the calling thread without
// holding up more important stuff.
void
DevMgr::printDevices( string& str )
{
map<uint32_t, DevIDRelay> agedDevs;
{
MutexLock ml( &m_mapLock );
map<DevIDRelay,UDPAddrRec>::const_iterator iter;
for ( iter = m_devAddrMap.begin(); iter != m_devAddrMap.end(); ++iter ) {
DevIDRelay devid = iter->first;
uint32_t added = iter->second.m_added;
agedDevs.insert( pair<uint32_t, DevIDRelay>(added, devid) );
}
}
// Now sort by age and print
vector<uint32_t> keys;
map<uint32_t, DevIDRelay>::const_iterator iter1;
for ( iter1 = agedDevs.begin(); agedDevs.end() != iter1; ++iter1 ) {
keys.push_back( iter1->first );
}
std::sort( keys.begin(), keys.end() );
time_t now = time(NULL);
vector<uint32_t>::const_iterator keysIter;
for ( keysIter = keys.begin(); keys.end() != keysIter; ++keysIter ) {
uint32_t age = *keysIter;
DevIDRelay devid = agedDevs.find( age )->second;
age = now - age;
string_printf( str, "devid: %d; age: %d seconds\n", devid, age );
}
}

View file

@ -35,6 +35,8 @@ class DevMgr {
void Remember( DevIDRelay devid, const AddrInfo* addr );
const AddrInfo::AddrUnion* get( DevIDRelay devid );
void printDevices( string& str );
private:
DevMgr() { pthread_mutex_init( &m_mapLock, NULL ); }
/* destructor's never called....

View file

@ -1537,7 +1537,7 @@ read_udp_packet( int udpsock )
/* From stack overflow, toward a snprintf with an expanding buffer.
*/
void
string&
string_printf( string& str, const char* fmt, ... )
{
const int origsiz = str.size();
@ -2042,8 +2042,7 @@ main( int argc, char** argv )
}
}
if ( FD_ISSET( g_control, &rfds ) ) {
assert(0); // not working; don't use until fixed
// run_ctrl_thread( g_control );
run_ctrl_thread( g_control );
--retval;
}
if ( -1 != g_udpsock && FD_ISSET( g_udpsock, &rfds ) ) {

View file

@ -58,7 +58,7 @@ int GetNSpawns(void);
int make_socket( unsigned long addr, unsigned short port );
void string_printf( std::string& str, const char* fmt, ... );
std::string& string_printf( std::string& str, const char* fmt, ... );
int read_packet( int sock, unsigned char* buf, int buflen );