asterisk/res/res_pjsip/include/res_pjsip_private.h
George Joseph e83499df56 res_pjsip: Add serialized scheduler (res_pjsip/pjsip_scheduler.c)
There are several places that do scheduled tasks or periodic housecleaning,
each with its own implementation:

* res_pjsip_keepalive has a thread that sends keepalives.
* pjsip_distributor has a thread that cleans up expired unidentified requests.
* res_pjsip_registrar_expire has a thread that cleans up expired contacts.
* res_pjsip_pubsub uses ast_sched directly and then calls ast_sip_push_task.
* res_pjsip_sdp_rtp also uses ast_sched to send keepalives.

There are also places where we should be doing scheduled work but aren't.
A good example are the places we have sorcery observers to start registration
or qualify.  These don't work when changes are made to a backend database
without a pjsip reload.  We need to check periodically.

As a first step to solving these issues, a new ast_sip_sched facility has
been created.

ast_sip_sched wraps ast_sched but only uses ast_sched as a scheduled queue.
When a task is ready to run, ast_sip_task_pusk is called for it. This ensures
that the task is executed in a PJLIB registered thread and doesn't hold up the
ast_sched thread so it can immediately continue processing the queue.  The
serializer used by ast_sip_sched is one of your choosing or a random one from
the res_pjsip pool if you don't choose one.

Another feature is the ability to automatically clean up the task_data when the
task expires (if ever).  If it's an ao2 object, it will be dereferenced, if
it's a malloc'd object it will be freed.  This is selectable when the task is
scheduled.  Even if you choose to not auto dereference an ao2 task data object,
the scheduler itself maintains a reference to it while the task is under it's
control.  This prevents the data from disappearing out from under the task.

There are two scheduling models.

AST_SIP_SCHED_TASK_PERIODIC specifies that the invocations of the task occur at
the specific interval.  That is, every "interval" milliseconds, regardless of
how long the task takes.  If the task takes longer than the interval, it will
be scheduled at the next available multiple of interval.  For exmaple: If the
task has an interval of 60 secs and the task takes 70 secs (it better not),
the next invocation will happen at 120 seconds.

AST_SIP_SCHED_TASK_DELAY specifies that the next invocation of the task should
start "interval" milliseconds after the current invocation has finished.

Also, the same ast_sched facility for fixed or variable intervals exists.  The
task's return code in conjunction with the AST_SIP_SCHED_TASK_FIXED or
AST_SIP_SCHED_TASK_VARIABLE flags controls the next invocation start time.

One res_pjsip.h housekeeping change was made.  The pjsip header files were
added to the top.  There have been a few cases lately where I've needed
res_pjsip.h just for ast_sip calls and had compiles fail spectacularly because
I didn't add the pjsip header files to my source even though I never referenced
any pjsip calls.

Finally, a few new convenience APIs were added to astobj2 to make things a
little easier in the scheduler.  ao2_ref_and_lock() calls ao2_ref() and
ao2_lock() in one go.  ao2_unlock_and_unref() does the reverse. A few macros
were also copied from res_phoneprov because I got tired of having to duplicate
the same hash, sort and compare functions over and over again. The
AO2_STRING_FIELD_(HASH|SORT|CMP)_FN macros will insert functions suitable for
aor_container_alloc into your source.

This facility can be used immediately for the situations where we already have
a thread that wakes up periodically or do some scheduled work.  For the
registration and qualify issues, additional sorcery and schema changes would
need to be made so that we can easily detect changed objects on a periodic
basis without having to pull the entire database back to check.  I'm thinking
of a last-updated timestamp on the rows but more on this later.

Change-Id: I7af6ad2b2d896ea68e478aa1ae201d6dd016ba1c
2016-04-14 13:16:21 -06:00

336 lines
7.1 KiB
C

/*
* res_pjsip_private.h
*
* Created on: Jan 25, 2013
* Author: mjordan
*/
#ifndef RES_PJSIP_PRIVATE_H_
#define RES_PJSIP_PRIVATE_H_
/*!
* \todo XXX Functions prototyped in this file that begin with "ast_sip_"
* need to be renamed so res_pjsip.so does not export the names outside
* of the module.
*/
#include "asterisk/module.h"
#include "asterisk/compat.h"
struct ao2_container;
struct ast_threadpool_options;
struct ast_sip_cli_context;
/*!
* \internal
* \brief Initialize the configuration for res_pjsip
*/
int ast_res_pjsip_initialize_configuration(void);
/*!
* \internal
* \brief Annihilate the configuration objects
*/
void ast_res_pjsip_destroy_configuration(void);
/*!
* \internal
* \brief Reload the configuration
*/
int ast_res_pjsip_reload_configuration(void);
/*!
* \internal
* \brief Initialize transport support on a sorcery instance
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_initialize_sorcery_transport(void);
/*!
* \internal
* \brief Destroy transport support on a sorcery instance
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_destroy_sorcery_transport(void);
/*!
* \internal
* \brief Initialize qualify support on a sorcery instance
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_initialize_sorcery_qualify(void);
/*!
* \internal
* \brief Initialize location support on a sorcery instance
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_initialize_sorcery_location(void);
/*!
* \internal
* \brief Destroy location support on a sorcery instance
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_destroy_sorcery_location(void);
/*!
* \internal
* \brief Initialize domain aliases support on a sorcery instance
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_initialize_sorcery_domain_alias(void);
/*!
* \internal
* \brief Initialize authentication support on a sorcery instance
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_initialize_sorcery_auth(void);
/*!
* \internal
* \brief Destroy authentication support on a sorcery instance
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_destroy_sorcery_auth(void);
/*!
* \internal
* \brief Initialize the distributor module
*
* The distributor module is responsible for taking an incoming
* SIP message and placing it into the threadpool. Once in the threadpool,
* the distributor will perform endpoint lookups and authentication, and
* then distribute the message up the stack to any further modules.
*
* \retval -1 Failure
* \retval 0 Success
*/
int ast_sip_initialize_distributor(void);
/*!
* \internal
* \brief Destruct the distributor module.
*
* Unregisters pjsip modules and cleans up any allocated resources.
*/
void ast_sip_destroy_distributor(void);
/*!
* \internal
* \brief Initialize global type on a sorcery instance
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_initialize_sorcery_global(void);
/*!
* \internal
* \brief Destroy global type on a sorcery instance
* \since 13.3.0
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_destroy_sorcery_global(void);
/*!
* \internal
* \brief Initialize global headers support
*
* \return Nothing
*/
void ast_sip_initialize_global_headers(void);
/*!
* \internal
* \brief Destroy global headers support
*
* \return Nothing
*/
void ast_sip_destroy_global_headers(void);
/*!
* \internal
* \brief Initialize OPTIONS request handling.
*
* XXX This currently includes qualifying peers. It shouldn't.
* That should go into a registrar. When that occurs, we won't
* need the reload stuff.
*
* \param reload Reload options handling
*
* \retval 0 on success
* \retval other on failure
*/
int ast_res_pjsip_init_options_handling(int reload);
/*!
* \internal
* \brief Initialize transport storage for contacts.
*
* \retval 0 on success
* \retval other on failure
*/
int ast_res_pjsip_init_contact_transports(void);
/*!
* \internal
* \brief Initialize system configuration
*
* \retval 0 Success
* \retval non-zero Failure
*/
int ast_sip_initialize_system(void);
/*!
* \internal
* \brief Destroy system configuration
*/
void ast_sip_destroy_system(void);
/*!
* \internal
* \brief Initialize nameserver configuration
*/
void ast_sip_initialize_dns(void);
/*!
* \internal
* \brief Initialize our own resolver support
*/
void ast_sip_initialize_resolver(void);
/*!
* \internal
* \brief Initialize global configuration
*
* \retval 0 Success
* \retval non-zero Failure
*/
int ast_sip_initialize_global(void);
/*!
* \internal
* \brief Clean up res_pjsip options handling
*/
void ast_res_pjsip_cleanup_options_handling(void);
/*!
* \internal
* \brief Get threadpool options
*/
void sip_get_threadpool_options(struct ast_threadpool_options *threadpool_options);
/*!
* \internal
* \brief Retrieve the name of the default outbound endpoint.
*
* \note This returns a memory allocated copy of the name that
* needs to be freed by the caller.
*
* \retval The name of the default outbound endpoint.
* \retval NULL if configuration not found.
*/
char *ast_sip_global_default_outbound_endpoint(void);
/*!
* \internal
* \brief Functions for initializing and destroying the CLI.
*/
int ast_sip_initialize_cli(void);
void ast_sip_destroy_cli(void);
/*!
* \internal
* \brief Add res_pjsip global configuration options to the cli context.
*
* \param context context to add options to
* \retval 0 Success, -1 on failure
*/
int sip_cli_print_global(struct ast_sip_cli_context *context);
/*!
* \internal
* \brief Add res_pjsip system configuration options to the cli context.
*
* \param context context to add options to
* \retval 0 Success, -1 on failure
*/
int sip_cli_print_system(struct ast_sip_cli_context *context);
/*!
* \internal
* \brief Used by res_pjsip.so to register a service without adding a self reference
*/
int internal_sip_register_service(pjsip_module *module);
/*!
* \internal
* \brief Used by res_pjsip.so to unregister a service without removing a self reference
*/
int internal_sip_unregister_service(pjsip_module *module);
/*!
* \internal
* \brief Used by res_pjsip.so to register an endpoint formatter without adding a self reference
*/
void internal_sip_register_endpoint_formatter(struct ast_sip_endpoint_formatter *obj);
/*!
* \internal
* \brief Used by res_pjsip.so to unregister a endpoint formatter without removing a self reference
*/
int internal_sip_unregister_endpoint_formatter(struct ast_sip_endpoint_formatter *obj);
/*!
* \internal
* \brief Finds or creates contact_status for a contact
*/
struct ast_sip_contact_status *ast_res_pjsip_find_or_create_contact_status(const struct ast_sip_contact *contact);
/*!
* \internal
* \brief Validate that the uri meets pjproject length restrictions
*/
int ast_sip_validate_uri_length(const char *uri);
/*!
* \brief Initialize scheduler
* \since 13.9.0
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_initialize_scheduler(void);
/*!
* \internal
* \brief Destroy scheduler
* \since 13.9.0
*
* \retval -1 failure
* \retval 0 success
*/
int ast_sip_destroy_scheduler(void);
#endif /* RES_PJSIP_PRIVATE_H_ */