core ev: minor cleanups
[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(unlikely(_sr_events_list.net_data_in!=0))
64                                 {
65                                         p = (str*)data;
66 #ifdef EXTRA_DEBUG
67                                         LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n"
68                                                         "%.*s\n+++++\n", p->len, p->s);
69 #endif /* EXTRA_DEBUG */
70                                         ret = _sr_events_list.net_data_in(data);
71 #ifdef EXTRA_DEBUG
72                                         LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n"
73                                                         "%.*s\n+++++\n", p->len, p->s);
74 #endif /* EXTRA_DEBUG */
75                                         return ret;
76                                 } else return 1;
77                         break;
78                 case SREV_NET_DATA_OUT:
79                                 if(unlikely(_sr_events_list.net_data_out!=0))
80                                 {
81                                         p = (str*)data;
82 #ifdef EXTRA_DEBUG
83                                         LM_DBG("PRE-OUT ++++++++++++++++++++\n"
84                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
85 #endif /* EXTRA_DEBUG */
86                                         ret = _sr_events_list.net_data_out(data);
87 #ifdef EXTRA_DEBUG
88                                         LM_DBG("POST-OUT ++++++++++++++++++++\n"
89                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
90 #endif /* EXTRA_DEBUG */
91                                         return ret;
92                                 } else return 1;
93                         break;
94                 default:
95                         return -1;
96         }
97 }
98