core: new core event: SREV_CFG_RUN_ACTION
[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 void sr_event_cb_init(void)
36 {
37         if(_sr_events_inited == 0)
38         {
39                 memset(&_sr_events_list, 0, sizeof(sr_event_cb_t));
40                 _sr_events_inited = 1;
41         }
42 }
43
44 int sr_event_register_cb(int type, sr_event_cb_f f)
45 {
46         sr_event_cb_init();
47         switch(type) {
48                 case SREV_NET_DATA_IN:
49                                 if(_sr_events_list.net_data_in==0)
50                                         _sr_events_list.net_data_in = f;
51                                 else return -1;
52                         break;
53                 case SREV_NET_DATA_OUT:
54                                 if(_sr_events_list.net_data_out==0)
55                                         _sr_events_list.net_data_out = f;
56                                 else return -1;
57                         break;
58                 case SREV_CORE_STATS:
59                                 if(_sr_events_list.core_stats==0)
60                                         _sr_events_list.core_stats = f;
61                                 else return -1;
62                         break;
63                 case SREV_CFG_RUN_ACTION:
64                                 if(_sr_events_list.run_action==0)
65                                         _sr_events_list.run_action = f;
66                                 else return -1;
67                         break;
68                 default:
69                         return -1;
70         }
71         return 0;
72 }
73
74 int sr_event_exec(int type, void *data)
75 {
76         int ret;
77         str *p;
78         switch(type) {
79                 case SREV_NET_DATA_IN:
80                                 if(unlikely(_sr_events_list.net_data_in!=0))
81                                 {
82                                         p = (str*)data;
83 #ifdef EXTRA_DEBUG
84                                         LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n"
85                                                         "%.*s\n+++++\n", p->len, p->s);
86 #endif /* EXTRA_DEBUG */
87                                         ret = _sr_events_list.net_data_in(data);
88 #ifdef EXTRA_DEBUG
89                                         LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n"
90                                                         "%.*s\n+++++\n", p->len, p->s);
91 #endif /* EXTRA_DEBUG */
92                                         return ret;
93                                 } else return 1;
94                         break;
95                 case SREV_NET_DATA_OUT:
96                                 if(unlikely(_sr_events_list.net_data_out!=0))
97                                 {
98                                         p = (str*)data;
99 #ifdef EXTRA_DEBUG
100                                         LM_DBG("PRE-OUT ++++++++++++++++++++\n"
101                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
102 #endif /* EXTRA_DEBUG */
103                                         ret = _sr_events_list.net_data_out(data);
104 #ifdef EXTRA_DEBUG
105                                         LM_DBG("POST-OUT ++++++++++++++++++++\n"
106                                                         "%.*s\n+++++++++++++++++++\n", p->len, p->s);
107 #endif /* EXTRA_DEBUG */
108                                         return ret;
109                                 } else return 1;
110                         break;
111                 case SREV_CORE_STATS:
112                                 if(unlikely(_sr_events_list.core_stats!=0))
113                                 {
114                                         ret = _sr_events_list.core_stats(data);
115                                         return ret;
116                                 } else return 1;
117                         break;
118                 case SREV_CFG_RUN_ACTION:
119                                 if(unlikely(_sr_events_list.run_action!=0))
120                                 {
121                                         ret = _sr_events_list.run_action(data);
122                                         return ret;
123                                 } else return 1;
124                 default:
125                         return -1;
126         }
127 }
128