Merge remote branch 'origin/tmp/k3.0_sr_backports' into sr_3.0
[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                 case SREV_CORE_STATS:
52                                 if(_sr_events_list.core_stats==0)
53                                         _sr_events_list.core_stats = f;
54                                 else return -1;
55                         break;
56                 default:
57                         return -1;
58         }
59         return 0;
60 }
61
62 int sr_event_exec(int type, void *data)
63 {
64         int ret;
65         str *p;
66         switch(type) {
67                 case SREV_NET_DATA_IN:
68                                 if(unlikely(_sr_events_list.net_data_in!=0))
69                                 {
70                                         p = (str*)data;
71 #ifdef EXTRA_DEBUG
72                                         LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n"
73                                                         "%.*s\n+++++\n", p->len, p->s);
74 #endif /* EXTRA_DEBUG */
75                                         ret = _sr_events_list.net_data_in(data);
76 #ifdef EXTRA_DEBUG
77                                         LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n"
78                                                         "%.*s\n+++++\n", p->len, p->s);
79 #endif /* EXTRA_DEBUG */
80                                         return ret;
81                                 } else return 1;
82                         break;
83                 case SREV_NET_DATA_OUT:
84                                 if(unlikely(_sr_events_list.net_data_out!=0))
85                                 {
86                                         p = (str*)data;
87 #ifdef EXTRA_DEBUG
88                                         LM_DBG("PRE-OUT ++++++++++++++++++++\n"
89                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
90 #endif /* EXTRA_DEBUG */
91                                         ret = _sr_events_list.net_data_out(data);
92 #ifdef EXTRA_DEBUG
93                                         LM_DBG("POST-OUT ++++++++++++++++++++\n"
94                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
95 #endif /* EXTRA_DEBUG */
96                                         return ret;
97                                 } else return 1;
98                         break;
99                 case SREV_CORE_STATS:
100                                 if(unlikely(_sr_events_list.core_stats!=0))
101                                 {
102                                         ret = _sr_events_list.core_stats(data);
103                                         return ret;
104                                 } else return 1;
105                         break;
106                 default:
107                         return -1;
108         }
109 }
110