pkg: fix wrong package name, closes FS#148, reported from Andrew Pogrebennyk
[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 /*!
22  * \file
23  * \brief SIP-router core :: 
24  * \ingroup core
25  * Module: \ref core
26  */
27
28 #include "dprint.h"
29 #include "mem/mem.h"
30 #include "events.h"
31
32 static sr_event_cb_t _sr_events_list;
33 static int _sr_events_inited = 0;
34
35 /**
36  *
37  */
38 void sr_event_cb_init(void)
39 {
40         if(_sr_events_inited == 0)
41         {
42                 memset(&_sr_events_list, 0, sizeof(sr_event_cb_t));
43                 _sr_events_inited = 1;
44         }
45 }
46
47 /**
48  *
49  */
50 int sr_event_register_cb(int type, sr_event_cb_f f)
51 {
52         sr_event_cb_init();
53         switch(type) {
54                 case SREV_NET_DATA_IN:
55                                 if(_sr_events_list.net_data_in==0)
56                                         _sr_events_list.net_data_in = f;
57                                 else return -1;
58                         break;
59                 case SREV_NET_DATA_OUT:
60                                 if(_sr_events_list.net_data_out==0)
61                                         _sr_events_list.net_data_out = f;
62                                 else return -1;
63                         break;
64                 case SREV_CORE_STATS:
65                                 if(_sr_events_list.core_stats==0)
66                                         _sr_events_list.core_stats = f;
67                                 else return -1;
68                         break;
69                 case SREV_CFG_RUN_ACTION:
70                                 if(_sr_events_list.run_action==0)
71                                         _sr_events_list.run_action = f;
72                                 else return -1;
73                         break;
74                 case SREV_PKG_SET_USED:
75                                 if(_sr_events_list.pkg_set_used==0)
76                                         _sr_events_list.pkg_set_used = f;
77                                 else return -1;
78                         break;
79                 case SREV_PKG_SET_REAL_USED:
80                                 if(_sr_events_list.pkg_set_real_used==0)
81                                         _sr_events_list.pkg_set_real_used = f;
82                                 else return -1;
83                         break;
84                 case SREV_NET_DGRAM_IN:
85                                 if(_sr_events_list.net_dgram_in==0)
86                                         _sr_events_list.net_dgram_in = f;
87                                 else return -1;
88                         break;
89                 default:
90                         return -1;
91         }
92         return 0;
93 }
94
95 /**
96  *
97  */
98 int sr_event_exec(int type, void *data)
99 {
100         int ret;
101 #ifdef EXTRA_DEBUG
102         str *p;
103 #endif /* EXTRA_DEBUG */
104         switch(type) {
105                 case SREV_NET_DATA_IN:
106                                 if(unlikely(_sr_events_list.net_data_in!=0))
107                                 {
108 #ifdef EXTRA_DEBUG
109                                         p = (str*)data;
110                                         LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n"
111                                                         "%.*s\n+++++\n", p->len, p->s);
112 #endif /* EXTRA_DEBUG */
113                                         ret = _sr_events_list.net_data_in(data);
114 #ifdef EXTRA_DEBUG
115                                         LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n"
116                                                         "%.*s\n+++++\n", p->len, p->s);
117 #endif /* EXTRA_DEBUG */
118                                         return ret;
119                                 } else return 1;
120                         break;
121                 case SREV_NET_DATA_OUT:
122                                 if(unlikely(_sr_events_list.net_data_out!=0))
123                                 {
124 #ifdef EXTRA_DEBUG
125                                         p = (str*)data;
126                                         LM_DBG("PRE-OUT ++++++++++++++++++++\n"
127                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
128 #endif /* EXTRA_DEBUG */
129                                         ret = _sr_events_list.net_data_out(data);
130 #ifdef EXTRA_DEBUG
131                                         LM_DBG("POST-OUT ++++++++++++++++++++\n"
132                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
133 #endif /* EXTRA_DEBUG */
134                                         return ret;
135                                 } else return 1;
136                         break;
137                 case SREV_CORE_STATS:
138                                 if(unlikely(_sr_events_list.core_stats!=0))
139                                 {
140                                         ret = _sr_events_list.core_stats(data);
141                                         return ret;
142                                 } else return 1;
143                         break;
144                 case SREV_CFG_RUN_ACTION:
145                                 if(unlikely(_sr_events_list.run_action!=0))
146                                 {
147                                         ret = _sr_events_list.run_action(data);
148                                         return ret;
149                                 } else return 1;
150                 case SREV_PKG_SET_USED:
151                                 if(unlikely(_sr_events_list.pkg_set_used!=0))
152                                 {
153                                         ret = _sr_events_list.pkg_set_used(data);
154                                         return ret;
155                                 } else return 1;
156                 case SREV_PKG_SET_REAL_USED:
157                                 if(unlikely(_sr_events_list.pkg_set_real_used!=0))
158                                 {
159                                         ret = _sr_events_list.pkg_set_real_used(data);
160                                         return ret;
161                                 } else return 1;
162                 case SREV_NET_DGRAM_IN:
163                                 if(unlikely(_sr_events_list.net_dgram_in!=0))
164                                 {
165                                         ret = _sr_events_list.net_dgram_in(data);
166                                         return ret;
167                                 } else return 1;
168                 default:
169                         return -1;
170         }
171 }
172
173 /**
174  *
175  */
176 int sr_event_enabled(int type)
177 {
178         switch(type) {
179                 case SREV_NET_DATA_IN:
180                                 return (_sr_events_list.net_data_in!=0)?1:0;
181                 case SREV_NET_DATA_OUT:
182                                 return (_sr_events_list.net_data_out!=0)?1:0;
183                 case SREV_CORE_STATS:
184                                 return (_sr_events_list.core_stats!=0)?1:0;
185                 case SREV_CFG_RUN_ACTION:
186                                 return (_sr_events_list.run_action!=0)?1:0;
187                 case SREV_PKG_SET_USED:
188                                 return (_sr_events_list.pkg_set_used!=0)?1:0;
189                 case SREV_PKG_SET_REAL_USED:
190                                 return (_sr_events_list.pkg_set_real_used!=0)?1:0;
191                 case SREV_NET_DGRAM_IN:
192                                 return (_sr_events_list.net_dgram_in!=0)?1:0;
193         }
194         return 0;
195 }
196