int ast_ari_websocket_session_write(struct ast_ari_websocket_session *session, struct ast_json *message) { RAII_VAR(char *, str, NULL, ast_json_free); #ifdef AST_DEVMODE if (!session->validator(message)) { ast_log(LOG_ERROR, "Outgoing message failed validation\n"); return ast_websocket_write_string(session->ws_session, VALIDATION_FAILED); } #endif str = ast_json_dump_string_format(message, ast_ari_json_format()); if (str == NULL) { ast_log(LOG_ERROR, "Failed to encode JSON object\n"); return -1; } ast_debug(3, "Examining ARI event (length %zu): \n%s\n", strlen(str), str); if (ast_websocket_write_string(session->ws_session, str)) { ast_log(LOG_NOTICE, "Problem occurred during websocket write, websocket closed\n"); return -1; } return 0; }
/*! * \brief Send a message to the given application. * \param app App to send the message to. * \param message Message to send. */ void app_send(struct stasis_app *app, struct ast_json *message) { stasis_app_cb handler; int debug; char eid[20]; RAII_VAR(void *, data, NULL, ao2_cleanup); if (ast_json_object_set(message, "asterisk_id", ast_json_string_create( ast_eid_to_str(eid, sizeof(eid), &ast_eid_default)))) { ast_log(AST_LOG_WARNING, "Failed to append EID to outgoing event %s\n", ast_json_string_get(ast_json_object_get(message, "type"))); } /* Copy off mutable state with lock held */ { SCOPED_AO2LOCK(lock, app); debug = app->debug; handler = app->handler; if (app->data) { ao2_ref(app->data, +1); data = app->data; } /* Name is immutable; no need to copy */ } if (debug) { char *dump = ast_json_dump_string_format(message, AST_JSON_PRETTY); ast_verb(0, "Dispatching message to Stasis app '%s':\n%s\n", app->name, dump); ast_json_free(dump); } if (!handler) { ast_verb(3, "Inactive Stasis app '%s' missed message\n", app->name); return; } handler(data, app->name, message); }