mirror of https://github.com/asterisk/asterisk
This set of changes introduces a new generic event API for use within Asterisk. I am still working on a way for events to be shared between servers, but this part is ready and can already be used inside of Asterisk. This set of changes introduces the first use of the API, as well. I have restructured the way that MWI (message waiting indication) is handled. It is now event based instead of polling based. For example, if there are a bunch of SIP phones subscribed to mailboxes, then chan_sip will not have to constantly poll the mailboxes for changes. app_voicemail will generate events when changes occur. See UPGRADE.txt and CHANGES for some more information on the effects of these changes from the user perspective. For developer information, see the text in include/asterisk/event.h. As always, additional feedback is welcome on the asterisk-dev mailing list. git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@62292 65c4cc65-6c06-0410-ace0-fbb531ad65f31.6.0
parent
503b75f885
commit
b6b1bf3213
@ -0,0 +1,416 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2007, Digium, Inc.
|
||||
*
|
||||
* Russell Bryant <russell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \author Russell Bryant <russell@digium.com>
|
||||
* \brief Generic event system
|
||||
*
|
||||
* The purpose of this API is to provide a generic way to share events between
|
||||
* Asterisk modules. Code can generate events, and other code can subscribe to
|
||||
* them.
|
||||
*
|
||||
* Events have an associated event type, as well as information elements. The
|
||||
* information elements are the meta data that go along with each event. For
|
||||
* example, in the case of message waiting indication, the event type is MWI,
|
||||
* and each MWI event containts at least three information elements: the
|
||||
* mailbox, the number of new messages, and the number of old messages.
|
||||
*
|
||||
* Subscriptions to events consist of an event type and information elements,
|
||||
* as well. Subscriptions can be to all events, or a certain subset of events.
|
||||
* If an event type is provided, only events of that type will be sent to this
|
||||
* subscriber. Furthermore, if information elements are supplied with the
|
||||
* subscription, only events that contain the specified information elements
|
||||
* with specified values will be sent to the subscriber. For example, when a
|
||||
* SIP phone subscribes to MWI for mailbox 1234, then chan_sip can subscribe
|
||||
* to internal Asterisk MWI events with the MAILBOX information element with
|
||||
* a value of "1234".
|
||||
*
|
||||
* Another key feature of this event system is the ability to cache events.
|
||||
* It is useful for some types of events to be able to remember the last known
|
||||
* value. These are usually events that indicate some kind of state change.
|
||||
* In the example of MWI, app_voicemail can instruct the event core to cache
|
||||
* these events based on the mailbox. So, the last known MWI state of each
|
||||
* mailbox will be cached, and other modules can retrieve this information
|
||||
* on demand without having to poll the mailbox directly.
|
||||
*/
|
||||
|
||||
#ifndef AST_EVENT_H
|
||||
#define AST_EVENT_H
|
||||
|
||||
#include "asterisk/event_defs.h"
|
||||
|
||||
/*!
|
||||
* \brief Subscriber event callback type
|
||||
*
|
||||
* \param event the event being passed to the subscriber
|
||||
* \param userdata the data provider in the call to ast_event_subscribe()
|
||||
*
|
||||
* \return The event callbacks do not return anything.
|
||||
*/
|
||||
typedef void (*ast_event_cb_t)(const struct ast_event *event, void *userdata);
|
||||
|
||||
/*!
|
||||
* \brief Subscribe to events
|
||||
*
|
||||
* \param event_type The type of events to subscribe to
|
||||
* \param cb The function to be called with events
|
||||
* \param userdata data to be passed to the event callback
|
||||
*
|
||||
* The rest of the arguments to this function specify additional parameters for
|
||||
* the subscription to filter which events are passed to this subscriber. The
|
||||
* arguments must be in sets of:
|
||||
* \code
|
||||
* <enum ast_event_ie_type>, [enum ast_event_ie_pltype, [payload] ]
|
||||
* \endcode
|
||||
* and must end with AST_EVENT_IE_END.
|
||||
*
|
||||
* If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
|
||||
* by a valid IE payload type. If the payload type specified is
|
||||
* AST_EVENT_IE_PLTYPE_EXISTS, then the 3rd argument should not be provided.
|
||||
* Otherwise, a payload must also be specified.
|
||||
*
|
||||
* \return This returns a reference to the subscription for use with
|
||||
* un-subscribing later. If there is a failure in creating the
|
||||
* subscription, NULL will be returned.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* \code
|
||||
* peer->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, peer,
|
||||
* AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, peer->mailbox,
|
||||
* AST_EVENT_IE_END);
|
||||
* \endcode
|
||||
*
|
||||
* This creates a subscription to AST_EVENT_MWI events that contain an
|
||||
* information element, AST_EVENT_IE_MAILBOX, with the same string value
|
||||
* contained in peer->mailbox. Also, the event callback will be passed a
|
||||
* pointer to the peer.
|
||||
*/
|
||||
struct ast_event_sub *ast_event_subscribe(enum ast_event_type event_type,
|
||||
ast_event_cb_t cb, void *userdata, ...);
|
||||
|
||||
/*!
|
||||
* \brief Un-subscribe from events
|
||||
*
|
||||
* \param event_sub This is the reference to the subscription returned by
|
||||
* ast_event_subscribe.
|
||||
*
|
||||
* \return Nothing
|
||||
*/
|
||||
void ast_event_unsubscribe(struct ast_event_sub *event_sub);
|
||||
|
||||
/*!
|
||||
* \brief Check if subscribers exist
|
||||
*
|
||||
* \param event_type This is the type of event that the caller would like to
|
||||
* check for subscribers to.
|
||||
*
|
||||
* The rest of the arguments to this function specify additional parameters for
|
||||
* checking for subscriptions to subsets of an event type. The arguments must
|
||||
* in sets of:
|
||||
* \code
|
||||
* <enum ast_event_ie_type>, [enum ast_event_ie_pltype, [payload] ]
|
||||
* \endcode
|
||||
* and must end with AST_EVENT_IE_END.
|
||||
*
|
||||
* If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
|
||||
* by a valid IE payload type. If the payload type specified is
|
||||
* AST_EVENT_IE_PLTYPE_EXISTS, then the 3rd argument should not be provided.
|
||||
* Otherwise, a payload must also be specified.
|
||||
*
|
||||
* \return This returns one of the values defined in the ast_event_subscriber_res
|
||||
* enum which will indicate if subscribers exist that match the given
|
||||
* criteria.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* \code
|
||||
* if (ast_event_check_subscriber(AST_EVENT_MWI,
|
||||
* AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
|
||||
* AST_EVENT_IE_END) == AST_EVENT_SUB_NONE) {
|
||||
* return;
|
||||
* }
|
||||
* \endcode
|
||||
*
|
||||
* This example will check if there are any subscribers to MWI events for the
|
||||
* mailbox defined in the "mailbox" variable.
|
||||
*/
|
||||
enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type event_type, ...);
|
||||
|
||||
/*!
|
||||
* \brief Report current subscriptions to a subscription subscriber
|
||||
*
|
||||
* \arg sub the subscription subscriber
|
||||
*
|
||||
* \return nothing
|
||||
*
|
||||
* This reports all of the current subscribers to a subscriber of
|
||||
* subscribers to a specific event type. (Try saying that a few times fast).
|
||||
*
|
||||
* The idea here is that it is sometimes very useful for a module to know when
|
||||
* someone subscribes to events. However, when they first subscribe, this
|
||||
* provides that module the ability to request the event core report to them
|
||||
* all of the subscriptions to that event type that already exist.
|
||||
*/
|
||||
void ast_event_report_subs(const struct ast_event_sub *sub);
|
||||
|
||||
/*!
|
||||
* \brief Create a new event
|
||||
*
|
||||
* \param event_type The type of event to create
|
||||
*
|
||||
* The rest of the arguments to this function specify information elements to
|
||||
* add to the event. They are specified in the form:
|
||||
* \code
|
||||
* <enum ast_event_ie_type>, [enum ast_event_ie_pltype, [payload] ]
|
||||
* \endcode
|
||||
* and must end with AST_EVENT_IE_END.
|
||||
*
|
||||
* If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
|
||||
* by a valid IE payload type. The payload type, EXISTS, should not be used here
|
||||
* because it makes no sense to do so. So, a payload must also be specified
|
||||
* after the IE payload type.
|
||||
*
|
||||
* \return This returns the event that has been created. If there is an error
|
||||
* creating the event, NULL will be returned.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* \code
|
||||
* if (!(event = ast_event_new(AST_EVENT_MWI,
|
||||
* AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
|
||||
* AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, new,
|
||||
* AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, old,
|
||||
* AST_EVENT_IE_END))) {
|
||||
* return;
|
||||
* }
|
||||
* \code
|
||||
*
|
||||
* This creates a MWI event with 3 information elements, a mailbox which is
|
||||
* a string, and the number of new and old messages, specified as integers.
|
||||
*/
|
||||
struct ast_event *ast_event_new(enum ast_event_type event_type, ...);
|
||||
|
||||
/*!
|
||||
* \brief Destroy an event
|
||||
*
|
||||
* \param event the event to destroy
|
||||
*
|
||||
* \return Nothing
|
||||
*
|
||||
* \note Events that have been queued should *not* be destroyed by the code that
|
||||
* created the event. It will be automatically destroyed after being
|
||||
* dispatched to the appropriate subscribers.
|
||||
*/
|
||||
void ast_event_destroy(struct ast_event *event);
|
||||
|
||||
/*!
|
||||
* \brief Queue an event
|
||||
*
|
||||
* \param event the event to be queued
|
||||
*
|
||||
* \retval zero success
|
||||
* \retval non-zero failure
|
||||
*
|
||||
* This function queues an event to be dispatched to all of the appropriate
|
||||
* subscribers. This function will not block while the event is being
|
||||
* dispatched because a pool of event dispatching threads handle the event
|
||||
* queue.
|
||||
*/
|
||||
int ast_event_queue(struct ast_event *event);
|
||||
|
||||
/*!
|
||||
* \brief Queue and cache an event
|
||||
*
|
||||
* \param event the event to be queued and cached
|
||||
*
|
||||
* The rest of the arguments to this function specify information elements to
|
||||
* use for determining which events in the cache that this event should replace.
|
||||
* All events in the cache that match the specified criteria will be removed from
|
||||
* the cache and then this one will be added. The arguments are specified in
|
||||
* the form:
|
||||
*
|
||||
* \code
|
||||
* <enum ast_event_ie_type>, [enum ast_event_ie_pltype]
|
||||
* \endcode
|
||||
* and must end with AST_EVENT_IE_END.
|
||||
*
|
||||
* If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
|
||||
* by a valid IE payload type. If the payload type given is EXISTS, then all
|
||||
* events that contain that information element will be removed from the cache.
|
||||
* Otherwise, all events in the cache that contain an information element with
|
||||
* the same value as the new event will be removed.
|
||||
*
|
||||
* \note If more than one IE parameter is specified, they *all* must match for
|
||||
* the event to be removed from the cache.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* \code
|
||||
* ast_event_queue_and_cache(event,
|
||||
* AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
|
||||
* AST_EVENT_IE_END);
|
||||
* \endcode
|
||||
*
|
||||
* This example queues and caches an event. Any events in the cache that have
|
||||
* the same MAILBOX information element as this event will be removed.
|
||||
*
|
||||
* The purpose of caching events is so that the core can retain the last known
|
||||
* information for events that represent some sort of state. That way, when
|
||||
* code needs to find out the current state, it can query the cache.
|
||||
*/
|
||||
int ast_event_queue_and_cache(struct ast_event *event, ...);
|
||||
|
||||
/*!
|
||||
* \brief Retrieve an event from the cache
|
||||
*
|
||||
* \param event_type The type of event to retrieve from the cache
|
||||
*
|
||||
* The rest of the arguments to this function specify information elements to
|
||||
* match for retrieving events from the cache. They are specified in the form:
|
||||
* \code
|
||||
* <enum ast_event_ie_type>, [enum ast_event_ie_pltype, [payload] ]
|
||||
* \endcode
|
||||
* and must end with AST_EVENT_IE_END.
|
||||
*
|
||||
* If the ie_type specified is *not* AST_EVENT_IE_END, then it must be followed
|
||||
* by a valid IE payload type. If the payload type specified is
|
||||
* AST_EVENT_IE_PLTYPE_EXISTS, then the 3rd argument should not be provided.
|
||||
* Otherwise, a payload must also be specified.
|
||||
*
|
||||
* \return A reference to an event retrieved from the cache. If no event was
|
||||
* found that matches the specified criteria, then NULL will be returned.
|
||||
*
|
||||
* \note If more than one event in the cache matches the specified criteria, only
|
||||
* one will be returned, and it is undefined which one it will be.
|
||||
*
|
||||
* \note The caller of this function *must* call ast_event_destroy() on the
|
||||
* returned event after it is done using it.
|
||||
*
|
||||
* Example Usage:
|
||||
*
|
||||
* \code
|
||||
* event = ast_event_get_cached(AST_EVENT_MWI,
|
||||
* AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
|
||||
* AST_EVENT_IE_END);
|
||||
* \endcode
|
||||
*
|
||||
* This example will check for an MWI event in the cache that matches the
|
||||
* specified mailbox. This would be the way to find out the last known state
|
||||
* of a mailbox without having to poll the mailbox directly.
|
||||
*/
|
||||
struct ast_event *ast_event_get_cached(enum ast_event_type, ...);
|
||||
|
||||
/*!
|
||||
* \brief Append an information element that has a string payload
|
||||
*
|
||||
* \param event the event that the IE will be appended to
|
||||
* \param ie_type the type of IE to append
|
||||
* \param str The string for the payload of the IE
|
||||
*
|
||||
* \retval 0 success
|
||||
* \retval -1 failure
|
||||
*
|
||||
* The pointer to the event will get updated with the new location for the event
|
||||
* that now contains the appended information element. If the re-allocation of
|
||||
* the memory for this event fails, it will be set to NULL.
|
||||
*/
|
||||
int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
|
||||
const char *str);
|
||||
|
||||
/*!
|
||||
* \brief Append an information element that has an integer payload
|
||||
*
|
||||
* \param event the event that the IE will be appended to
|
||||
* \param ie_type the type of IE to append
|
||||
* \param data The integer for the payload of the IE
|
||||
*
|
||||
* \retval 0 success
|
||||
* \retval -1 failure
|
||||
*
|
||||
* The pointer to the event will get updated with the new location for the event
|
||||
* that now contains the appended information element. If the re-allocation of
|
||||
* the memory for this event fails, it will be set to NULL.
|
||||
*/
|
||||
int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
|
||||
uint32_t data);
|
||||
|
||||
/*!
|
||||
* \brief Append an information element that has a raw payload
|
||||
*
|
||||
* \param event the event that the IE will be appended to
|
||||
* \param ie_type the type of IE to append
|
||||
* \param data A pointer to the raw data for the payload of the IE
|
||||
* \param data_len The amount of data to copy into the payload
|
||||
*
|
||||
* \retval 0 success
|
||||
* \retval -1 failure
|
||||
*
|
||||
* The pointer to the event will get updated with the new location for the event
|
||||
* that now contains the appended information element. If the re-allocation of
|
||||
* the memory for this event fails, it will be set to NULL.
|
||||
*/
|
||||
int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
|
||||
const void *data, size_t data_len);
|
||||
|
||||
/*!
|
||||
* \brief Get the value of an information element that has an integer payload
|
||||
*
|
||||
* \param event The event to get the IE from
|
||||
* \param ie_type the type of information element to retrieve
|
||||
*
|
||||
* \return This returns the payload of the information element with the given type.
|
||||
* However, an IE with a payload of 0, and the case where no IE is found
|
||||
* yield the same return value.
|
||||
*/
|
||||
uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type);
|
||||
|
||||
/*!
|
||||
* \brief Get the value of an information element that has a string payload
|
||||
*
|
||||
* \param event The event to get the IE from
|
||||
* \param ie_type the type of information element to retrieve
|
||||
*
|
||||
* \return This returns the payload of the information element with the given type.
|
||||
* If the information element isn't found, NULL will be returned.
|
||||
*/
|
||||
const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type);
|
||||
|
||||
/*!
|
||||
* \brief Get the value of an information element that has a raw payload
|
||||
*
|
||||
* \param event The event to get the IE from
|
||||
* \param ie_type the type of information element to retrieve
|
||||
*
|
||||
* \return This returns the payload of the information element with the given type.
|
||||
* If the information element isn't found, NULL will be returned.
|
||||
*/
|
||||
const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type);
|
||||
|
||||
/*!
|
||||
* \brief Get the type for an event
|
||||
*
|
||||
* \param event the event to get the type for
|
||||
*
|
||||
* \return the event type as represented by one of the values in the
|
||||
* ast_event_type enum
|
||||
*/
|
||||
enum ast_event_type ast_event_get_type(const struct ast_event *event);
|
||||
|
||||
#endif /* AST_EVENT_H */
|
@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2007, Digium, Inc.
|
||||
*
|
||||
* Russell Bryant <russell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \author Russell Bryant <russell@digium.com>
|
||||
* \brief Generic event system
|
||||
*/
|
||||
|
||||
#ifndef AST_EVENT_DEFS_H
|
||||
#define AST_EVENT_DEFS_H
|
||||
|
||||
/*! \brief Event types
|
||||
* \note These values can *never* change. */
|
||||
enum ast_event_type {
|
||||
/*! Reserved to provide the ability to subscribe to all events. A specific
|
||||
event should never have a payload of 0. */
|
||||
AST_EVENT_ALL = 0x00,
|
||||
/*! This event type is reserved for use by third-party modules to create
|
||||
custom events without having to modify this file.
|
||||
\note There are no "custom" IE types, because IEs only have to be
|
||||
unique to the event itself, not necessarily across all events. */
|
||||
AST_EVENT_CUSTOM = 0x01,
|
||||
/*! Voicemail message waiting indication */
|
||||
AST_EVENT_MWI = 0x02,
|
||||
/*! Someone has subscribed to events */
|
||||
AST_EVENT_SUB = 0x03,
|
||||
/*! Someone has unsubscribed from events */
|
||||
AST_EVENT_UNSUB = 0x04,
|
||||
/*! Number of event types. This should be the last event type + 1 */
|
||||
AST_EVENT_TOTAL = 0x05,
|
||||
};
|
||||
|
||||
/*! \brief Event Information Element types */
|
||||
enum ast_event_ie_type {
|
||||
/*! Used to terminate the arguments to event functions */
|
||||
AST_EVENT_IE_END = -1,
|
||||
|
||||
/*!
|
||||
* \brief Number of new messages
|
||||
* Used by: AST_EVENT_MWI
|
||||
* Payload type: UINT
|
||||
*/
|
||||
AST_EVENT_IE_NEWMSGS = 0x01,
|
||||
/*!
|
||||
* \brief Number of
|
||||
* Used by: AST_EVENT_MWI
|
||||
* Payload type: UINT
|
||||
*/
|
||||
AST_EVENT_IE_OLDMSGS = 0x02,
|
||||
/*!
|
||||
* \brief Mailbox name (mailbox[@context])
|
||||
* Used by: AST_EVENT_MWI
|
||||
* Payload type: STR
|
||||
*/
|
||||
AST_EVENT_IE_MAILBOX = 0x03,
|
||||
/*!
|
||||
* \brief Unique ID
|
||||
* Used by: AST_EVENT_SUB, AST_EVENT_UNSUB
|
||||
* Payload type: UINT
|
||||
*/
|
||||
AST_EVENT_IE_UNIQUEID = 0x04,
|
||||
/*!
|
||||
* \brief Event type
|
||||
* Used by: AST_EVENT_SUB, AST_EVENT_UNSUB
|
||||
* Payload type: UINT
|
||||
*/
|
||||
AST_EVENT_IE_EVENTTYPE = 0x05,
|
||||
/*!
|
||||
* \brief Hint that someone cares than an IE exists
|
||||
* Used by: AST_EVENT_SUB
|
||||
* Payload type: UINT (ast_event_ie_type)
|
||||
*/
|
||||
AST_EVENT_IE_EXISTS = 0x06,
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief Payload types for event information elements
|
||||
*/
|
||||
enum ast_event_ie_pltype {
|
||||
/*! Just check if it exists, not the value */
|
||||
AST_EVENT_IE_PLTYPE_EXISTS,
|
||||
/*! Unsigned Integer (Can be used for signed, too ...) */
|
||||
AST_EVENT_IE_PLTYPE_UINT,
|
||||
/*! String */
|
||||
AST_EVENT_IE_PLTYPE_STR,
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief Results for checking for subscribers
|
||||
*
|
||||
* \ref ast_event_check_subscriber()
|
||||
*/
|
||||
enum ast_event_subscriber_res {
|
||||
/*! No subscribers exist */
|
||||
AST_EVENT_SUB_NONE,
|
||||
/*! At least one subscriber exists */
|
||||
AST_EVENT_SUB_EXISTS,
|
||||
};
|
||||
|
||||
struct ast_event;
|
||||
struct ast_event_ie;
|
||||
struct ast_event_sub;
|
||||
|
||||
#endif /* AST_EVENT_DEFS_H */
|
@ -0,0 +1,774 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2007, Digium, Inc.
|
||||
*
|
||||
* Russell Bryant <russell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*! \file
|
||||
*
|
||||
* \brief Internal generic event system
|
||||
*
|
||||
* \author Russell Bryant <russell@digium.com>
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "asterisk/event.h"
|
||||
#include "asterisk/linkedlists.h"
|
||||
#include "asterisk/lock.h"
|
||||
#include "asterisk/utils.h"
|
||||
|
||||
#define NUM_EVENT_THREADS 5
|
||||
|
||||
struct ast_event_ie {
|
||||
enum ast_event_ie_type ie_type:16;
|
||||
/*! Total length of the IE payload */
|
||||
uint16_t ie_payload_len;
|
||||
unsigned char ie_payload[0];
|
||||
} __attribute__ ((packed));
|
||||
|
||||
/*!
|
||||
* \brief An event
|
||||
*
|
||||
* \note The format of this structure is important, and can not change, since
|
||||
* they are sent directly over the network (via IAX2).
|
||||
*
|
||||
*/
|
||||
struct ast_event {
|
||||
/*! Event type */
|
||||
enum ast_event_type type:16;
|
||||
/*! Total length of the event */
|
||||
uint16_t event_len:16;
|
||||
/*! The data payload of the event, made up of information elements */
|
||||
unsigned char payload[0];
|
||||
} __attribute__ ((packed));
|
||||
|
||||
struct ast_event_ref {
|
||||
struct ast_event *event;
|
||||
AST_LIST_ENTRY(ast_event_ref) entry;
|
||||
};
|
||||
|
||||
/*! \brief data shared between event dispatching threads */
|
||||
static struct {
|
||||
ast_cond_t cond;
|
||||
ast_mutex_t lock;
|
||||
AST_LIST_HEAD_NOLOCK(, ast_event_ref) event_q;
|
||||
} event_thread = {
|
||||
.lock = AST_MUTEX_INIT_VALUE,
|
||||
};
|
||||
|
||||
struct ast_event_ie_val {
|
||||
AST_LIST_ENTRY(ast_event_ie_val) entry;
|
||||
enum ast_event_ie_type ie_type;
|
||||
enum ast_event_ie_pltype ie_pltype;
|
||||
union {
|
||||
uint32_t uint;
|
||||
const char *str;
|
||||
} payload;
|
||||
};
|
||||
|
||||
/*! \brief Event subscription */
|
||||
struct ast_event_sub {
|
||||
enum ast_event_type type;
|
||||
ast_event_cb_t cb;
|
||||
void *userdata;
|
||||
uint32_t uniqueid;
|
||||
AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
|
||||
AST_RWLIST_ENTRY(ast_event_sub) entry;
|
||||
};
|
||||
|
||||
static uint32_t sub_uniqueid;
|
||||
|
||||
/*! \brief Event subscriptions
|
||||
* The event subscribers are indexed by which event they are subscribed to */
|
||||
static AST_RWLIST_HEAD(ast_event_sub_list, ast_event_sub) ast_event_subs[AST_EVENT_TOTAL];
|
||||
|
||||
/*! \brief Cached events
|
||||
* The event cache is indexed on the event type. The purpose of this is
|
||||
* for events that express some sort of state. So, when someone first
|
||||
* needs to know this state, it can get the last known state from the cache. */
|
||||
static AST_RWLIST_HEAD(ast_event_ref_list, ast_event_ref) ast_event_cache[AST_EVENT_TOTAL];
|
||||
|
||||
static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
|
||||
{
|
||||
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
|
||||
free((void *) ie_val->payload.str);
|
||||
|
||||
free(ie_val);
|
||||
}
|
||||
|
||||
enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type type, ...)
|
||||
{
|
||||
va_list ap;
|
||||
enum ast_event_ie_type ie_type;
|
||||
enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
|
||||
struct ast_event_ie_val *ie_val, *sub_ie_val;
|
||||
struct ast_event_sub *sub;
|
||||
AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
|
||||
|
||||
if (type >= AST_EVENT_TOTAL) {
|
||||
ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
|
||||
return res;
|
||||
}
|
||||
|
||||
va_start(ap, type);
|
||||
for (ie_type = va_arg(ap, enum ast_event_type);
|
||||
ie_type != AST_EVENT_IE_END;
|
||||
ie_type = va_arg(ap, enum ast_event_type))
|
||||
{
|
||||
struct ast_event_ie_val *ie_val = alloca(sizeof(*ie_val));
|
||||
memset(ie_val, 0, sizeof(*ie_val));
|
||||
ie_val->ie_type = ie_type;
|
||||
ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
|
||||
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
|
||||
ie_val->payload.uint = va_arg(ap, uint32_t);
|
||||
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
|
||||
ie_val->payload.str = ast_strdupa(va_arg(ap, const char *));
|
||||
AST_LIST_INSERT_TAIL(&ie_vals, ie_val, entry);
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
AST_RWLIST_RDLOCK(&ast_event_subs[type]);
|
||||
AST_RWLIST_TRAVERSE(&ast_event_subs[type], sub, entry) {
|
||||
AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
|
||||
AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) {
|
||||
if (sub_ie_val->ie_type == ie_val->ie_type)
|
||||
break;
|
||||
}
|
||||
if (!sub_ie_val) {
|
||||
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
/* The subscriber doesn't actually care what the value is */
|
||||
if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
|
||||
continue;
|
||||
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
|
||||
ie_val->payload.uint != sub_ie_val->payload.uint)
|
||||
break;
|
||||
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
|
||||
strcmp(ie_val->payload.str, sub_ie_val->payload.str))
|
||||
break;
|
||||
}
|
||||
if (!ie_val)
|
||||
break;
|
||||
}
|
||||
AST_RWLIST_UNLOCK(&ast_event_subs[type]);
|
||||
|
||||
if (sub) /* All parameters were matched */
|
||||
return AST_EVENT_SUB_EXISTS;
|
||||
|
||||
AST_RWLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
|
||||
if (!AST_LIST_EMPTY(&ast_event_subs[AST_EVENT_ALL]))
|
||||
res = AST_EVENT_SUB_EXISTS;
|
||||
AST_RWLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/*! Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
|
||||
void ast_event_report_subs(const struct ast_event_sub *event_sub)
|
||||
{
|
||||
struct ast_event *event;
|
||||
struct ast_event_sub *sub;
|
||||
enum ast_event_type event_type = -1;
|
||||
struct ast_event_ie_val *ie_val;
|
||||
|
||||
if (event_sub->type != AST_EVENT_SUB)
|
||||
return;
|
||||
|
||||
AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
|
||||
if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
|
||||
event_type = ie_val->payload.uint;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (event_type == -1)
|
||||
return;
|
||||
|
||||
AST_RWLIST_RDLOCK(&ast_event_subs[event_type]);
|
||||
AST_RWLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
|
||||
if (event_sub == sub)
|
||||
continue;
|
||||
|
||||
event = ast_event_new(AST_EVENT_SUB,
|
||||
AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
|
||||
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
|
||||
AST_EVENT_IE_END);
|
||||
|
||||
AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
|
||||
switch (ie_val->ie_pltype) {
|
||||
case AST_EVENT_IE_PLTYPE_EXISTS:
|
||||
ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
|
||||
break;
|
||||
case AST_EVENT_IE_PLTYPE_UINT:
|
||||
ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
|
||||
break;
|
||||
case AST_EVENT_IE_PLTYPE_STR:
|
||||
ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
|
||||
break;
|
||||
}
|
||||
if (!event)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!event)
|
||||
continue;
|
||||
|
||||
event_sub->cb(event, event_sub->userdata);
|
||||
|
||||
ast_event_destroy(event);
|
||||
}
|
||||
AST_RWLIST_UNLOCK(&ast_event_subs[event_type]);
|
||||
}
|
||||
|
||||
struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb,
|
||||
void *userdata, ...)
|
||||
{
|
||||
va_list ap;
|
||||
enum ast_event_ie_type ie_type;
|
||||
struct ast_event_sub *sub;
|
||||
struct ast_event *event;
|
||||
|
||||
if (type >= AST_EVENT_TOTAL) {
|
||||
ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!(sub = ast_calloc(1, sizeof(*sub))))
|
||||
return NULL;
|
||||
|
||||
va_start(ap, userdata);
|
||||
for (ie_type = va_arg(ap, enum ast_event_type);
|
||||
ie_type != AST_EVENT_IE_END;
|
||||
ie_type = va_arg(ap, enum ast_event_type))
|
||||
{
|
||||
struct ast_event_ie_val *ie_val;
|
||||
if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
|
||||
continue;
|
||||
ie_val->ie_type = ie_type;
|
||||
ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
|
||||
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
|
||||
ie_val->payload.uint = va_arg(ap, uint32_t);
|
||||
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR) {
|
||||
if (!(ie_val->payload.str = ast_strdup(va_arg(ap, const char *)))) {
|
||||
free(ie_val);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
sub->type = type;
|
||||
sub->cb = cb;
|
||||
sub->userdata = userdata;
|
||||
sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
|
||||
|
||||
if (ast_event_check_subscriber(AST_EVENT_SUB,
|
||||
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, type,
|
||||
AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
|
||||
struct ast_event_ie_val *ie_val;
|
||||
|
||||
event = ast_event_new(AST_EVENT_SUB,
|
||||
AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
|
||||
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
|
||||
AST_EVENT_IE_END);
|
||||
|
||||
AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
|
||||
switch (ie_val->ie_pltype) {
|
||||
case AST_EVENT_IE_PLTYPE_EXISTS:
|
||||
ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
|
||||
break;
|
||||
case AST_EVENT_IE_PLTYPE_UINT:
|
||||
ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
|
||||
break;
|
||||
case AST_EVENT_IE_PLTYPE_STR:
|
||||
ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
|
||||
break;
|
||||
}
|
||||
if (!event)
|
||||
break;
|
||||
}
|
||||
|
||||
if (event)
|
||||
ast_event_queue(event);
|
||||
}
|
||||
|
||||
AST_RWLIST_WRLOCK(&ast_event_subs[type]);
|
||||
AST_RWLIST_INSERT_TAIL(&ast_event_subs[type], sub, entry);
|
||||
AST_RWLIST_UNLOCK(&ast_event_subs[type]);
|
||||
|
||||
return sub;
|
||||
}
|
||||
|
||||
static void ast_event_sub_destroy(struct ast_event_sub *sub)
|
||||
{
|
||||
struct ast_event_ie_val *ie_val;
|
||||
|
||||
while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry)))
|
||||
ast_event_ie_val_destroy(ie_val);
|
||||
|
||||
free(sub);
|
||||
}
|
||||
|
||||
void ast_event_unsubscribe(struct ast_event_sub *sub)
|
||||
{
|
||||
struct ast_event *event;
|
||||
|
||||
AST_RWLIST_WRLOCK(&ast_event_subs[sub->type]);
|
||||
AST_LIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
|
||||
AST_RWLIST_UNLOCK(&ast_event_subs[sub->type]);
|
||||
|
||||
if (ast_event_check_subscriber(AST_EVENT_UNSUB,
|
||||
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
|
||||
AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
|
||||
|
||||
event = ast_event_new(AST_EVENT_UNSUB,
|
||||
AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
|
||||
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
|
||||
AST_EVENT_IE_END);
|
||||
|
||||
if (event)
|
||||
ast_event_queue(event);
|
||||
}
|
||||
|
||||
ast_event_sub_destroy(sub);
|
||||
}
|
||||
|
||||
enum ast_event_type ast_event_get_type(const struct ast_event *event)
|
||||
{
|
||||
return ntohs(event->type);
|
||||
}
|
||||
|
||||
uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
|
||||
{
|
||||
const uint32_t *ie_val;
|
||||
|
||||
ie_val = ast_event_get_ie_raw(event, ie_type);
|
||||
|
||||
return ie_val ? ntohl(*ie_val) : 0;
|
||||
}
|
||||
|
||||
const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
|
||||
{
|
||||
return ast_event_get_ie_raw(event, ie_type);
|
||||
}
|
||||
|
||||
const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
|
||||
{
|
||||
struct ast_event_ie *ie;
|
||||
uint16_t event_len;
|
||||
|
||||
ie_type = ntohs(ie_type);
|
||||
event_len = ntohs(event->event_len);
|
||||
|
||||
ie = ((void *) event) + sizeof(*event);
|
||||
|
||||
while ((((void *) ie) - ((void *) event)) < event_len) {
|
||||
if (ie->ie_type == ie_type)
|
||||
return ie->ie_payload;
|
||||
ie = ((void *) ie) + sizeof(*ie) + ntohs(ie->ie_payload_len);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
|
||||
const char *str)
|
||||
{
|
||||
return ast_event_append_ie_raw(event, ie_type, str, strlen(str) + 1);
|
||||
}
|
||||
|
||||
int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
|
||||
uint32_t data)
|
||||
{
|
||||
data = htonl(data);
|
||||
return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
|
||||
}
|
||||
|
||||
int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
|
||||
const void *data, size_t data_len)
|
||||
{
|
||||
struct ast_event_ie *ie;
|
||||
unsigned int extra_len;
|
||||
uint16_t event_len;
|
||||
|
||||
event_len = ntohs((*event)->event_len);
|
||||
extra_len = sizeof(*ie) + data_len;
|
||||
|
||||
if (!(*event = ast_realloc(*event, event_len + extra_len)))
|
||||
return -1;
|
||||
|
||||
ie = ((void *) *event) + event_len;
|
||||
ie->ie_type = htons(ie_type);
|
||||
ie->ie_payload_len = htons(data_len);
|
||||
memcpy(ie->ie_payload, data, data_len);
|
||||
|
||||
(*event)->event_len = htons(event_len + extra_len);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ast_event *ast_event_new(enum ast_event_type type, ...)
|
||||
{
|
||||
va_list ap;
|
||||
struct ast_event *event;
|
||||
enum ast_event_type ie_type;
|
||||
struct ast_event_ie_val *ie_val;
|
||||
AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
|
||||
|
||||
/* Invalid type */
|
||||
if (type >= AST_EVENT_TOTAL) {
|
||||
ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
|
||||
"type '%d'!\n", type);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
va_start(ap, type);
|
||||
for (ie_type = va_arg(ap, enum ast_event_type);
|
||||
ie_type != AST_EVENT_IE_END;
|
||||
ie_type = va_arg(ap, enum ast_event_type))
|
||||
{
|
||||
struct ast_event_ie_val *ie_val = alloca(sizeof(*ie_val));
|
||||
memset(ie_val, 0, sizeof(*ie_val));
|
||||
ie_val->ie_type = ie_type;
|
||||
ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
|
||||
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
|
||||
ie_val->payload.uint = va_arg(ap, uint32_t);
|
||||
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
|
||||
ie_val->payload.str = ast_strdupa(va_arg(ap, const char *));
|
||||
AST_LIST_INSERT_TAIL(&ie_vals, ie_val, entry);
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
if (!(event = ast_calloc(1, sizeof(*event))))
|
||||
return NULL;
|
||||
|
||||
event->type = htons(type);
|
||||
event->event_len = htons(sizeof(*event));
|
||||
|
||||
AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
|
||||
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
|
||||
ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
|
||||
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
|
||||
ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
|
||||
|
||||
if (!event)
|
||||
break;
|
||||
}
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
void ast_event_destroy(struct ast_event *event)
|
||||
{
|
||||
free(event);
|
||||
}
|
||||
|
||||
static void ast_event_ref_destroy(struct ast_event_ref *event_ref)
|
||||
{
|
||||
ast_event_destroy(event_ref->event);
|
||||
free(event_ref);
|
||||
}
|
||||
|
||||
static struct ast_event *ast_event_dup(const struct ast_event *event)
|
||||
{
|
||||
struct ast_event *dup_event;
|
||||
uint16_t event_len;
|
||||
|
||||
event_len = ntohs(event->event_len);
|
||||
|
||||
if (!(dup_event = ast_calloc(1, event_len)))
|
||||
return NULL;
|
||||
|
||||
memcpy(dup_event, event, event_len);
|
||||
|
||||
return dup_event;
|
||||
}
|
||||
|
||||
struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
|
||||
{
|
||||
va_list ap;
|
||||
enum ast_event_ie_type ie_type;
|
||||
struct ast_event *dup_event = NULL;
|
||||
struct ast_event_ref *event_ref;
|
||||
struct cache_arg {
|
||||
AST_LIST_ENTRY(cache_arg) entry;
|
||||
enum ast_event_ie_type ie_type;
|
||||
enum ast_event_ie_pltype ie_pltype;
|
||||
union {
|
||||
uint32_t uint;
|
||||
const char *str;
|
||||
} payload;
|
||||
} *cache_arg;
|
||||
AST_LIST_HEAD_NOLOCK_STATIC(cache_args, cache_arg);
|
||||
|
||||
if (type >= AST_EVENT_TOTAL) {
|
||||
ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
va_start(ap, type);
|
||||
for (ie_type = va_arg(ap, enum ast_event_type);
|
||||
ie_type != AST_EVENT_IE_END;
|
||||
ie_type = va_arg(ap, enum ast_event_type))
|
||||
{
|
||||
cache_arg = alloca(sizeof(*cache_arg));
|
||||
memset(cache_arg, 0, sizeof(*cache_arg));
|
||||
cache_arg->ie_type = ie_type;
|
||||
cache_arg->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
|
||||
if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
|
||||
cache_arg->payload.uint = va_arg(ap, uint32_t);
|
||||
else if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
|
||||
cache_arg->payload.str = ast_strdupa(va_arg(ap, const char *));
|
||||
AST_LIST_INSERT_TAIL(&cache_args, cache_arg, entry);
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
if (AST_LIST_EMPTY(&cache_args)) {
|
||||
ast_log(LOG_ERROR, "Events can not be retrieved from the cache without "
|
||||
"specifying at least one IE type!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
AST_RWLIST_RDLOCK(&ast_event_cache[type]);
|
||||
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[type], event_ref, entry) {
|
||||
AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
|
||||
if ( ! ( (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
|
||||
(cache_arg->payload.uint ==
|
||||
ast_event_get_ie_uint(event_ref->event, cache_arg->ie_type))) ||
|
||||
|
||||
(cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
|
||||
(!strcmp(cache_arg->payload.str,
|
||||
ast_event_get_ie_str(event_ref->event, cache_arg->ie_type)))) ||
|
||||
|
||||
(cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
|
||||
ast_event_get_ie_raw(event_ref->event, cache_arg->ie_type)) ) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!cache_arg) {
|
||||
/* All parameters were matched on this cache entry, so return it */
|
||||
dup_event = ast_event_dup(event_ref->event);
|
||||
break;
|
||||
}
|
||||
}
|
||||
AST_RWLIST_TRAVERSE_SAFE_END
|
||||
AST_RWLIST_UNLOCK(&ast_event_cache[type]);
|
||||
|
||||
return dup_event;
|
||||
}
|
||||
|
||||
/*! \brief Duplicate an event and add it to the cache
|
||||
* \note This assumes this index in to the cache is locked */
|
||||
static int ast_event_dup_and_cache(const struct ast_event *event)
|
||||
{
|
||||
struct ast_event *dup_event;
|
||||
struct ast_event_ref *event_ref;
|
||||
|
||||
if (!(dup_event = ast_event_dup(event)))
|
||||
return -1;
|
||||
if (!(event_ref = ast_calloc(1, sizeof(*event_ref))))
|
||||
return -1;
|
||||
|
||||
event_ref->event = dup_event;
|
||||
|
||||
AST_LIST_INSERT_TAIL(&ast_event_cache[ntohs(event->type)], event_ref, entry);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ast_event_queue_and_cache(struct ast_event *event, ...)
|
||||
{
|
||||
va_list ap;
|
||||
enum ast_event_type ie_type;
|
||||
uint16_t host_event_type;
|
||||
struct ast_event_ref *event_ref;
|
||||
int res;
|
||||
struct cache_arg {
|
||||
AST_LIST_ENTRY(cache_arg) entry;
|
||||
enum ast_event_ie_type ie_type;
|
||||
enum ast_event_ie_pltype ie_pltype;
|
||||
} *cache_arg;
|
||||
AST_LIST_HEAD_NOLOCK_STATIC(cache_args, cache_arg);
|
||||
|
||||
host_event_type = ntohs(event->type);
|
||||
|
||||
/* Invalid type */
|
||||
if (host_event_type >= AST_EVENT_TOTAL) {
|
||||
ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
|
||||
"type '%d'!\n", host_event_type);
|
||||
return -1;
|
||||
}
|
||||
|
||||
va_start(ap, event);
|
||||
for (ie_type = va_arg(ap, enum ast_event_type);
|
||||
ie_type != AST_EVENT_IE_END;
|
||||
ie_type = va_arg(ap, enum ast_event_type))
|
||||
{
|
||||
cache_arg = alloca(sizeof(*cache_arg));
|
||||
memset(cache_arg, 0, sizeof(*cache_arg));
|
||||
cache_arg->ie_type = ie_type;
|
||||
cache_arg->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
|
||||
AST_LIST_INSERT_TAIL(&cache_args, cache_arg, entry);
|
||||
}
|
||||
va_end(ap);
|
||||
|
||||
if (AST_LIST_EMPTY(&cache_args)) {
|
||||
ast_log(LOG_ERROR, "Events can not be cached without specifying at "
|
||||
"least one IE type!\n");
|
||||
return ast_event_queue(event);
|
||||
}
|
||||
|
||||
AST_RWLIST_WRLOCK(&ast_event_cache[host_event_type]);
|
||||
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[host_event_type], event_ref, entry) {
|
||||
AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
|
||||
if ( ! ( (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
|
||||
(ast_event_get_ie_uint(event, cache_arg->ie_type) ==
|
||||
ast_event_get_ie_uint(event_ref->event, cache_arg->ie_type))) ||
|
||||
|
||||
(cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
|
||||
(!strcmp(ast_event_get_ie_str(event, cache_arg->ie_type),
|
||||
ast_event_get_ie_str(event_ref->event, cache_arg->ie_type)))) ||
|
||||
|
||||
(cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
|
||||
ast_event_get_ie_raw(event_ref->event, cache_arg->ie_type)) ) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!cache_arg) {
|
||||
/* All parameters were matched on this cache entry, so remove it */
|
||||
AST_LIST_REMOVE_CURRENT(&ast_event_cache[host_event_type], entry);
|
||||
ast_event_ref_destroy(event_ref);
|
||||
}
|
||||
}
|
||||
AST_RWLIST_TRAVERSE_SAFE_END
|
||||
res = ast_event_dup_and_cache(event);
|
||||
AST_RWLIST_UNLOCK(&ast_event_cache[host_event_type]);
|
||||
|
||||
return (ast_event_queue(event) || res) ? -1 : 0;
|
||||
}
|
||||
|
||||
int ast_event_queue(struct ast_event *event)
|
||||
{
|
||||
struct ast_event_ref *event_ref;
|
||||
uint16_t host_event_type;
|
||||
|
||||
host_event_type = ntohs(event->type);
|
||||
|
||||
/* Invalid type */
|
||||
if (host_event_type >= AST_EVENT_TOTAL) {
|
||||
ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
|
||||
"type '%d'!\n", host_event_type);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If nobody has subscribed to this event type, throw it away now */
|
||||
if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END)
|
||||
== AST_EVENT_SUB_NONE) {
|
||||
ast_event_destroy(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(event_ref = ast_calloc(1, sizeof(*event_ref))))
|
||||
return -1;
|
||||
|
||||
event_ref->event = event;
|
||||
|
||||
ast_mutex_lock(&event_thread.lock);
|
||||
AST_LIST_INSERT_TAIL(&event_thread.event_q, event_ref, entry);
|
||||
ast_cond_signal(&event_thread.cond);
|
||||
ast_mutex_unlock(&event_thread.lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void *ast_event_dispatcher(void *unused)
|
||||
{
|
||||
for (;;) {
|
||||
struct ast_event_ref *event_ref;
|
||||
struct ast_event_sub *sub;
|
||||
uint16_t host_event_type;
|
||||
|
||||
ast_mutex_lock(&event_thread.lock);
|
||||
while (!(event_ref = AST_LIST_REMOVE_HEAD(&event_thread.event_q, entry)))
|
||||
ast_cond_wait(&event_thread.cond, &event_thread.lock);
|
||||
ast_mutex_unlock(&event_thread.lock);
|
||||
|
||||
host_event_type = ntohs(event_ref->event->type);
|
||||
|
||||
/* Subscribers to this specific event first */
|
||||
AST_RWLIST_RDLOCK(&ast_event_subs[host_event_type]);
|
||||
AST_RWLIST_TRAVERSE(&ast_event_subs[host_event_type], sub, entry) {
|
||||
struct ast_event_ie_val *ie_val;
|
||||
AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
|
||||
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
|
||||
ast_event_get_ie_raw(event_ref->event, ie_val->ie_type)) {
|
||||
continue;
|
||||
} else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
|
||||
ast_event_get_ie_uint(event_ref->event, ie_val->ie_type)
|
||||
== ie_val->payload.uint) {
|
||||
continue;
|
||||
} else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
|
||||
!strcmp(ast_event_get_ie_str(event_ref->event, ie_val->ie_type),
|
||||
ie_val->payload.str)) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (ie_val)
|
||||
continue;
|
||||
sub->cb(event_ref->event, sub->userdata);
|
||||
}
|
||||
AST_RWLIST_UNLOCK(&ast_event_subs[host_event_type]);
|
||||
|
||||
/* Now to subscribers to all event types */
|
||||
AST_RWLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
|
||||
AST_RWLIST_TRAVERSE(&ast_event_subs[AST_EVENT_ALL], sub, entry)
|
||||
sub->cb(event_ref->event, sub->userdata);
|
||||
AST_RWLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
|
||||
|
||||
ast_event_ref_destroy(event_ref);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void ast_event_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < AST_EVENT_TOTAL; i++)
|
||||
AST_RWLIST_HEAD_INIT(&ast_event_subs[i]);
|
||||
|
||||
for (i = 0; i < AST_EVENT_TOTAL; i++)
|
||||
AST_RWLIST_HEAD_INIT(&ast_event_cache[i]);
|
||||
|
||||
ast_cond_init(&event_thread.cond, NULL);
|
||||
|
||||
for (i = 0; i < NUM_EVENT_THREADS; i++) {
|
||||
pthread_t dont_care;
|
||||
ast_pthread_create_background(&dont_care, NULL, ast_event_dispatcher, NULL);
|
||||
}
|
||||
}
|
@ -0,0 +1,177 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2007, Digium, Inc.
|
||||
*
|
||||
* Russell Bryant <russell@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
* \author Russell Bryant <russell@digium.com>
|
||||
*
|
||||
* \brief Test code for the internal event system
|
||||
*
|
||||
*/
|
||||
|
||||
/*** MODULEINFO
|
||||
<defaultenabled>no</defaultenabled>
|
||||
***/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$");
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/event.h"
|
||||
#include "asterisk/cli.h"
|
||||
|
||||
static void process_event_generic(const struct ast_event *event)
|
||||
{
|
||||
ast_log(LOG_DEBUG, "Event type: %u\n", ast_event_get_type(event));
|
||||
}
|
||||
|
||||
static void process_event_mwi(const struct ast_event *event)
|
||||
{
|
||||
const char *mailbox;
|
||||
unsigned int new;
|
||||
unsigned int old;
|
||||
|
||||
mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
|
||||
new = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
|
||||
old = ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS);
|
||||
|
||||
ast_log(LOG_DEBUG, "MWI Event. Mailbox: %s New: %u Old: %u\n",
|
||||
mailbox, new, old);
|
||||
}
|
||||
|
||||
static void ast_event_process(const struct ast_event *event, void *userdata)
|
||||
{
|
||||
switch (ast_event_get_type(event)) {
|
||||
case AST_EVENT_MWI:
|
||||
process_event_mwi(event);
|
||||
break;
|
||||
default:
|
||||
process_event_generic(event);
|
||||
}
|
||||
}
|
||||
|
||||
static char *event_gen(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
||||
{
|
||||
struct ast_event *event;
|
||||
const char *mailbox = "1234@fakecontext";
|
||||
unsigned int new = 5;
|
||||
unsigned int old = 12;
|
||||
struct ast_event_sub *event_sub;
|
||||
|
||||
switch (cmd) {
|
||||
case CLI_INIT:
|
||||
e->command = "event generate";
|
||||
e->usage =
|
||||
"Usage: event generate\n"
|
||||
" Generate a test event.\n";
|
||||
return NULL;
|
||||
|
||||
case CLI_GENERATE:
|
||||
return NULL; /* no completion */
|
||||
}
|
||||
if (a->argc != e->args)
|
||||
return CLI_SHOWUSAGE;
|
||||
|
||||
if (!(event_sub = ast_event_subscribe(AST_EVENT_ALL, ast_event_process,
|
||||
NULL, AST_EVENT_IE_END))) {
|
||||
return CLI_FAILURE;
|
||||
}
|
||||
|
||||
if (!(event = ast_event_new(AST_EVENT_MWI,
|
||||
AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
|
||||
AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, new,
|
||||
AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, old,
|
||||
AST_EVENT_IE_END))) {
|
||||
return CLI_FAILURE;
|
||||
}
|
||||
|
||||
ast_event_queue_and_cache(event,
|
||||
AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
|
||||
AST_EVENT_IE_END);
|
||||
|
||||
/* XXX This is a hack. I should use a timed thread condition instead. */
|
||||
usleep(1000000);
|
||||
|
||||
ast_event_unsubscribe(event_sub);
|
||||
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
||||
static char *event_get_cached(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
||||
{
|
||||
struct ast_event *event;
|
||||
const char *mailbox = "1234@fakecontext";
|
||||
|
||||
switch (cmd) {
|
||||
case CLI_INIT:
|
||||
e->command = "event get cached";
|
||||
e->usage =
|
||||
"Usage: event get cached\n"
|
||||
" Test getting an event from the cache.\n";
|
||||
return NULL;
|
||||
|
||||
case CLI_GENERATE:
|
||||
return NULL; /* no completion */
|
||||
}
|
||||
if (a->argc != e->args)
|
||||
return CLI_SHOWUSAGE;
|
||||
|
||||
event = ast_event_get_cached(AST_EVENT_MWI,
|
||||
AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
|
||||
AST_EVENT_IE_END);
|
||||
|
||||
if (!event) {
|
||||
ast_cli(a->fd, "No event retrieved!\n");
|
||||
return CLI_FAILURE;
|
||||
}
|
||||
|
||||
ast_cli(a->fd, "Got the event. New: %u Old: %u\n",
|
||||
ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS),
|
||||
ast_event_get_ie_uint(event, AST_EVENT_IE_OLDMSGS));
|
||||
|
||||
ast_event_destroy(event);
|
||||
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
||||
static struct ast_cli_entry cli_commands[] = {
|
||||
NEW_CLI(event_gen, "Generate a test event"),
|
||||
NEW_CLI(event_get_cached, "Get an event from the cache"),
|
||||
};
|
||||
|
||||
static int load_module(void)
|
||||
{
|
||||
ast_cli_register_multiple(cli_commands, ARRAY_LEN(cli_commands));
|
||||
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
static int unload_module(void)
|
||||
{
|
||||
ast_cli_unregister_multiple(cli_commands, ARRAY_LEN(cli_commands));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Test code for the internal event system");
|
Loading…
Reference in new issue