asterisk/res/res_eventtest.c
Russell Bryant b6b1bf3213 Merge changes from team/russell/events
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-fbb531ad65f3
2007-04-28 21:01:44 +00:00

178 lines
4.2 KiB
C

/*
* 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");