mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-30 08:34:16 +01:00
change 'devs msg' command to send to a list, or to all
This commit is contained in:
parent
b3342c6e8a
commit
2e7433f2b2
3 changed files with 41 additions and 12 deletions
|
@ -546,7 +546,7 @@ cmd_devs( int socket, const char* cmd, int argc, gchar** args )
|
||||||
string result;
|
string result;
|
||||||
if ( 1 >= argc ) {
|
if ( 1 >= argc ) {
|
||||||
/* missing param; let help print */
|
/* missing param; let help print */
|
||||||
} else if ( 0 == strcmp( "print", args[1] ) ) {
|
} else if ( 0 == strcmp( "list", args[1] ) ) {
|
||||||
DevIDRelay devid = 0;
|
DevIDRelay devid = 0;
|
||||||
if ( 2 < argc ) {
|
if ( 2 < argc ) {
|
||||||
devid = (DevIDRelay)strtoul( args[2], NULL, 10 );
|
devid = (DevIDRelay)strtoul( args[2], NULL, 10 );
|
||||||
|
@ -555,14 +555,33 @@ cmd_devs( int socket, const char* cmd, int argc, gchar** args )
|
||||||
found = true;
|
found = true;
|
||||||
} else if ( 0 == strcmp( "ping", args[1] ) ) {
|
} else if ( 0 == strcmp( "ping", args[1] ) ) {
|
||||||
} else if ( 0 == strcmp( "msg", args[1] ) && 3 < argc ) {
|
} else if ( 0 == strcmp( "msg", args[1] ) && 3 < argc ) {
|
||||||
DevIDRelay devid = (DevIDRelay)strtoul( args[2], NULL, 10 );
|
/* Get the list to send to */
|
||||||
const char* msg = args[3];
|
vector<DevIDRelay> devids;
|
||||||
if ( post_message( devid, msg, onAckProc, (void*)socket ) ) {
|
if ( 0 == strcmp( "all", args[3] ) ) {
|
||||||
|
DevMgr::Get()->getKnownDevices( devids );
|
||||||
|
} else {
|
||||||
|
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<DevIDRelay>::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 );
|
string_printf( result, "posted message: %s\n", msg );
|
||||||
} else {
|
} else {
|
||||||
string_printf( result, "unable to post; does dev %d exist\n",
|
string_printf( result, "unable to post; does "
|
||||||
devid );
|
"dev %d exist\n", devid );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
found = true;
|
found = true;
|
||||||
} else if ( 0 == strcmp( "rm", args[1] ) && 2 < argc ) {
|
} else if ( 0 == strcmp( "rm", args[1] ) && 2 < argc ) {
|
||||||
DevIDRelay devid = (DevIDRelay)strtoul( args[2], NULL, 10 );
|
DevIDRelay devid = (DevIDRelay)strtoul( args[2], NULL, 10 );
|
||||||
|
@ -580,14 +599,15 @@ cmd_devs( int socket, const char* cmd, int argc, gchar** args )
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const char* strs[] = {
|
const char* strs[] = {
|
||||||
"* %s print [<id>]\n",
|
"* %s list [<id>] -- prints one id, or all if none specified\n",
|
||||||
" %s ping\n",
|
" %s ping\n",
|
||||||
" %s msg <devid> <msg_text>\n",
|
" %s msg <msg_text> <devid>+\n",
|
||||||
|
" %s msg <msg_text> all\n",
|
||||||
" %s rm <devid>\n",
|
" %s rm <devid>\n",
|
||||||
};
|
};
|
||||||
string help;
|
string help;
|
||||||
for ( size_t ii = 0; ii < VSIZE(strs); ++ii ) {
|
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 );
|
send( socket, help.c_str(), help.size(), 0 );
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,6 +114,15 @@ DevMgr::forgetDevice( DevIDRelay devid )
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
DevMgr::getKnownDevices( vector<DevIDRelay>& devids )
|
||||||
|
{
|
||||||
|
MutexLock ml( &m_mapLock );
|
||||||
|
map<DevIDRelay,UDPAddrRec>::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
|
// 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
|
// 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;
|
uint32_t age = *keysIter;
|
||||||
DevIDRelay devid = agedDevs.find( age )->second;
|
DevIDRelay devid = agedDevs.find( age )->second;
|
||||||
age = now - age;
|
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 );
|
devid, age );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -37,6 +37,7 @@ class DevMgr {
|
||||||
/* Called from ctrl port */
|
/* Called from ctrl port */
|
||||||
void printDevices( string& str, DevIDRelay devid /* 0 means all */ );
|
void printDevices( string& str, DevIDRelay devid /* 0 means all */ );
|
||||||
bool forgetDevice( DevIDRelay devid );
|
bool forgetDevice( DevIDRelay devid );
|
||||||
|
void getKnownDevices( vector<DevIDRelay>& devids );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue