mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-11-13 13:07:06 +00:00
FS-10167: Preliminary support for blade.execute, tested with a basic test.echo across 3 nodes with a common master. Multitier routing is not yet fully supported
This commit is contained in:
1 parent
14d4f8d565
commit
076a9adbf6
15 files changed
+1350
-1365
No files matched your search
+100
-132
@@ -16,20 +16,98 @@ struct command_def_s {
|
||||
};
|
||||
|
||||
void command_quit(blade_handle_t *bh, char *args);
|
||||
void command_connect(blade_handle_t *bh, char *args);
|
||||
//void command_chat(blade_handle_t *bh, char *args);
|
||||
void command_execute(blade_handle_t *bh, char *args);
|
||||
|
||||
static const struct command_def_s command_defs[] = {
|
||||
{ "quit", command_quit },
|
||||
{ "connect", command_connect },
|
||||
// { "chat", command_chat },
|
||||
{ "execute", command_execute },
|
||||
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
//ks_bool_t on_blade_chat_join_response(blade_response_t *bres);
|
||||
//ks_bool_t on_blade_chat_message_event(blade_event_t *bev);
|
||||
//void on_blade_session_state_callback(blade_session_t *bs, blade_session_state_condition_t condition, void *data);
|
||||
ks_bool_t test_echo_response_handler(blade_rpc_response_t *brpcres, void *data)
|
||||
{
|
||||
blade_handle_t *bh = NULL;
|
||||
blade_session_t *bs = NULL;
|
||||
cJSON *result = NULL;
|
||||
const char *text = NULL;
|
||||
|
||||
ks_assert(brpcres);
|
||||
|
||||
bh = blade_rpc_response_handle_get(brpcres);
|
||||
ks_assert(bh);
|
||||
|
||||
bs = blade_handle_sessions_lookup(bh, blade_rpc_response_sessionid_get(brpcres));
|
||||
ks_assert(bs);
|
||||
|
||||
result = blade_protocol_execute_response_result_get(brpcres);
|
||||
ks_assert(result);
|
||||
|
||||
text = cJSON_GetObjectCstr(result, "text");
|
||||
ks_assert(text);
|
||||
|
||||
ks_log(KS_LOG_DEBUG, "Session (%s) test.echo response processing\n", blade_session_id_get(bs));
|
||||
|
||||
blade_session_read_unlock(bs);
|
||||
|
||||
ks_log(KS_LOG_DEBUG, "Session (%s) test.echo: %s\n", blade_session_id_get(bs), text);
|
||||
|
||||
return KS_FALSE;
|
||||
}
|
||||
|
||||
ks_bool_t blade_locate_response_handler(blade_rpc_response_t *brpcres, void *data)
|
||||
{
|
||||
blade_handle_t *bh = NULL;
|
||||
blade_session_t *bs = NULL;
|
||||
const char *nodeid = NULL;
|
||||
cJSON *res = NULL;
|
||||
cJSON *res_result = NULL;
|
||||
cJSON *res_result_providers = NULL;
|
||||
const char *res_result_protocol = NULL;
|
||||
const char *res_result_realm = NULL;
|
||||
cJSON *params = NULL;
|
||||
|
||||
ks_assert(brpcres);
|
||||
|
||||
bh = blade_rpc_response_handle_get(brpcres);
|
||||
ks_assert(bh);
|
||||
|
||||
bs = blade_handle_sessions_lookup(bh, blade_rpc_response_sessionid_get(brpcres));
|
||||
ks_assert(bs);
|
||||
|
||||
res = blade_rpc_response_message_get(brpcres);
|
||||
ks_assert(res);
|
||||
|
||||
res_result = cJSON_GetObjectItem(res, "result");
|
||||
ks_assert(res_result);
|
||||
|
||||
res_result_protocol = cJSON_GetObjectCstr(res_result, "protocol");
|
||||
ks_assert(res_result_protocol);
|
||||
|
||||
res_result_realm = cJSON_GetObjectCstr(res_result, "realm");
|
||||
ks_assert(res_result_realm);
|
||||
|
||||
res_result_providers = cJSON_GetObjectItem(res_result, "providers");
|
||||
ks_assert(res_result_providers);
|
||||
|
||||
ks_log(KS_LOG_DEBUG, "Session (%s) blade.locate response processing\n", blade_session_id_get(bs));
|
||||
|
||||
for (int index = 0; index < cJSON_GetArraySize(res_result_providers); ++index) {
|
||||
cJSON *elem = cJSON_GetArrayItem(res_result_providers, index);
|
||||
if (elem->type == cJSON_String) {
|
||||
ks_log(KS_LOG_DEBUG, "Session (%s) blade.locate (%s@%s) provider (%s)\n", blade_session_id_get(bs), res_result_protocol, res_result_realm, elem->valuestring);
|
||||
nodeid = elem->valuestring;
|
||||
}
|
||||
}
|
||||
|
||||
blade_session_read_unlock(bs);
|
||||
|
||||
params = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(params, "text", "hello world!");
|
||||
blade_protocol_execute(bh, nodeid, "test.echo", res_result_protocol, res_result_realm, params, test_echo_response_handler, NULL);
|
||||
|
||||
return KS_FALSE;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
@@ -71,9 +149,6 @@ int main(int argc, char **argv)
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
//blade_handle_event_register(bh, "blade.chat.message", on_blade_chat_message_event);
|
||||
//blade_handle_session_state_callback_register(bh, NULL, on_blade_session_state_callback, &session_state_callback_id);
|
||||
|
||||
if (autoconnect) {
|
||||
blade_connection_t *bc = NULL;
|
||||
blade_identity_t *target = NULL;
|
||||
@@ -85,13 +160,9 @@ int main(int argc, char **argv)
|
||||
blade_identity_destroy(&target);
|
||||
|
||||
ks_sleep_ms(5000);
|
||||
|
||||
blade_protocol_publish(bh, "test", "mydomain.com");
|
||||
|
||||
ks_sleep_ms(5000);
|
||||
} else loop(bh);
|
||||
|
||||
//blade_handle_session_state_callback_unregister(bh, session_state_callback_id);
|
||||
}
|
||||
|
||||
loop(bh);
|
||||
|
||||
blade_handle_destroy(&bh);
|
||||
|
||||
@@ -102,38 +173,6 @@ int main(int argc, char **argv)
|
||||
return 0;
|
||||
}
|
||||
|
||||
//ks_bool_t on_blade_chat_message_event(blade_event_t *bev)
|
||||
//{
|
||||
// cJSON *res = NULL;
|
||||
// const char *from = NULL;
|
||||
// const char *message = NULL;
|
||||
//
|
||||
// ks_assert(bev);
|
||||
//
|
||||
// res = cJSON_GetObjectItem(bev->message, "result");
|
||||
// from = cJSON_GetObjectCstr(res, "from");
|
||||
// message = cJSON_GetObjectCstr(res, "message");
|
||||
//
|
||||
// ks_log(KS_LOG_DEBUG, "Received Chat Message Event: (%s) %s\n", from, message);
|
||||
//
|
||||
// return KS_FALSE;
|
||||
//}
|
||||
//
|
||||
//void on_blade_session_state_callback(blade_session_t *bs, blade_session_state_condition_t condition, void *data)
|
||||
//{
|
||||
// blade_session_state_t state = blade_session_state_get(bs);
|
||||
//
|
||||
// if (condition == BLADE_SESSION_STATE_CONDITION_PRE) {
|
||||
// ks_log(KS_LOG_DEBUG, "Blade Session State Changed: %s, %d\n", blade_session_id_get(bs), state);
|
||||
// if (state == BLADE_SESSION_STATE_READY) {
|
||||
// cJSON *req = NULL;
|
||||
// blade_jsonrpc_request_raw_create(blade_handle_pool_get(blade_session_handle_get(bs)), &req, NULL, NULL, "blade.chat.join");
|
||||
// blade_session_send(bs, req, on_blade_chat_join_response);
|
||||
// cJSON_Delete(req);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
void loop(blade_handle_t *bh)
|
||||
{
|
||||
char buf[CONSOLE_INPUT_MAX];
|
||||
@@ -199,92 +238,21 @@ void command_quit(blade_handle_t *bh, char *args)
|
||||
g_shutdown = KS_TRUE;
|
||||
}
|
||||
|
||||
void command_connect(blade_handle_t *bh, char *args)
|
||||
void command_execute(blade_handle_t *bh, char *args)
|
||||
{
|
||||
blade_connection_t *bc = NULL;
|
||||
blade_identity_t *target = NULL;
|
||||
|
||||
ks_assert(bh);
|
||||
ks_assert(args);
|
||||
|
||||
blade_identity_create(&target, blade_handle_pool_get(bh));
|
||||
|
||||
if (blade_identity_parse(target, args) == KS_STATUS_SUCCESS) blade_handle_connect(bh, &bc, target, NULL);
|
||||
|
||||
blade_identity_destroy(&target);
|
||||
blade_protocol_locate(bh, "test", "mydomain.com", blade_locate_response_handler, NULL);
|
||||
}
|
||||
|
||||
//ks_bool_t on_blade_chat_send_response(blade_response_t *bres);
|
||||
//
|
||||
//ks_bool_t on_blade_chat_join_response(blade_response_t *bres) // @todo this should get userdata passed in from when the callback is registered
|
||||
//{
|
||||
// blade_session_t *bs = NULL;
|
||||
// cJSON *req = NULL;
|
||||
// cJSON *params = NULL;
|
||||
//
|
||||
// ks_log(KS_LOG_DEBUG, "Received Chat Join Response!\n");
|
||||
//
|
||||
// bs = blade_handle_sessions_get(bres->handle, bres->session_id);
|
||||
// if (!bs) {
|
||||
// ks_log(KS_LOG_DEBUG, "Unknown Session: %s\n", bres->session_id);
|
||||
// return KS_FALSE;
|
||||
// }
|
||||
//
|
||||
// blade_jsonrpc_request_raw_create(blade_handle_pool_get(bres->handle), &req, ¶ms, NULL, "blade.chat.send");
|
||||
// ks_assert(req);
|
||||
// ks_assert(params);
|
||||
//
|
||||
// cJSON_AddStringToObject(params, "message", "Hello World!");
|
||||
//
|
||||
// blade_session_send(bs, req, on_blade_chat_send_response);
|
||||
//
|
||||
// blade_session_read_unlock(bs);
|
||||
//
|
||||
// return KS_FALSE;
|
||||
//}
|
||||
//
|
||||
//ks_bool_t on_blade_chat_send_response(blade_response_t *bres) // @todo this should get userdata passed in from when the callback is registered
|
||||
//{
|
||||
// ks_log(KS_LOG_DEBUG, "Received Chat Send Response!\n");
|
||||
// return KS_FALSE;
|
||||
//}
|
||||
//
|
||||
//void command_chat(blade_handle_t *bh, char *args)
|
||||
//{
|
||||
// char *cmd = NULL;
|
||||
//
|
||||
// ks_assert(bh);
|
||||
// ks_assert(args);
|
||||
//
|
||||
// parse_argument(&args, &cmd, ' ');
|
||||
// ks_log(KS_LOG_DEBUG, "Chat Command: %s, Args: %s\n", cmd, args);
|
||||
//
|
||||
// if (!strcmp(cmd, "leave")) {
|
||||
// } else if (!strcmp(cmd, "send")) {
|
||||
// char *sid = NULL;
|
||||
// blade_session_t *bs = NULL;
|
||||
// cJSON *req = NULL;
|
||||
// cJSON *params = NULL;
|
||||
//
|
||||
// parse_argument(&args, &sid, ' ');
|
||||
//
|
||||
// bs = blade_handle_sessions_get(bh, sid);
|
||||
// if (!bs) {
|
||||
// ks_log(KS_LOG_DEBUG, "Unknown Session: %s\n", sid);
|
||||
// return;
|
||||
// }
|
||||
// blade_jsonrpc_request_raw_create(blade_handle_pool_get(bh), &req, ¶ms, NULL, "blade.chat.send");
|
||||
// ks_assert(req);
|
||||
// ks_assert(params);
|
||||
//
|
||||
// cJSON_AddStringToObject(params, "message", args);
|
||||
//
|
||||
// blade_session_send(bs, req, on_blade_chat_send_response);
|
||||
//
|
||||
// blade_session_read_unlock(bs);
|
||||
//
|
||||
// cJSON_Delete(req);
|
||||
// } else {
|
||||
// ks_log(KS_LOG_DEBUG, "Unknown Chat Command: %s\n", cmd);
|
||||
// }
|
||||
//}
|
||||
/* 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:
|
||||
*/
|
||||
+62
-372
@@ -16,45 +16,70 @@ struct command_def_s {
|
||||
};
|
||||
|
||||
void command_quit(blade_handle_t *bh, char *args);
|
||||
void command_locate(blade_handle_t *bh, char *args);
|
||||
|
||||
static const struct command_def_s command_defs[] = {
|
||||
{ "quit", command_quit },
|
||||
{ "locate", command_locate },
|
||||
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
|
||||
|
||||
//ks_status_t blade_module_chat_create(blade_module_t **bmP, blade_handle_t *bh);
|
||||
//ks_status_t blade_module_chat_on_startup(blade_module_t *bm, config_setting_t *config);
|
||||
//ks_status_t blade_module_chat_on_shutdown(blade_module_t *bm);
|
||||
//
|
||||
//typedef struct blade_module_chat_s blade_module_chat_t;
|
||||
//struct blade_module_chat_s {
|
||||
// blade_handle_t *handle;
|
||||
// ks_pool_t *pool;
|
||||
// ks_thread_pool_t *tpool;
|
||||
// blade_module_t *module;
|
||||
// //blade_module_callbacks_t *module_callbacks;
|
||||
//
|
||||
// blade_space_t *blade_chat_space;
|
||||
// const char *session_state_callback_id;
|
||||
// ks_list_t *participants;
|
||||
//};
|
||||
//
|
||||
//void blade_module_chat_on_session_state(blade_session_t *bs, blade_session_state_condition_t condition, void *data);
|
||||
//
|
||||
//ks_bool_t blade_chat_join_request_handler(blade_module_t *bm, blade_request_t *breq);
|
||||
//ks_bool_t blade_chat_leave_request_handler(blade_module_t *bm, blade_request_t *breq);
|
||||
//ks_bool_t blade_chat_send_request_handler(blade_module_t *bm, blade_request_t *breq);
|
||||
//
|
||||
//static blade_module_callbacks_t g_module_chat_callbacks =
|
||||
//{
|
||||
// blade_module_chat_on_startup,
|
||||
// blade_module_chat_on_shutdown,
|
||||
//};
|
||||
ks_bool_t blade_publish_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_handle_sessions_lookup(bh, blade_rpc_response_sessionid_get(brpcres));
|
||||
ks_assert(bs);
|
||||
|
||||
ks_log(KS_LOG_DEBUG, "Session (%s) blade.publish response processing\n", blade_session_id_get(bs));
|
||||
|
||||
blade_session_read_unlock(bs);
|
||||
|
||||
return KS_FALSE;
|
||||
}
|
||||
|
||||
ks_bool_t test_echo_request_handler(blade_rpc_request_t *brpcreq, void *data)
|
||||
{
|
||||
blade_handle_t *bh = NULL;
|
||||
blade_session_t *bs = NULL;
|
||||
cJSON *params = NULL;
|
||||
cJSON *result = NULL;
|
||||
const char *text = NULL;
|
||||
|
||||
ks_assert(brpcreq);
|
||||
|
||||
bh = blade_rpc_request_handle_get(brpcreq);
|
||||
ks_assert(bh);
|
||||
|
||||
bs = blade_handle_sessions_lookup(bh, blade_rpc_response_sessionid_get(brpcreq));
|
||||
ks_assert(bs);
|
||||
|
||||
// @todo get the inner parameters of a blade.execute request for protocolrpcs
|
||||
params = blade_protocol_execute_request_params_get(brpcreq);
|
||||
ks_assert(params);
|
||||
|
||||
text = cJSON_GetObjectCstr(params, "text");
|
||||
ks_assert(text);
|
||||
|
||||
ks_log(KS_LOG_DEBUG, "Session (%s) test.echo request processing\n", blade_session_id_get(bs));
|
||||
|
||||
blade_session_read_unlock(bs);
|
||||
|
||||
// @todo build and send response
|
||||
result = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(result, "text", text);
|
||||
|
||||
blade_protocol_execute_response_send(brpcreq, result);
|
||||
|
||||
return KS_FALSE;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
@@ -62,8 +87,6 @@ int main(int argc, char **argv)
|
||||
blade_handle_t *bh = NULL;
|
||||
config_t config;
|
||||
config_setting_t *config_blade = NULL;
|
||||
//blade_module_t *mod_chat = NULL;
|
||||
//blade_identity_t *id = NULL;
|
||||
const char *cfgpath = "blades.cfg";
|
||||
const char *autoconnect = NULL;
|
||||
|
||||
@@ -73,7 +96,6 @@ int main(int argc, char **argv)
|
||||
|
||||
blade_handle_create(&bh);
|
||||
|
||||
//if (argc > 1) cfgpath = argv[1];
|
||||
if (argc > 1) autoconnect = argv[1];
|
||||
|
||||
config_init(&config);
|
||||
@@ -93,10 +115,6 @@ int main(int argc, char **argv)
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// must occur before startup
|
||||
//blade_module_chat_create(&mod_chat, bh);
|
||||
//blade_handle_module_register(mod_chat);
|
||||
|
||||
if (blade_handle_startup(bh, config_blade) != KS_STATUS_SUCCESS) {
|
||||
ks_log(KS_LOG_ERROR, "Blade startup failed\n");
|
||||
return EXIT_FAILURE;
|
||||
@@ -105,6 +123,7 @@ int main(int argc, char **argv)
|
||||
if (autoconnect) {
|
||||
blade_connection_t *bc = NULL;
|
||||
blade_identity_t *target = NULL;
|
||||
blade_rpc_t *brpc = NULL;
|
||||
|
||||
blade_identity_create(&target, blade_handle_pool_get(bh));
|
||||
|
||||
@@ -112,9 +131,13 @@ int main(int argc, char **argv)
|
||||
|
||||
blade_identity_destroy(&target);
|
||||
|
||||
ks_sleep_ms(5000); // @todo use session state change callback to know when the session is ready, this ensures it's ready before trying to publish upstream
|
||||
ks_sleep_ms(5000); // @todo use session state change callback to know when the session is ready, this hack temporarily ensures it's ready before trying to publish upstream
|
||||
|
||||
blade_protocol_publish(bh, "test", "mydomain.com");
|
||||
blade_rpc_create(&brpc, bh, "test.echo", "test", "mydomain.com", test_echo_request_handler, NULL);
|
||||
blade_handle_protocolrpc_register(brpc);
|
||||
|
||||
// @todo build up json-based method schema for each protocolrpc registered above, and pass into blade_protocol_publish() to attach to the request, to be stored in the blade_protocol_t tracked by the master node
|
||||
blade_protocol_publish(bh, "test", "mydomain.com", blade_publish_response_handler, NULL);
|
||||
}
|
||||
|
||||
loop(bh);
|
||||
@@ -193,339 +216,6 @@ void command_quit(blade_handle_t *bh, char *args)
|
||||
g_shutdown = KS_TRUE;
|
||||
}
|
||||
|
||||
void command_locate(blade_handle_t *bh, char *args)
|
||||
{
|
||||
ks_assert(bh);
|
||||
ks_assert(args);
|
||||
|
||||
blade_protocol_locate(bh, "test", "mydomain.com");
|
||||
}
|
||||
|
||||
|
||||
|
||||
//static void blade_module_chat_cleanup(ks_pool_t *pool, void *ptr, void *arg, ks_pool_cleanup_action_t action, ks_pool_cleanup_type_t type)
|
||||
//{
|
||||
// blade_module_chat_t *bm_chat = (blade_module_chat_t *)ptr;
|
||||
//
|
||||
// ks_assert(bm_chat);
|
||||
//
|
||||
// switch (action) {
|
||||
// case KS_MPCL_ANNOUNCE:
|
||||
// break;
|
||||
// case KS_MPCL_TEARDOWN:
|
||||
// break;
|
||||
// case KS_MPCL_DESTROY:
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//
|
||||
//ks_status_t blade_module_chat_create(blade_module_t **bmP, blade_handle_t *bh)
|
||||
//{
|
||||
// blade_module_chat_t *bm_chat = NULL;
|
||||
// ks_pool_t *pool = NULL;
|
||||
//
|
||||
// ks_assert(bmP);
|
||||
// ks_assert(bh);
|
||||
//
|
||||
// ks_pool_open(&pool);
|
||||
// ks_assert(pool);
|
||||
//
|
||||
// bm_chat = ks_pool_alloc(pool, sizeof(blade_module_chat_t));
|
||||
// bm_chat->handle = bh;
|
||||
// bm_chat->pool = pool;
|
||||
// bm_chat->tpool = blade_handle_tpool_get(bh);
|
||||
// bm_chat->session_state_callback_id = NULL;
|
||||
//
|
||||
// ks_list_create(&bm_chat->participants, pool);
|
||||
// ks_assert(bm_chat->participants);
|
||||
//
|
||||
// blade_module_create(&bm_chat->module, bh, pool, bm_chat, &g_module_chat_callbacks);
|
||||
// ks_assert(bm_chat->module);
|
||||
//
|
||||
// ks_pool_set_cleanup(pool, bm_chat, NULL, blade_module_chat_cleanup);
|
||||
//
|
||||
// ks_log(KS_LOG_DEBUG, "Created\n");
|
||||
//
|
||||
// *bmP = bm_chat->module;
|
||||
//
|
||||
// return KS_STATUS_SUCCESS;
|
||||
//}
|
||||
//
|
||||
//
|
||||
//ks_status_t blade_module_chat_config(blade_module_chat_t *bm_chat, config_setting_t *config)
|
||||
//{
|
||||
// config_setting_t *chat = NULL;
|
||||
//
|
||||
// ks_assert(bm_chat);
|
||||
// ks_assert(config);
|
||||
//
|
||||
// if (!config_setting_is_group(config)) {
|
||||
// ks_log(KS_LOG_DEBUG, "!config_setting_is_group(config)\n");
|
||||
// return KS_STATUS_FAIL;
|
||||
// }
|
||||
//
|
||||
// chat = config_setting_get_member(config, "chat");
|
||||
// if (chat) {
|
||||
// }
|
||||
//
|
||||
//
|
||||
// // Configuration is valid, now assign it to the variables that are used
|
||||
// // If the configuration was invalid, then this does not get changed
|
||||
//
|
||||
// ks_log(KS_LOG_DEBUG, "Configured\n");
|
||||
//
|
||||
// return KS_STATUS_SUCCESS;
|
||||
//}
|
||||
//
|
||||
//ks_status_t blade_module_chat_on_startup(blade_module_t *bm, config_setting_t *config)
|
||||
//{
|
||||
// blade_module_chat_t *bm_chat = NULL;
|
||||
// blade_space_t *space = NULL;
|
||||
// blade_method_t *method = NULL;
|
||||
//
|
||||
// ks_assert(bm);
|
||||
// ks_assert(config);
|
||||
//
|
||||
// bm_chat = (blade_module_chat_t *)blade_module_data_get(bm);
|
||||
//
|
||||
// if (blade_module_chat_config(bm_chat, config) != KS_STATUS_SUCCESS) {
|
||||
// ks_log(KS_LOG_DEBUG, "blade_module_chat_config failed\n");
|
||||
// return KS_STATUS_FAIL;
|
||||
// }
|
||||
//
|
||||
// blade_space_create(&space, bm_chat->handle, bm, "blade.chat");
|
||||
// ks_assert(space);
|
||||
//
|
||||
// bm_chat->blade_chat_space = space;
|
||||
//
|
||||
// blade_method_create(&method, space, "join", blade_chat_join_request_handler);
|
||||
// ks_assert(method);
|
||||
// blade_space_methods_add(space, method);
|
||||
//
|
||||
// blade_method_create(&method, space, "leave", blade_chat_leave_request_handler);
|
||||
// ks_assert(method);
|
||||
// blade_space_methods_add(space, method);
|
||||
//
|
||||
// blade_method_create(&method, space, "send", blade_chat_send_request_handler);
|
||||
// ks_assert(method);
|
||||
// blade_space_methods_add(space, method);
|
||||
//
|
||||
// blade_handle_space_register(space);
|
||||
//
|
||||
// blade_handle_session_state_callback_register(blade_module_handle_get(bm), bm, blade_module_chat_on_session_state, &bm_chat->session_state_callback_id);
|
||||
//
|
||||
// ks_log(KS_LOG_DEBUG, "Started\n");
|
||||
//
|
||||
// return KS_STATUS_SUCCESS;
|
||||
//}
|
||||
//
|
||||
//ks_status_t blade_module_chat_on_shutdown(blade_module_t *bm)
|
||||
//{
|
||||
// blade_module_chat_t *bm_chat = NULL;
|
||||
//
|
||||
// ks_assert(bm);
|
||||
//
|
||||
// bm_chat = (blade_module_chat_t *)blade_module_data_get(bm);
|
||||
// ks_assert(bm_chat);
|
||||
//
|
||||
// if (bm_chat->session_state_callback_id) blade_handle_session_state_callback_unregister(blade_module_handle_get(bm), bm_chat->session_state_callback_id);
|
||||
// bm_chat->session_state_callback_id = NULL;
|
||||
//
|
||||
// if (bm_chat->blade_chat_space) blade_handle_space_unregister(bm_chat->blade_chat_space);
|
||||
//
|
||||
// ks_log(KS_LOG_DEBUG, "Stopped\n");
|
||||
//
|
||||
// return KS_STATUS_SUCCESS;
|
||||
//}
|
||||
//
|
||||
//void blade_module_chat_on_session_state(blade_session_t *bs, blade_session_state_condition_t condition, void *data)
|
||||
//{
|
||||
// blade_module_t *bm = NULL;
|
||||
// blade_module_chat_t *bm_chat = NULL;
|
||||
//
|
||||
// ks_assert(bs);
|
||||
// ks_assert(data);
|
||||
//
|
||||
// bm = (blade_module_t *)data;
|
||||
// bm_chat = (blade_module_chat_t *)blade_module_data_get(bm);
|
||||
// ks_assert(bm_chat);
|
||||
//
|
||||
// if (blade_session_state_get(bs) == BLADE_SESSION_STATE_HANGUP && condition == BLADE_SESSION_STATE_CONDITION_PRE) {
|
||||
// cJSON *props = NULL;
|
||||
//
|
||||
// ks_log(KS_LOG_DEBUG, "Removing session from chat participants if present\n");
|
||||
//
|
||||
// props = blade_session_properties_get(bs);
|
||||
// ks_assert(props);
|
||||
//
|
||||
// cJSON_DeleteItemFromObject(props, "blade.chat.participant");
|
||||
//
|
||||
// ks_list_delete(bm_chat->participants, blade_session_id_get(bs)); // @todo make copy of session id instead and search manually, also free the id
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//ks_bool_t blade_chat_join_request_handler(blade_module_t *bm, blade_request_t *breq)
|
||||
//{
|
||||
// blade_module_chat_t *bm_chat = NULL;
|
||||
// blade_session_t *bs = NULL;
|
||||
// cJSON *res = NULL;
|
||||
// cJSON *props = NULL;
|
||||
// cJSON *props_participant = NULL;
|
||||
//
|
||||
// ks_assert(bm);
|
||||
// ks_assert(breq);
|
||||
//
|
||||
// ks_log(KS_LOG_DEBUG, "Request Received!\n");
|
||||
//
|
||||
// bm_chat = (blade_module_chat_t *)blade_module_data_get(bm);
|
||||
// ks_assert(bm_chat);
|
||||
//
|
||||
// bs = blade_handle_sessions_get(breq->handle, breq->session_id);
|
||||
// ks_assert(bs);
|
||||
//
|
||||
// // @todo properties only used to demonstrate a flexible container for session data, should just rely on the participants list/hash
|
||||
// blade_session_properties_write_lock(bs, KS_TRUE);
|
||||
//
|
||||
// props = blade_session_properties_get(bs);
|
||||
// ks_assert(props);
|
||||
//
|
||||
// props_participant = cJSON_GetObjectItem(props, "blade.chat.participant");
|
||||
// if (props_participant && props_participant->type == cJSON_True) {
|
||||
// ks_log(KS_LOG_DEBUG, "Session (%s) attempted to join chat but is already a participant\n", blade_session_id_get(bs));
|
||||
// blade_rpc_error_create(&res, NULL, breq->message_id, -10000, "Already a participant of chat");
|
||||
// }
|
||||
// else {
|
||||
// ks_log(KS_LOG_DEBUG, "Session (%s) joined chat\n", blade_session_id_get(bs));
|
||||
//
|
||||
// if (props_participant) props_participant->type = cJSON_True;
|
||||
// else cJSON_AddTrueToObject(props, "blade.chat.participant");
|
||||
//
|
||||
// ks_list_append(bm_chat->participants, blade_session_id_get(bs)); // @todo make copy of session id instead and cleanup when removed
|
||||
//
|
||||
// blade_rpc_response_create(&res, NULL, breq->message_id);
|
||||
//
|
||||
// // @todo create an event to send to participants when a session joins and leaves, send after main response though
|
||||
// }
|
||||
//
|
||||
// blade_session_properties_write_unlock(bs);
|
||||
//
|
||||
// blade_session_send(bs, res, NULL);
|
||||
//
|
||||
// blade_session_read_unlock(bs);
|
||||
//
|
||||
// cJSON_Delete(res);
|
||||
//
|
||||
// return KS_FALSE;
|
||||
//}
|
||||
//
|
||||
//ks_bool_t blade_chat_leave_request_handler(blade_module_t *bm, blade_request_t *breq)
|
||||
//{
|
||||
// blade_module_chat_t *bm_chat = NULL;
|
||||
// blade_session_t *bs = NULL;
|
||||
// cJSON *res = NULL;
|
||||
// cJSON *props = NULL;
|
||||
// cJSON *props_participant = NULL;
|
||||
//
|
||||
// ks_assert(bm);
|
||||
// ks_assert(breq);
|
||||
//
|
||||
// ks_log(KS_LOG_DEBUG, "Request Received!\n");
|
||||
//
|
||||
// bm_chat = (blade_module_chat_t *)blade_module_data_get(bm);
|
||||
// ks_assert(bm_chat);
|
||||
//
|
||||
// bs = blade_handle_sessions_get(breq->handle, breq->session_id);
|
||||
// ks_assert(bs);
|
||||
//
|
||||
// blade_session_properties_write_lock(bs, KS_TRUE);
|
||||
//
|
||||
// props = blade_session_properties_get(bs);
|
||||
// ks_assert(props);
|
||||
//
|
||||
// props_participant = cJSON_GetObjectItem(props, "blade.chat.participant");
|
||||
// if (!props_participant || props_participant->type == cJSON_False) {
|
||||
// ks_log(KS_LOG_DEBUG, "Session (%s) attempted to leave chat but is not a participant\n", blade_session_id_get(bs));
|
||||
// blade_rpc_error_create(&res, NULL, breq->message_id, -10000, "Not a participant of chat");
|
||||
// }
|
||||
// else {
|
||||
// ks_log(KS_LOG_DEBUG, "Session (%s) left chat\n", blade_session_id_get(bs));
|
||||
//
|
||||
// cJSON_DeleteItemFromObject(props, "blade.chat.participant");
|
||||
//
|
||||
// ks_list_delete(bm_chat->participants, blade_session_id_get(bs)); // @todo make copy of session id instead and search manually, also free the id
|
||||
//
|
||||
// blade_rpc_response_create(&res, NULL, breq->message_id);
|
||||
//
|
||||
// // @todo create an event to send to participants when a session joins and leaves, send after main response though
|
||||
// }
|
||||
//
|
||||
// blade_session_properties_write_unlock(bs);
|
||||
//
|
||||
// blade_session_send(bs, res, NULL);
|
||||
//
|
||||
// blade_session_read_unlock(bs);
|
||||
//
|
||||
// cJSON_Delete(res);
|
||||
//
|
||||
// return KS_FALSE;
|
||||
//}
|
||||
//
|
||||
//ks_bool_t blade_chat_send_request_handler(blade_module_t *bm, blade_request_t *breq)
|
||||
//{
|
||||
// blade_module_chat_t *bm_chat = NULL;
|
||||
// blade_session_t *bs = NULL;
|
||||
// cJSON *params = NULL;
|
||||
// cJSON *res = NULL;
|
||||
// cJSON *event = NULL;
|
||||
// const char *message = NULL;
|
||||
// ks_bool_t sendevent = KS_FALSE;
|
||||
//
|
||||
// ks_assert(bm);
|
||||
// ks_assert(breq);
|
||||
//
|
||||
// ks_log(KS_LOG_DEBUG, "Request Received!\n");
|
||||
//
|
||||
// bm_chat = (blade_module_chat_t *)blade_module_data_get(bm);
|
||||
// ks_assert(bm_chat);
|
||||
//
|
||||
// params = cJSON_GetObjectItem(breq->message, "params"); // @todo cache this in blade_request_t for quicker/easier access
|
||||
// if (!params) {
|
||||
// ks_log(KS_LOG_DEBUG, "Session (%s) attempted to send chat message with no 'params' object\n", blade_session_id_get(bs));
|
||||
// blade_rpc_error_create(&res, NULL, breq->message_id, -32602, "Missing params object");
|
||||
// }
|
||||
// else if (!(message = cJSON_GetObjectCstr(params, "message"))) {
|
||||
// ks_log(KS_LOG_DEBUG, "Session (%s) attempted to send chat message with no 'message'\n", blade_session_id_get(bs));
|
||||
// blade_rpc_error_create(&res, NULL, breq->message_id, -32602, "Missing params message string");
|
||||
// }
|
||||
//
|
||||
// bs = blade_handle_sessions_get(breq->handle, breq->session_id);
|
||||
// ks_assert(bs);
|
||||
//
|
||||
// if (!res) {
|
||||
// blade_rpc_response_create(&res, NULL, breq->message_id);
|
||||
// sendevent = KS_TRUE;
|
||||
// }
|
||||
// blade_session_send(bs, res, NULL);
|
||||
//
|
||||
// blade_session_read_unlock(bs);
|
||||
//
|
||||
// cJSON_Delete(res);
|
||||
//
|
||||
// if (sendevent) {
|
||||
// blade_rpc_event_create(&event, &res, "blade.chat.message");
|
||||
// ks_assert(event);
|
||||
// cJSON_AddStringToObject(res, "from", breq->session_id); // @todo should really be the identity, but we don't have that in place yet
|
||||
// cJSON_AddStringToObject(res, "message", message);
|
||||
//
|
||||
// blade_handle_sessions_send(breq->handle, bm_chat->participants, NULL, event);
|
||||
//
|
||||
// cJSON_Delete(event);
|
||||
// }
|
||||
//
|
||||
// return KS_FALSE;
|
||||
//}
|
||||
|
||||
|
||||
/* For Emacs:
|
||||
* Local Variables:
|
||||
|
||||
Reference in new issue
Block a user