diff --git a/xwords4/relay/ctrl.cpp b/xwords4/relay/ctrl.cpp index 85141d9bf..19d5685e7 100644 --- a/xwords4/relay/ctrl.cpp +++ b/xwords4/relay/ctrl.cpp @@ -546,7 +546,7 @@ cmd_devs( int socket, const char* cmd, int argc, gchar** args ) string result; if ( 1 >= argc ) { /* missing param; let help print */ - } else if ( 0 == strcmp( "print", args[1] ) ) { + } else if ( 0 == strcmp( "list", args[1] ) ) { DevIDRelay devid = 0; if ( 2 < argc ) { devid = (DevIDRelay)strtoul( args[2], NULL, 10 ); @@ -555,14 +555,33 @@ cmd_devs( int socket, const char* cmd, int argc, gchar** args ) found = true; } else if ( 0 == strcmp( "ping", args[1] ) ) { } else if ( 0 == strcmp( "msg", args[1] ) && 3 < argc ) { - DevIDRelay devid = (DevIDRelay)strtoul( args[2], NULL, 10 ); - const char* msg = args[3]; - if ( post_message( devid, msg, onAckProc, (void*)socket ) ) { - string_printf( result, "posted message: %s\n", msg ); + /* Get the list to send to */ + vector devids; + if ( 0 == strcmp( "all", args[3] ) ) { + DevMgr::Get()->getKnownDevices( devids ); } else { - string_printf( result, "unable to post; does dev %d exist\n", - devid ); + for ( int ii = 3; ii < argc; ++ii ) { + DevIDRelay devid = (DevIDRelay)strtoul( args[ii], NULL, 10 ); + devids.push_back( devid ); + } } + + /* Send to each in the list */ + const char* msg = args[2]; + vector::const_iterator iter; + for ( iter = devids.begin(); devids.end() != iter; ++iter ) { + DevIDRelay devid = *iter; + if ( 0 != devid ) { + if ( post_message( devid, msg, onAckProc, + (void*)socket ) ) { + string_printf( result, "posted message: %s\n", msg ); + } else { + string_printf( result, "unable to post; does " + "dev %d exist\n", devid ); + } + } + } + found = true; } else if ( 0 == strcmp( "rm", args[1] ) && 2 < argc ) { DevIDRelay devid = (DevIDRelay)strtoul( args[2], NULL, 10 ); @@ -580,14 +599,15 @@ cmd_devs( int socket, const char* cmd, int argc, gchar** args ) } } else { const char* strs[] = { - "* %s print []\n", + "* %s list [] -- prints one id, or all if none specified\n", " %s ping\n", - " %s msg \n", + " %s msg +\n", + " %s msg all\n", " %s rm \n", }; string help; for ( size_t ii = 0; ii < VSIZE(strs); ++ii ) { - string_printf( help, strs[ii], args[0] ); + string_printf( help, strs[ii], cmd ); } send( socket, help.c_str(), help.size(), 0 ); } diff --git a/xwords4/relay/devmgr.cpp b/xwords4/relay/devmgr.cpp index 00aeadc39..cdda67755 100644 --- a/xwords4/relay/devmgr.cpp +++ b/xwords4/relay/devmgr.cpp @@ -114,6 +114,15 @@ DevMgr::forgetDevice( DevIDRelay devid ) return found; } +void +DevMgr::getKnownDevices( vector& devids ) +{ + MutexLock ml( &m_mapLock ); + map::const_iterator iter; + for ( iter = m_devAddrMap.begin(); m_devAddrMap.end() != iter; ++iter ) { + devids.push_back( iter->first ); + } +} // 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 @@ -155,10 +164,9 @@ DevMgr::printDevices( string& str, DevIDRelay devid ) uint32_t age = *keysIter; DevIDRelay devid = agedDevs.find( age )->second; age = now - age; - string_printf( str, "%.3d: devid: %.10d; age: %.3d seconds\n", ++row, + string_printf( str, "%.3d: devid: % 10d; age: %.3d seconds\n", ++row, devid, age ); } - } void diff --git a/xwords4/relay/devmgr.h b/xwords4/relay/devmgr.h index 500656f88..1b7badd0a 100644 --- a/xwords4/relay/devmgr.h +++ b/xwords4/relay/devmgr.h @@ -37,6 +37,7 @@ class DevMgr { /* Called from ctrl port */ void printDevices( string& str, DevIDRelay devid /* 0 means all */ ); bool forgetDevice( DevIDRelay devid ); + void getKnownDevices( vector& devids ); private: