Skip to content

Commit

Permalink
bevent: add backwards wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 committed Aug 7, 2024
1 parent dd1ecaa commit d413257
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 16 deletions.
64 changes: 52 additions & 12 deletions src/bevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ static int add_rtcp_stats(struct odict *od_parent, const struct rtcp_stats *rs)
* @param call Call object (optional)
* @param prm Event parameters
*
* @deprecated Use odict_encode_event() instead
*
* @return 0 if success, otherwise errorcode
*/
int event_encode_dict(struct odict *od, struct ua *ua, enum ua_event ev,
Expand Down Expand Up @@ -570,15 +572,21 @@ int event_add_au_jb_stat(struct odict *od_parent, const struct call *call)
* @param h Event handler
* @param arg Handler argument
*
* @deprecated Use bevent_register()
*
* @return 0 if success, otherwise errorcode
*/
int uag_event_register(ua_event_h *h, void *arg)
{
struct ua_eh *eh;
static int w = 3;

if (!h)
return EINVAL;

if (w && w--)
warning("Used deprecated uag_event_register(). "
"Use bevent_register() instead!\n");
uag_event_unregister(h);

eh = mem_zalloc(sizeof(*eh), eh_destructor);
Expand All @@ -598,6 +606,8 @@ int uag_event_register(ua_event_h *h, void *arg)
* Unregister a User-Agent event handler
*
* @param h Event handler
*
* @deprecated Use bevent_unregister()
*/
void uag_event_unregister(ua_event_h *h)
{
Expand Down Expand Up @@ -666,6 +676,25 @@ void bevent_unregister(bevent_h *eh)
}


static void ua_event_private(struct ua *ua, enum ua_event ev,
struct call *call, const char *txt)
{
/* send event to all clients */
struct le *le = ehl.head;
while (le) {
struct ua_eh *eh = le->data;
le = le->next;

if (call_is_evstop(call)) {
call_set_evstop(call, false);
break;
}

eh->h(ua, ev, call, txt, eh->arg);
}
}


/**
* Send a User-Agent event to all UA event handlers
*
Expand All @@ -674,31 +703,34 @@ void bevent_unregister(bevent_h *eh)
* @param call Call object (optional)
* @param fmt Formatted arguments
* @param ... Variable arguments
*
* @deprecated Use one of the event_xxx_emit() functions
*/
void ua_event(struct ua *ua, enum ua_event ev, struct call *call,
const char *fmt, ...)
{
struct le *le;
char buf[256];
va_list ap;
static int w = 3;

va_start(ap, fmt);
(void)re_vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);

/* send event to all clients */
le = ehl.head;
while (le) {
struct ua_eh *eh = le->data;
le = le->next;
ua_event_private(ua, ev, call, buf);

if (call_is_evstop(call)) {
call_set_evstop(call, false);
break;
}
if (w && w--)
warning("Used deprecated ua_event() for %s. "
"Use one of event_xxx_emit() instead!\n",
uag_event_str(ev));
struct bevent event = {ev, buf, 0, false, { 0 } };

eh->h(ua, ev, call, buf, eh->arg);
}
if (bevent_class(ev) == BEVENT_CLASS_CALL)
event.u.call = call;
else if (bevent_class(ev) == BEVENT_CLASS_UA)
event.u.ua = ua;

(void)bevent_emit_base(&event);
}


Expand Down Expand Up @@ -747,6 +779,9 @@ void module_event(const char *module, const char *event, struct ua *ua,
eh->h(ua, UA_EVENT_MODULE, call, buf, eh->arg);
}

struct bevent bevent = {UA_EVENT_MODULE, buf, 0, false, { 0 } };
bevent_emit_base(&bevent);

out:
mem_deref(buf);
}
Expand All @@ -771,6 +806,8 @@ static void bevent_emit_base(struct bevent *event)
static int bevent_emit(struct bevent *event, const char *fmt, va_list ap)
{
char *buf;
struct call *call = bevent_get_call(event);
struct ua *ua = bevent_get_ua(event);
int err;

if (!fmt)
Expand All @@ -788,9 +825,12 @@ static int bevent_emit(struct bevent *event, const char *fmt, va_list ap)
goto out;
}

/* backwards compatibility */
if (event->stop)
goto out;

ua_event_private(ua, event->ev, call, event->txt);

out:
mem_deref(buf);
return err;
Expand Down
2 changes: 1 addition & 1 deletion src/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct call {
bool use_video;
bool use_rtp;
char *user_data; /**< User data related to the call */
bool evstop; /**< UA events stopped flag */
bool evstop; /**< UA events stopped flag, @deprecated */
};


Expand Down
48 changes: 45 additions & 3 deletions test/bevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

struct fixture {
int cnt;
int cntold;

enum ua_event expected_event;
};
Expand Down Expand Up @@ -69,17 +70,19 @@ static void event_handler(enum ua_event ev, struct bevent *event, void *arg)
struct ua *ua = bevent_get_ua(event);
struct call *call = bevent_get_call(event);
const struct sip_msg *msg = bevent_get_msg(event);
(void)ev;

if (apparg && apparg != (void *) 0xdeadbeef) {
bevent_set_error(event, EINVAL);
}
else if (ua && ua != (void *) 0xdeadbeef) {
bevent_set_error(event, EINVAL);
}
else if (call && call != (void *) 0xdeadbeef) {
else if (call) {
bevent_set_error(event, EINVAL);
}
else if (ev == UA_EVENT_CALL_INCOMING) {
++f->cnt;
}
else if (msg && msg != (void *) 0xdeadbeef) {
bevent_set_error(event, EINVAL);
}
Expand All @@ -91,6 +94,24 @@ static void event_handler(enum ua_event ev, struct bevent *event, void *arg)
}


static void ua_event_handler(struct ua *ua, enum ua_event ev,
struct call *call, const char *prm, void *arg)
{
struct fixture *f = arg;

if (ua == (void *) 0xdeadbeef && !call)
++f->cntold;
else if (ev == UA_EVENT_CALL_INCOMING)
++f->cntold;
else if (ev == UA_EVENT_SIPSESS_CONN)
++f->cntold;
else if (ev == UA_EVENT_EXIT && !str_cmp(prm, "details"))
++f->cntold;
else if (ev == UA_EVENT_SHUTDOWN && !str_cmp(prm, "details"))
++f->cntold;
}


int test_event_register(void)
{
int err = 0;
Expand All @@ -101,6 +122,9 @@ int test_event_register(void)
err = bevent_register(event_handler, &f);
TEST_ERR(err);

err = uag_event_register(ua_event_handler, &f);
TEST_ERR(err);

f.expected_event = UA_EVENT_EXIT;
err = bevent_app_emit(UA_EVENT_EXIT, (void *) 0xdeadbeef, "%s",
"details");
Expand All @@ -124,9 +148,27 @@ int test_event_register(void)
(struct sip_msg *) 0xdeadbeef, NULL);
TEST_ERR(err);


ASSERT_EQ(4, f.cnt);
ASSERT_EQ(4, f.cntold);

/* test deprecated ua_event() with old and new handlers */
f.expected_event = UA_EVENT_EXIT;
ua_event(NULL, UA_EVENT_EXIT, NULL, "%s", "details");
ua_event(NULL, UA_EVENT_SHUTDOWN, NULL, "%s", "details");
f.expected_event = UA_EVENT_REGISTER_OK;
ua_event((struct ua *) 0xdeadbeef, UA_EVENT_REGISTER_OK, NULL, "%s",
"details");
f.expected_event = UA_EVENT_CALL_INCOMING;
ua_event(NULL, UA_EVENT_CALL_INCOMING, NULL, NULL);
f.expected_event = UA_EVENT_SIPSESS_CONN;
ua_event(NULL, UA_EVENT_SIPSESS_CONN, NULL, NULL);

ASSERT_EQ(8, f.cnt);

/* event error not supported with deprecated ua_event() */
ASSERT_EQ(9, f.cntold);
out:
bevent_unregister(event_handler);
uag_event_unregister(ua_event_handler);
return err;
}

0 comments on commit d413257

Please sign in to comment.