提交 07e0d4f9 authored 作者: Andrey Volk's avatar Andrey Volk

FS-10801: [core] Add a database interface to the FreeSWITCH Core.

上级 fbbac957
......@@ -24,6 +24,7 @@
* Contributor(s):
*
* Anthony Minessale II <anthm@freeswitch.org>
* Andrey Volk <andywolk@gmail.com>
*
*
* switch_core.h -- Core Library Private Data (not to be installed into the system)
......@@ -334,6 +335,8 @@ extern struct switch_session_manager session_manager;
switch_status_t switch_core_sqldb_init(const char **err);
void switch_core_sqldb_destroy();
switch_status_t switch_core_sqldb_start(switch_memory_pool_t *pool, switch_bool_t manage);
void switch_core_sqldb_stop(void);
void switch_core_session_init(switch_memory_pool_t *pool);
......
......@@ -27,6 +27,7 @@
* Luke Dashjr <luke@openmethods.com> (OpenMethods, LLC)
* Joseph Sullivan <jossulli@amazon.com>
* Emmanuel Schmidbauer <eschmidbauer@gmail.com>
* Andrey Volk <andywolk@gmail.com>
*
* switch_core.h -- Core Library
*
......@@ -2474,13 +2475,15 @@ typedef enum {
typedef enum {
SCDB_TYPE_CORE_DB,
SCDB_TYPE_ODBC,
SCDB_TYPE_PGSQL
SCDB_TYPE_PGSQL,
SCDB_TYPE_DATABASE_INTERFACE
} switch_cache_db_handle_type_t;
typedef union {
switch_core_db_t *core_db_dbh;
switch_odbc_handle_t *odbc_dbh;
switch_pgsql_handle_t *pgsql_dbh;
switch_database_interface_handle_t *database_interface_dbh;
} switch_cache_db_native_handle_t;
typedef struct {
......@@ -2497,10 +2500,18 @@ typedef struct {
char *dsn;
} switch_cache_db_pgsql_options_t;
typedef struct {
char *dsn;
char prefix[16];
switch_database_interface_t *database_interface;
switch_bool_t make_module_no_unloadable;
} switch_cache_db_database_interface_options_t;
typedef union {
switch_cache_db_core_db_options_t core_db_options;
switch_cache_db_odbc_options_t odbc_options;
switch_cache_db_pgsql_options_t pgsql_options;
switch_cache_db_database_interface_options_t database_interface_options;
} switch_cache_db_connection_options_t;
struct switch_cache_db_handle;
......@@ -2511,6 +2522,11 @@ static inline const char *switch_cache_db_type_name(switch_cache_db_handle_type_
const char *type_str = "INVALID";
switch (type) {
case SCDB_TYPE_DATABASE_INTERFACE:
{
type_str = "DATABASE_INTERFACE";
}
break;
case SCDB_TYPE_PGSQL:
{
type_str = "PGSQL";
......@@ -2559,6 +2575,8 @@ SWITCH_DECLARE(switch_status_t) _switch_cache_db_get_db_handle(switch_cache_db_h
const char *file, const char *func, int line);
#define switch_cache_db_get_db_handle(_a, _b, _c) _switch_cache_db_get_db_handle(_a, _b, _c, __FILE__, __SWITCH_FUNC__, __LINE__)
SWITCH_DECLARE(switch_status_t) _switch_cache_db_get_db_handle_dsn_ex(switch_cache_db_handle_t **dbh, const char *dsn, switch_bool_t make_module_no_unloadable,
const char *file, const char *func, int line);
SWITCH_DECLARE(switch_status_t) _switch_cache_db_get_db_handle_dsn(switch_cache_db_handle_t **dbh, const char *dsn,
const char *file, const char *func, int line);
#define switch_cache_db_get_db_handle_dsn(_a, _b) _switch_cache_db_get_db_handle_dsn(_a, _b, __FILE__, __SWITCH_FUNC__, __LINE__)
......@@ -2643,6 +2661,13 @@ SWITCH_DECLARE(switch_status_t) switch_cache_db_persistant_execute_trans_full(sw
const char *inner_post_trans_execute);
#define switch_cache_db_persistant_execute_trans(_d, _s, _r) switch_cache_db_persistant_execute_trans_full(_d, _s, _r, NULL, NULL, NULL, NULL)
SWITCH_DECLARE(void) switch_cache_db_database_interface_flush_handles(switch_database_interface_t *database_interface);
/*!
\brief Returns error if no suitable database interface found to serve core db dsn.
*/
SWITCH_DECLARE(switch_status_t) switch_core_check_core_db_dsn();
SWITCH_DECLARE(void) switch_core_set_signal_handlers(void);
SWITCH_DECLARE(uint32_t) switch_core_debug_level(void);
SWITCH_DECLARE(int32_t) switch_core_sps(void);
......
......@@ -24,6 +24,7 @@
* Contributor(s):
*
* Anthony Minessale II <anthm@freeswitch.org>
* Andrey Volk <andywolk@gmail.com>
*
*
* switch_loadable_module.h -- Loadable Modules
......@@ -51,6 +52,14 @@ SWITCH_BEGIN_EXTERN_C
\ingroup core1
\{
*/
/*! \brief List of loadable module types */
typedef enum {
SWITCH_LOADABLE_MODULE_TYPE_PRELOAD,
SWITCH_LOADABLE_MODULE_TYPE_COMMON,
SWITCH_LOADABLE_MODULE_TYPE_POSTLOAD
} switch_loadable_module_type_t;
/*! \brief The abstraction of a loadable module */
struct switch_loadable_module_interface {
/*! the name of the module */
......@@ -87,6 +96,8 @@ SWITCH_BEGIN_EXTERN_C
switch_management_interface_t *management_interface;
/*! the table of limit interfaces the module has implemented */
switch_limit_interface_t *limit_interface;
/*! the table of database interfaces the module has implemented */
switch_database_interface_t *database_interface;
switch_thread_rwlock_t *rwlock;
int refs;
switch_memory_pool_t *pool;
......@@ -205,6 +216,13 @@ SWITCH_DECLARE(switch_json_api_interface_t *) switch_loadable_module_get_json_ap
*/
SWITCH_DECLARE(switch_file_interface_t *) switch_loadable_module_get_file_interface(const char *name, const char *modname);
/*!
\brief Retrieve the database interface by it's registered name
\param name the name of the dsn prefix
\return the desired database format interface
*/
SWITCH_DECLARE(switch_database_interface_t *) switch_loadable_module_get_database_interface(const char *name, const char *modname);
/*!
\brief Retrieve the speech interface by it's registered name
\param name the name of the speech interface
......@@ -311,6 +329,13 @@ SWITCH_DECLARE(switch_status_t) switch_loadable_module_load_module(const char *d
*/
SWITCH_DECLARE(switch_status_t) switch_loadable_module_exists(const char *mod);
/*!
\brief Protect module from beeing unloaded
\param mod the module name
\return the status
*/
SWITCH_DECLARE(switch_status_t) switch_loadable_module_protect(const char *mod);
/*!
\brief Unoad a module
\param dir the directory where the module resides
......
......@@ -25,6 +25,7 @@
*
* Anthony Minessale II <anthm@freeswitch.org>
* Luke Dashjr <luke@openmethods.com> (OpenMethods, LLC)
* Andrey Volk <andywolk@gmail.com>
*
*
* switch_module_interfaces.h -- Module Interface Definitions
......@@ -614,6 +615,38 @@ struct switch_directory_handle {
void *private_info;
};
/*! \brief Abstract interface to a database module */
struct switch_database_interface {
/*! the name of the interface */
const char *interface_name;
switch_status_t(*handle_new)(char *dsn, switch_database_interface_handle_t **dih);
switch_status_t(*handle_destroy)(switch_database_interface_handle_t **dih);
switch_status_t(*flush)(switch_database_interface_handle_t *dih);
switch_status_t(*exec_detailed)(const char *file, const char *func, int line,
switch_database_interface_handle_t *dih, const char *sql, char **err);
switch_status_t(*exec_string)(switch_database_interface_handle_t *dih, const char *sql, char *resbuf, size_t len, char **err);
switch_status_t(*sql_set_auto_commit_attr)(switch_database_interface_handle_t *dih, switch_bool_t on);
switch_status_t(*commit)(switch_database_interface_handle_t *dih);
switch_status_t(*rollback)(switch_database_interface_handle_t *dih);
switch_status_t(*callback_exec_detailed)(const char *file, const char *func, int line,
switch_database_interface_handle_t *dih, const char *sql, switch_core_db_callback_func_t callback, void *pdata, char **err);
switch_status_t(*affected_rows)(switch_database_interface_handle_t *dih, int *affected_rows);
/*! list of supported dsn prefixes */
char **prefixes;
switch_thread_rwlock_t *rwlock;
int refs;
switch_mutex_t *reflock;
switch_loadable_module_interface_t *parent;
struct switch_database_interface *next;
};
/*! an abstract representation of a database interface. */
struct switch_database_interface_handle {
switch_cache_db_database_interface_options_t connection_options;
void *handle;
};
struct switch_audio_codec_settings {
int unused;
};
......
......@@ -28,6 +28,7 @@
* Joseph Sullivan <jossulli@amazon.com>
* Raymond Chandler <intralanman@freeswitch.org>
* Emmanuel Schmidbauer <e.schmidbauer@gmail.com>
* Andrey Volk <andywolk@gmail.com>
*
* switch_types.h -- Data Types
*
......@@ -400,6 +401,7 @@ typedef enum {
SWITCH_LIMIT_INTERFACE,
SWITCH_CHAT_APPLICATION_INTERFACE,
SWITCH_JSON_API_INTERFACE,
SWITCH_DATABASE_INTERFACE,
} switch_module_interface_name_t;
typedef enum {
......@@ -2290,6 +2292,7 @@ typedef struct switch_codec_fmtp switch_codec_fmtp_t;
typedef struct switch_odbc_handle switch_odbc_handle_t;
typedef struct switch_pgsql_handle switch_pgsql_handle_t;
typedef struct switch_pgsql_result switch_pgsql_result_t;
typedef struct switch_database_interface_handle switch_database_interface_handle_t;
typedef struct switch_io_routines switch_io_routines_t;
typedef struct switch_speech_handle switch_speech_handle_t;
......@@ -2313,6 +2316,7 @@ typedef struct switch_management_interface switch_management_interface_t;
typedef struct switch_core_port_allocator switch_core_port_allocator_t;
typedef struct switch_media_bug switch_media_bug_t;
typedef struct switch_limit_interface switch_limit_interface_t;
typedef struct switch_database_interface switch_database_interface_t;
typedef void (*hashtable_destructor_t)(void *ptr);
......
......@@ -29,6 +29,7 @@
* Marcel Barbulescu <marcelbarbulescu@gmail.com>
* Joseph Sullivan <jossulli@amazon.com>
* Seven Du <dujinfang@gmail.com>
* Andrey Volk <andywolk@gmail.com>
*
* switch_core.c -- Main Core Library
*
......@@ -2015,10 +2016,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(switch_core_flag_t flags, switc
switch_core_state_machine_init(runtime.memory_pool);
if (switch_core_sqldb_start(runtime.memory_pool, switch_test_flag((&runtime), SCF_USE_SQL) ? SWITCH_TRUE : SWITCH_FALSE) != SWITCH_STATUS_SUCCESS) {
*err = "Error activating database";
return SWITCH_STATUS_FALSE;
}
switch_core_media_init();
switch_scheduler_task_thread_start();
......@@ -2343,11 +2340,7 @@ static void switch_load_core_config(const char *file)
} else if (!strcasecmp(var, "core-db-name") && !zstr(val)) {
runtime.dbname = switch_core_strdup(runtime.memory_pool, val);
} else if (!strcasecmp(var, "core-db-dsn") && !zstr(val)) {
if (switch_odbc_available() || switch_pgsql_available()) {
runtime.odbc_dsn = switch_core_strdup(runtime.memory_pool, val);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "ODBC AND PGSQL ARE NOT AVAILABLE!\n");
}
runtime.odbc_dsn = switch_core_strdup(runtime.memory_pool, val);
} else if (!strcasecmp(var, "core-non-sqlite-db-required") && !zstr(val)) {
switch_set_flag((&runtime), SCF_CORE_NON_SQLITE_DB_REQ);
} else if (!strcasecmp(var, "core-dbtype") && !zstr(val)) {
......@@ -2425,6 +2418,20 @@ SWITCH_DECLARE(const char *) switch_core_banner(void)
"\n");
}
switch_status_t switch_core_sqldb_init(const char **err)
{
if (switch_core_check_core_db_dsn() != SWITCH_STATUS_SUCCESS) {
*err = "NO SUITABLE DATABASE INTERFACE IS AVAILABLE TO SERVE 'core-db-dsn'!\n";
return SWITCH_STATUS_GENERR;
}
if (switch_core_sqldb_start(runtime.memory_pool, switch_test_flag((&runtime), SCF_USE_SQL) ? SWITCH_TRUE : SWITCH_FALSE) != SWITCH_STATUS_SUCCESS) {
*err = "Error activating database";
return SWITCH_STATUS_GENERR;
}
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(switch_status_t) switch_core_init_and_modload(switch_core_flag_t flags, switch_bool_t console, const char **err)
{
......@@ -2969,6 +2976,13 @@ SWITCH_DECLARE(switch_bool_t) switch_core_ready_outbound(void)
return (switch_test_flag((&runtime), SCF_SHUTTING_DOWN) || switch_test_flag((&runtime), SCF_NO_NEW_OUTBOUND_SESSIONS)) ? SWITCH_FALSE : SWITCH_TRUE;
}
void switch_core_sqldb_destroy()
{
if (switch_test_flag((&runtime), SCF_USE_SQL)) {
switch_core_sqldb_stop();
}
}
SWITCH_DECLARE(switch_status_t) switch_core_destroy(void)
{
switch_event_t *event;
......@@ -2989,9 +3003,6 @@ SWITCH_DECLARE(switch_status_t) switch_core_destroy(void)
switch_ssl_destroy_ssl_locks();
if (switch_test_flag((&runtime), SCF_USE_SQL)) {
switch_core_sqldb_stop();
}
switch_scheduler_task_thread_stop();
switch_rtp_shutdown();
......
差异被折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论