core: new sr events system
[sip-router] / events.c
1 /**
2  * $Id$
3  *
4  * Copyright (C) 2009 SIP-Router.org
5  *
6  * This file is part of Extensible SIP Router, a free SIP server.
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20
21 #include "dprint.h"
22 #include "mem/mem.h"
23 #include "events.h"
24
25 static sr_event_cb_t _sr_events_list;
26 static int _sr_events_inited = 0;
27
28 void sr_event_cb_init(void)
29 {
30         if(_sr_events_inited == 0)
31         {
32                 memset(&_sr_events_list, 0, sizeof(sr_event_cb_t));
33                 _sr_events_inited = 1;
34         }
35 }
36
37 int sr_event_register_cb(int type, sr_event_cb_f f)
38 {
39         sr_event_cb_init();
40         switch(type) {
41                 case SREV_NET_DATA_IN:
42                                 if(_sr_events_list.net_data_in==0)
43                                         _sr_events_list.net_data_in = f;
44                                 else return -1;
45                         break;
46                 case SREV_NET_DATA_OUT:
47                                 if(_sr_events_list.net_data_out==0)
48                                         _sr_events_list.net_data_out = f;
49                                 else return -1;
50                         break;
51                 default:
52                         return -1;
53         }
54         return 0;
55 }
56
57 int sr_event_exec(int type, void *data)
58 {
59         int ret;
60         str *p;
61         switch(type) {
62                 case SREV_NET_DATA_IN:
63                                 if(_sr_events_list.net_data_in!=0)
64                                 {
65                                         p = (str*)data;
66                                         LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n"
67                                                         "%.*s\n+++++\n", p->len, p->s);
68                                         ret = _sr_events_list.net_data_in(data);
69                                         LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n"
70                                                         "%.*s\n+++++\n", p->len, p->s);
71                                         return ret;
72                                 } else return 1;
73                         break;
74                 case SREV_NET_DATA_OUT:
75                                 if(_sr_events_list.net_data_out!=0)
76                                 {
77                                         p = (str*)data;
78                                         LM_DBG("PRE-OUT ++++++++++++++++++++\n"
79                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
80                                         ret = _sr_events_list.net_data_out(data);
81                                         LM_DBG("POST-OUT ++++++++++++++++++++\n"
82                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
83                                         return ret;
84                                 } else return 1;
85                         break;
86                 default:
87                         return -1;
88         }
89 }
90