2017-06-21 17:15:53 -06:00
# include "blade.h"
# include "tap.h"
# define CONSOLE_INPUT_MAX 512
ks_bool_t g_shutdown = KS_FALSE ;
void loop ( blade_handle_t * bh ) ;
void process_console_input ( blade_handle_t * bh , char * line ) ;
typedef void ( * command_callback ) ( blade_handle_t * bh , char * args ) ;
struct command_def_s {
const char * cmd ;
command_callback callback ;
} ;
void command_quit ( blade_handle_t * bh , char * args ) ;
2017-09-13 08:41:40 -06:00
void command_presence ( blade_handle_t * bh , char * args ) ;
2017-09-20 22:07:34 -06:00
void command_identity ( blade_handle_t * bh , char * args ) ;
2017-06-21 17:15:53 -06:00
static const struct command_def_s command_defs [ ] = {
{ " quit " , command_quit } ,
2017-09-13 08:41:40 -06:00
{ " presence " , command_presence } ,
2017-09-20 22:07:34 -06:00
{ " identity " , command_identity } ,
2017-06-21 17:15:53 -06:00
{ NULL , NULL }
} ;
struct testproto_s {
blade_handle_t * handle ;
ks_pool_t * pool ;
ks_hash_t * participants ;
2017-08-18 16:30:08 -06:00
ks_hash_t * channels ;
2017-06-21 17:15:53 -06:00
} ;
typedef struct testproto_s testproto_t ;
2017-08-18 16:30:08 -06:00
testproto_t * g_test = NULL ;
2017-08-03 20:26:07 -06:00
static void testproto_cleanup ( void * ptr , void * arg , ks_pool_cleanup_action_t action , ks_pool_cleanup_type_t type )
2017-06-21 17:15:53 -06:00
{
//testproto_t *test = (testproto_t *)ptr;
//ks_assert(test);
switch ( action ) {
case KS_MPCL_ANNOUNCE :
break ;
case KS_MPCL_TEARDOWN :
break ;
case KS_MPCL_DESTROY :
break ;
}
}
ks_status_t testproto_create ( testproto_t * * testP , blade_handle_t * bh )
{
testproto_t * test = NULL ;
ks_pool_t * pool = NULL ;
ks_assert ( testP ) ;
ks_assert ( bh ) ;
ks_pool_open ( & pool ) ;
ks_assert ( pool ) ;
test = ks_pool_alloc ( pool , sizeof ( testproto_t ) ) ;
test - > handle = bh ;
test - > pool = pool ;
ks_hash_create ( & test - > participants , KS_HASH_MODE_CASE_INSENSITIVE , KS_HASH_FLAG_RWLOCK | KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_FREE_KEY , pool ) ;
2017-08-18 16:30:08 -06:00
ks_hash_create ( & test - > channels , KS_HASH_MODE_CASE_INSENSITIVE , KS_HASH_FLAG_RWLOCK | KS_HASH_FLAG_DUP_CHECK | KS_HASH_FLAG_FREE_KEY , pool ) ;
2017-06-21 17:15:53 -06:00
2017-08-03 20:26:07 -06:00
ks_pool_set_cleanup ( test , NULL , testproto_cleanup ) ;
2017-06-21 17:15:53 -06:00
* testP = test ;
return KS_STATUS_SUCCESS ;
}
ks_status_t testproto_destroy ( testproto_t * * testP )
{
testproto_t * test = NULL ;
2017-07-03 13:45:29 -06:00
ks_pool_t * pool = NULL ;
2017-06-21 17:15:53 -06:00
ks_assert ( testP ) ;
ks_assert ( * testP ) ;
test = * testP ;
2017-07-03 13:45:29 -06:00
pool = test - > pool ;
ks_pool_close ( & pool ) ;
2017-06-21 17:15:53 -06:00
2017-07-03 13:45:29 -06:00
* testP = NULL ;
2017-06-21 17:15:53 -06:00
return KS_STATUS_SUCCESS ;
}
2017-08-01 16:30:25 -06:00
ks_bool_t test_publish_response_handler ( blade_rpc_response_t * brpcres , void * data )
2017-06-21 17:15:53 -06:00
{
2017-07-03 13:57:47 -05:00
//testproto_t *test = NULL;
2017-06-21 17:15:53 -06:00
blade_handle_t * bh = NULL ;
blade_session_t * bs = NULL ;
ks_assert ( brpcres ) ;
ks_assert ( data ) ;
2017-08-01 16:30:25 -06:00
//test = (testproto_t *)data;
2017-06-21 17:15:53 -06:00
bh = blade_rpc_response_handle_get ( brpcres ) ;
ks_assert ( bh ) ;
2017-07-03 12:48:46 -06:00
bs = blade_sessionmgr_session_lookup ( blade_handle_sessionmgr_get ( bh ) , blade_rpc_response_sessionid_get ( brpcres ) ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( bs ) ;
ks_log ( KS_LOG_DEBUG , " Session (%s) publish response processing \n " , blade_session_id_get ( bs ) ) ;
blade_session_read_unlock ( bs ) ;
return KS_FALSE ;
}
2017-08-01 16:30:25 -06:00
ks_bool_t test_join_request_handler ( blade_rpc_request_t * brpcreq , void * data )
2017-06-21 17:15:53 -06:00
{
testproto_t * test = NULL ;
blade_handle_t * bh = NULL ;
blade_session_t * bs = NULL ;
const char * requester_nodeid = NULL ;
const char * key = NULL ;
cJSON * params = NULL ;
2017-07-25 11:00:45 -06:00
cJSON * channels = NULL ;
2017-06-21 17:15:53 -06:00
cJSON * result = NULL ;
ks_assert ( brpcreq ) ;
ks_assert ( data ) ;
2017-08-01 16:30:25 -06:00
test = ( testproto_t * ) data ;
2017-06-21 17:15:53 -06:00
bh = blade_rpc_request_handle_get ( brpcreq ) ;
ks_assert ( bh ) ;
2017-07-25 11:00:45 -06:00
// session for execute response
2017-07-03 12:48:46 -06:00
bs = blade_sessionmgr_session_lookup ( blade_handle_sessionmgr_get ( bh ) , blade_rpc_request_sessionid_get ( brpcreq ) ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( bs ) ;
2017-07-12 03:35:49 -06:00
requester_nodeid = blade_rpcexecute_request_requester_nodeid_get ( brpcreq ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( requester_nodeid ) ;
2017-07-25 11:00:45 -06:00
// inner rpcexecute parameters
2017-07-12 03:35:49 -06:00
params = blade_rpcexecute_request_params_get ( brpcreq ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( params ) ;
ks_log ( KS_LOG_DEBUG , " Session (%s) test.join request processing \n " , blade_session_id_get ( bs ) ) ;
2017-07-25 11:00:45 -06:00
// add to participants
2017-06-21 17:15:53 -06:00
key = ks_pstrdup ( test - > pool , requester_nodeid ) ;
ks_assert ( key ) ;
2017-07-03 13:57:47 -05:00
2017-07-25 11:00:45 -06:00
// @todo to properly maintain protocol details tied to a specific node like this participants list requires a way to know if a specific node of interest goes offline to cleanup associated details
// refer back to work notes on ideas about this
2017-06-21 17:15:53 -06:00
ks_hash_write_lock ( test - > participants ) ;
ks_hash_insert ( test - > participants , ( void * ) key , ( void * ) KS_TRUE ) ;
ks_hash_write_unlock ( test - > participants ) ;
2017-07-25 11:00:45 -06:00
// authorize channels with the master for the requester
channels = cJSON_CreateArray ( ) ;
cJSON_AddItemToArray ( channels , cJSON_CreateString ( " channel " ) ) ;
2017-08-18 16:30:08 -06:00
for ( ks_hash_iterator_t * it = ks_hash_first ( test - > channels , KS_UNLOCKED ) ; it ; it = ks_hash_next ( & it ) ) {
void * key = NULL ;
void * value = NULL ;
2017-08-18 17:44:25 -05:00
ks_hash_this ( it , ( const void * * ) & key , NULL , & value ) ;
2017-08-18 16:30:08 -06:00
cJSON_AddItemToArray ( channels , cJSON_CreateString ( ( const char * ) key ) ) ;
}
2017-07-25 11:00:45 -06:00
2017-09-20 22:07:34 -06:00
blade_handle_rpcauthorize ( bh , requester_nodeid , KS_FALSE , " test " , channels , NULL , NULL ) ;
2017-07-25 11:00:45 -06:00
cJSON_Delete ( channels ) ;
2017-06-21 17:15:53 -06:00
2017-07-25 11:00:45 -06:00
// send rpcexecute response to the requester
2017-06-21 17:15:53 -06:00
result = cJSON_CreateObject ( ) ;
2017-07-12 03:35:49 -06:00
blade_rpcexecute_response_send ( brpcreq , result ) ;
2017-06-21 17:15:53 -06:00
2017-07-25 11:00:45 -06:00
cJSON_Delete ( result ) ;
blade_session_read_unlock ( bs ) ;
// broadcast to authorized nodes that have subscribed, that the requester has joined
2017-06-21 17:15:53 -06:00
params = cJSON_CreateObject ( ) ;
2017-07-25 11:00:45 -06:00
cJSON_AddStringToObject ( params , " joiner-nodeid " , requester_nodeid ) ;
2017-09-20 22:07:34 -06:00
blade_handle_rpcbroadcast ( bh , " test " , " channel " , " join " , params , NULL , NULL ) ;
2017-07-25 11:00:45 -06:00
cJSON_Delete ( params ) ;
2017-06-21 17:15:53 -06:00
return KS_FALSE ;
}
2017-08-01 16:30:25 -06:00
ks_bool_t test_leave_request_handler ( blade_rpc_request_t * brpcreq , void * data )
2017-06-21 17:15:53 -06:00
{
testproto_t * test = NULL ;
blade_handle_t * bh = NULL ;
blade_session_t * bs = NULL ;
const char * requester_nodeid = NULL ;
2017-07-03 13:57:47 -05:00
//const char *key = NULL;
2017-06-21 17:15:53 -06:00
cJSON * params = NULL ;
2017-08-01 16:30:25 -06:00
cJSON * channels = NULL ;
2017-06-21 17:15:53 -06:00
cJSON * result = NULL ;
ks_assert ( brpcreq ) ;
ks_assert ( data ) ;
2017-08-01 16:30:25 -06:00
test = ( testproto_t * ) data ;
2017-06-21 17:15:53 -06:00
bh = blade_rpc_request_handle_get ( brpcreq ) ;
ks_assert ( bh ) ;
2017-07-03 12:48:46 -06:00
bs = blade_sessionmgr_session_lookup ( blade_handle_sessionmgr_get ( bh ) , blade_rpc_request_sessionid_get ( brpcreq ) ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( bs ) ;
2017-07-12 03:35:49 -06:00
requester_nodeid = blade_rpcexecute_request_requester_nodeid_get ( brpcreq ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( requester_nodeid ) ;
2017-07-12 03:35:49 -06:00
params = blade_rpcexecute_request_params_get ( brpcreq ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( params ) ;
ks_log ( KS_LOG_DEBUG , " Session (%s) test.leave (%s) request processing \n " , blade_session_id_get ( bs ) , requester_nodeid ) ;
ks_hash_write_lock ( test - > participants ) ;
ks_hash_remove ( test - > participants , ( void * ) requester_nodeid ) ;
ks_hash_write_unlock ( test - > participants ) ;
2017-08-01 16:30:25 -06:00
// deauthorize channels with the master for the requester
channels = cJSON_CreateArray ( ) ;
cJSON_AddItemToArray ( channels , cJSON_CreateString ( " channel " ) ) ;
2017-08-18 16:30:08 -06:00
for ( ks_hash_iterator_t * it = ks_hash_first ( test - > channels , KS_UNLOCKED ) ; it ; it = ks_hash_next ( & it ) ) {
void * key = NULL ;
void * value = NULL ;
2017-08-18 17:44:25 -05:00
ks_hash_this ( it , ( const void * * ) & key , NULL , & value ) ;
2017-08-18 16:30:08 -06:00
cJSON_AddItemToArray ( channels , cJSON_CreateString ( ( const char * ) key ) ) ;
}
2017-08-01 16:30:25 -06:00
2017-09-20 22:07:34 -06:00
blade_handle_rpcauthorize ( bh , requester_nodeid , KS_TRUE , " test " , channels , NULL , NULL ) ;
2017-08-01 16:30:25 -06:00
cJSON_Delete ( channels ) ;
2017-06-21 17:15:53 -06:00
2017-08-01 16:30:25 -06:00
// send rpcexecute response to the requester
2017-06-21 17:15:53 -06:00
result = cJSON_CreateObject ( ) ;
2017-07-12 03:35:49 -06:00
blade_rpcexecute_response_send ( brpcreq , result ) ;
2017-06-21 17:15:53 -06:00
2017-08-01 16:30:25 -06:00
cJSON_Delete ( result ) ;
blade_session_read_unlock ( bs ) ;
// broadcast to authorized nodes that have subscribed, that the requester has left
2017-06-21 17:15:53 -06:00
params = cJSON_CreateObject ( ) ;
2017-07-25 11:00:45 -06:00
cJSON_AddStringToObject ( params , " leaver-nodeid " , requester_nodeid ) ;
2017-09-20 22:07:34 -06:00
blade_handle_rpcbroadcast ( bh , " test " , " channel " , " leave " , params , NULL , NULL ) ;
2017-06-21 17:15:53 -06:00
2017-08-01 16:30:25 -06:00
cJSON_Delete ( params ) ;
2017-06-21 17:15:53 -06:00
return KS_FALSE ;
}
2017-08-01 16:30:25 -06:00
ks_bool_t test_talk_request_handler ( blade_rpc_request_t * brpcreq , void * data )
2017-06-21 17:15:53 -06:00
{
2017-07-03 13:57:47 -05:00
//testproto_t *test = NULL;
2017-06-21 17:15:53 -06:00
blade_handle_t * bh = NULL ;
blade_session_t * bs = NULL ;
const char * requester_nodeid = NULL ;
const char * text = NULL ;
cJSON * params = NULL ;
cJSON * result = NULL ;
ks_assert ( brpcreq ) ;
ks_assert ( data ) ;
2017-08-01 16:30:25 -06:00
//test = (testproto_t *)data;
2017-06-21 17:15:53 -06:00
bh = blade_rpc_request_handle_get ( brpcreq ) ;
ks_assert ( bh ) ;
2017-07-03 12:48:46 -06:00
bs = blade_sessionmgr_session_lookup ( blade_handle_sessionmgr_get ( bh ) , blade_rpc_request_sessionid_get ( brpcreq ) ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( bs ) ;
2017-07-12 03:35:49 -06:00
requester_nodeid = blade_rpcexecute_request_requester_nodeid_get ( brpcreq ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( requester_nodeid ) ;
2017-07-12 03:35:49 -06:00
params = blade_rpcexecute_request_params_get ( brpcreq ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( params ) ;
text = cJSON_GetObjectCstr ( params , " text " ) ;
ks_assert ( text ) ;
ks_log ( KS_LOG_DEBUG , " Session (%s) test.talk (%s) request processing \n " , blade_session_id_get ( bs ) , requester_nodeid ) ;
2017-08-01 16:30:25 -06:00
// send rpcexecute response to the requester
2017-06-21 17:15:53 -06:00
result = cJSON_CreateObject ( ) ;
2017-07-12 03:35:49 -06:00
blade_rpcexecute_response_send ( brpcreq , result ) ;
2017-06-21 17:15:53 -06:00
2017-08-01 16:30:25 -06:00
cJSON_Delete ( result ) ;
blade_session_read_unlock ( bs ) ;
// broadcast to authorized nodes that have subscribed, that the requester has said something
2017-06-21 17:15:53 -06:00
params = cJSON_CreateObject ( ) ;
cJSON_AddStringToObject ( params , " text " , text ) ;
2017-07-25 11:00:45 -06:00
cJSON_AddStringToObject ( params , " talker-nodeid " , requester_nodeid ) ;
2017-09-20 22:07:34 -06:00
blade_handle_rpcbroadcast ( bh , " test " , " channel " , " talk " , params , NULL , NULL ) ;
2017-06-21 17:15:53 -06:00
2017-08-01 16:30:25 -06:00
cJSON_Delete ( params ) ;
2017-06-21 17:15:53 -06:00
return KS_FALSE ;
}
2017-09-13 08:41:40 -06:00
ks_bool_t test_presence_request_handler ( blade_rpc_request_t * brpcreq , void * data )
{
blade_handle_t * bh = NULL ;
blade_session_t * bs = NULL ;
const char * protocol = NULL ;
const char * channel = NULL ;
const char * event = NULL ;
cJSON * params = NULL ;
const char * nodeid = NULL ;
ks_assert ( brpcreq ) ;
ks_assert ( data ) ;
bh = blade_rpc_request_handle_get ( brpcreq ) ;
ks_assert ( bh ) ;
bs = blade_sessionmgr_session_lookup ( blade_handle_sessionmgr_get ( bh ) , blade_rpc_request_sessionid_get ( brpcreq ) ) ;
ks_assert ( bs ) ;
protocol = blade_rpcbroadcast_request_protocol_get ( brpcreq ) ;
channel = blade_rpcbroadcast_request_channel_get ( brpcreq ) ;
event = blade_rpcbroadcast_request_event_get ( brpcreq ) ;
params = blade_rpcbroadcast_request_params_get ( brpcreq ) ;
nodeid = cJSON_GetObjectCstr ( params , " nodeid " ) ;
2017-09-20 22:07:34 -06:00
ks_log ( KS_LOG_DEBUG , " Session (%s) presence (protocol %s, channel %s, event %s for %s) request processing \n " , blade_session_id_get ( bs ) , protocol , channel , event , nodeid ) ;
return KS_FALSE ;
}
ks_bool_t test_register_response_handler ( blade_rpc_response_t * brpcres , void * data )
{
blade_handle_t * bh = NULL ;
blade_session_t * bs = NULL ;
ks_assert ( brpcres ) ;
bh = blade_rpc_response_handle_get ( brpcres ) ;
ks_assert ( bh ) ;
bs = blade_sessionmgr_session_lookup ( blade_handle_sessionmgr_get ( bh ) , blade_rpc_response_sessionid_get ( brpcres ) ) ;
ks_assert ( bs ) ;
ks_log ( KS_LOG_DEBUG , " Session (%s) register response processing \n " , blade_session_id_get ( bs ) ) ;
blade_session_read_unlock ( bs ) ;
2017-09-13 08:41:40 -06:00
return KS_FALSE ;
}
2017-06-21 17:15:53 -06:00
int main ( int argc , char * * argv )
{
blade_handle_t * bh = NULL ;
ks_pool_t * pool = NULL ;
config_t config ;
config_setting_t * config_blade = NULL ;
const char * cfgpath = " testcon.cfg " ;
const char * autoconnect = NULL ;
ks_global_set_default_logger ( KS_LOG_LEVEL_DEBUG ) ;
blade_init ( ) ;
blade_handle_create ( & bh ) ;
ks_assert ( bh ) ;
2017-08-03 20:26:07 -06:00
pool = ks_pool_get ( bh ) ;
2017-06-21 17:15:53 -06:00
ks_assert ( pool ) ;
if ( argc > 1 ) autoconnect = argv [ 1 ] ;
config_init ( & config ) ;
if ( ! config_read_file ( & config , cfgpath ) ) {
ks_log ( KS_LOG_ERROR , " %s:%d - %s \n " , config_error_file ( & config ) , config_error_line ( & config ) , config_error_text ( & config ) ) ;
config_destroy ( & config ) ;
return EXIT_FAILURE ;
}
config_blade = config_lookup ( & config , " blade " ) ;
if ( ! config_blade ) {
ks_log ( KS_LOG_ERROR , " Missing 'blade' config group \n " ) ;
config_destroy ( & config ) ;
return EXIT_FAILURE ;
}
if ( config_setting_type ( config_blade ) ! = CONFIG_TYPE_GROUP ) {
ks_log ( KS_LOG_ERROR , " The 'blade' config setting is not a group \n " ) ;
return EXIT_FAILURE ;
}
if ( blade_handle_startup ( bh , config_blade ) ! = KS_STATUS_SUCCESS ) {
ks_log ( KS_LOG_ERROR , " Blade startup failed \n " ) ;
return EXIT_FAILURE ;
}
2017-08-18 16:30:08 -06:00
testproto_create ( & g_test , bh ) ;
2017-06-21 17:15:53 -06:00
if ( autoconnect ) {
blade_connection_t * bc = NULL ;
blade_identity_t * target = NULL ;
ks_bool_t connected = KS_FALSE ;
blade_rpc_t * brpc = NULL ;
2017-08-03 20:26:07 -06:00
blade_identity_create ( & target , ks_pool_get ( bh ) ) ;
2017-06-21 17:15:53 -06:00
if ( blade_identity_parse ( target , autoconnect ) = = KS_STATUS_SUCCESS ) connected = blade_handle_connect ( bh , & bc , target , NULL ) = = KS_STATUS_SUCCESS ;
blade_identity_destroy ( & target ) ;
if ( connected ) {
2017-07-25 11:00:45 -06:00
cJSON * channels = NULL ;
2017-09-20 22:07:34 -06:00
cJSON * entry = NULL ;
2017-07-25 11:00:45 -06:00
2017-09-20 22:07:34 -06:00
// @todo use session state change callback to know when the session is ready after blade.connect, this hack temporarily ensures it's ready before trying to publish upstream
2017-06-21 17:15:53 -06:00
ks_sleep_ms ( 3000 ) ;
2017-09-20 22:07:34 -06:00
blade_rpc_create ( & brpc , bh , " test.join " , " test " , test_join_request_handler , ( void * ) g_test ) ;
2017-07-03 12:48:46 -06:00
blade_rpcmgr_protocolrpc_add ( blade_handle_rpcmgr_get ( bh ) , brpc ) ;
2017-06-21 17:15:53 -06:00
2017-09-20 22:07:34 -06:00
blade_rpc_create ( & brpc , bh , " test.leave " , " test " , test_leave_request_handler , ( void * ) g_test ) ;
2017-07-03 12:48:46 -06:00
blade_rpcmgr_protocolrpc_add ( blade_handle_rpcmgr_get ( bh ) , brpc ) ;
2017-06-21 17:15:53 -06:00
2017-09-20 22:07:34 -06:00
blade_rpc_create ( & brpc , bh , " test.talk " , " test " , test_talk_request_handler , ( void * ) g_test ) ;
2017-07-03 12:48:46 -06:00
blade_rpcmgr_protocolrpc_add ( blade_handle_rpcmgr_get ( bh ) , brpc ) ;
2017-06-21 17:15:53 -06:00
2017-07-25 11:00:45 -06:00
channels = cJSON_CreateArray ( ) ;
2017-09-20 22:07:34 -06:00
entry = cJSON_CreateObject ( ) ;
cJSON_AddStringToObject ( entry , " name " , " channel " ) ;
cJSON_AddNumberToObject ( entry , " flags " , BLADE_CHANNEL_FLAGS_NONE ) ;
cJSON_AddItemToArray ( channels , entry ) ;
2017-07-25 11:00:45 -06:00
2017-09-20 22:07:34 -06:00
blade_handle_rpcpublish ( bh , BLADE_RPCPUBLISH_COMMAND_CONTROLLER_ADD , " test " , channels , test_publish_response_handler , ( void * ) g_test ) ;
2017-08-01 16:30:25 -06:00
cJSON_Delete ( channels ) ;
2017-06-21 17:15:53 -06:00
}
}
loop ( bh ) ;
blade_handle_destroy ( & bh ) ;
2017-08-18 16:30:08 -06:00
testproto_destroy ( & g_test ) ;
2017-06-21 17:15:53 -06:00
config_destroy ( & config ) ;
blade_shutdown ( ) ;
return 0 ;
}
void loop ( blade_handle_t * bh )
{
char buf [ CONSOLE_INPUT_MAX ] ;
while ( ! g_shutdown ) {
if ( ! fgets ( buf , CONSOLE_INPUT_MAX , stdin ) ) break ;
for ( int index = 0 ; buf [ index ] ; + + index ) {
if ( buf [ index ] = = ' \r ' | | buf [ index ] = = ' \n ' ) {
buf [ index ] = ' \0 ' ;
break ;
}
}
process_console_input ( bh , buf ) ;
}
}
void parse_argument ( char * * input , char * * arg , char terminator )
{
char * tmp ;
ks_assert ( input ) ;
ks_assert ( * input ) ;
ks_assert ( arg ) ;
tmp = * input ;
* arg = tmp ;
while ( * tmp & & * tmp ! = terminator ) + + tmp ;
if ( * tmp = = terminator ) {
* tmp = ' \0 ' ;
+ + tmp ;
}
* input = tmp ;
}
void process_console_input ( blade_handle_t * bh , char * line )
{
char * args = line ;
char * cmd = NULL ;
ks_bool_t found = KS_FALSE ;
parse_argument ( & args , & cmd , ' ' ) ;
ks_log ( KS_LOG_DEBUG , " Command: %s, Args: %s \n " , cmd , args ) ;
for ( int32_t index = 0 ; command_defs [ index ] . cmd ; + + index ) {
if ( ! strcmp ( command_defs [ index ] . cmd , cmd ) ) {
found = KS_TRUE ;
command_defs [ index ] . callback ( bh , args ) ;
}
}
if ( ! found ) ks_log ( KS_LOG_INFO , " Command '%s' unknown. \n " , cmd ) ;
}
void command_quit ( blade_handle_t * bh , char * args )
{
ks_assert ( bh ) ;
ks_assert ( args ) ;
g_shutdown = KS_TRUE ;
}
2017-09-20 22:07:34 -06:00
void command_presence ( blade_handle_t * bh , char * args )
2017-08-18 16:30:08 -06:00
{
cJSON * channels = NULL ;
ks_assert ( bh ) ;
ks_assert ( args ) ;
channels = cJSON_CreateArray ( ) ;
2017-09-20 22:07:34 -06:00
cJSON_AddItemToArray ( channels , cJSON_CreateString ( " join " ) ) ;
cJSON_AddItemToArray ( channels , cJSON_CreateString ( " leave " ) ) ;
2017-08-18 16:30:08 -06:00
2017-09-20 22:07:34 -06:00
blade_handle_rpcsubscribe ( bh , BLADE_RPCSUBSCRIBE_COMMAND_SUBSCRIBER_ADD , " blade.presence " , channels , NULL , NULL , test_presence_request_handler , ( void * ) g_test ) ;
2017-08-18 16:30:08 -06:00
}
2017-09-20 22:07:34 -06:00
void command_identity ( blade_handle_t * bh , char * args )
2017-09-13 08:41:40 -06:00
{
ks_assert ( bh ) ;
ks_assert ( args ) ;
2017-09-20 22:07:34 -06:00
blade_handle_rpcregister ( bh , " blade:testcon@freeswitch.com " , test_register_response_handler , NULL ) ;
2017-09-13 08:41:40 -06:00
}
2017-06-21 17:15:53 -06:00
/* For Emacs:
* Local Variables :
* mode : c
* indent - tabs - mode : t
* tab - width : 4
* c - basic - offset : 4
* End :
* For VIM :
* vim : set softtabstop = 4 shiftwidth = 4 tabstop = 4 noet :
*/