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