Module: sip-router Branch: tmp/core_events Commit: 85acffe10f182c5d0cfb382f33ff5a2d1203524a URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=85acffe1...
Author: Daniel-Constantin Mierla miconda@gmail.com Committer: Daniel-Constantin Mierla miconda@gmail.com Date: Tue Aug 4 16:47:10 2009 +0200
core: new sr events system
- designed to have small footprint in core - the goal is to execute a event manager function during core processing - event managers can be implemented in modules or elsewhere - two event types added: SREV_NET_DATA_IN and SREV_NET_DATA_OUT (see the other commit)
---
events.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ events.h | 40 +++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 0 deletions(-)
diff --git a/events.c b/events.c new file mode 100644 index 0000000..7ba5e34 --- /dev/null +++ b/events.c @@ -0,0 +1,90 @@ +/** + * $Id$ + * + * Copyright (C) 2009 SIP-Router.org + * + * This file is part of Extensible SIP Router, a free SIP server. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "dprint.h" +#include "mem/mem.h" +#include "events.h" + +static sr_event_cb_t _sr_events_list; +static int _sr_events_inited = 0; + +void sr_event_cb_init(void) +{ + if(_sr_events_inited == 0) + { + memset(&_sr_events_list, 0, sizeof(sr_event_cb_t)); + _sr_events_inited = 1; + } +} + +int sr_event_register_cb(int type, sr_event_cb_f f) +{ + sr_event_cb_init(); + switch(type) { + case SREV_NET_DATA_IN: + if(_sr_events_list.net_data_in==0) + _sr_events_list.net_data_in = f; + else return -1; + break; + case SREV_NET_DATA_OUT: + if(_sr_events_list.net_data_out==0) + _sr_events_list.net_data_out = f; + else return -1; + break; + default: + return -1; + } + return 0; +} + +int sr_event_exec(int type, void *data) +{ + int ret; + str *p; + switch(type) { + case SREV_NET_DATA_IN: + if(_sr_events_list.net_data_in!=0) + { + p = (str*)data; + LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n" + "%.*s\n+++++\n", p->len, p->s); + ret = _sr_events_list.net_data_in(data); + LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n" + "%.*s\n+++++\n", p->len, p->s); + return ret; + } else return 1; + break; + case SREV_NET_DATA_OUT: + if(_sr_events_list.net_data_out!=0) + { + p = (str*)data; + LM_DBG("PRE-OUT ++++++++++++++++++++\n" + "%.*s\n+++++++++++++++++++\n", p->len, p->s); + ret = _sr_events_list.net_data_out(data); + LM_DBG("POST-OUT ++++++++++++++++++++\n" + "%.*s\n+++++++++++++++++++\n", p->len, p->s); + return ret; + } else return 1; + break; + default: + return -1; + } +} + diff --git a/events.h b/events.h new file mode 100644 index 0000000..9fda619 --- /dev/null +++ b/events.h @@ -0,0 +1,40 @@ +/** + * $Id$ + * + * Copyright (C) 2009 SIP-Router.org + * + * This file is part of Extensible SIP Router, a free SIP server. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _SR_EVENTS_H_ +#define _SR_EVENTS_H_ + +#include "parser/msg_parser.h" + +#define SREV_NET_DATA_IN 1 +#define SREV_NET_DATA_OUT 2 + +typedef int (*sr_event_cb_f)(void *data); + +typedef struct sr_event_cb { + sr_event_cb_f net_data_in; + sr_event_cb_f net_data_out; +} sr_event_cb_t; + +void sr_event_cb_init(void); +int sr_event_register_cb(int type, sr_event_cb_f f); +int sr_event_exec(int type, void *data); + +#endif