Browse code

core: new sr events system

- designed to have small footprint in core
- the goal is to execute a event manager function during core processing
- event managers can be implemented in modules or elsewhere
- two event types added: SREV_NET_DATA_IN and SREV_NET_DATA_OUT (see the
other commit)

Daniel-Constantin Mierla authored on 04/08/2009 14:47:10
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,90 @@
0
+/**
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2009 SIP-Router.org
4
+ *
5
+ * This file is part of Extensible SIP Router, a free SIP server.
6
+ *
7
+ * Permission to use, copy, modify, and distribute this software for any
8
+ * purpose with or without fee is hereby granted, provided that the above
9
+ * copyright notice and this permission notice appear in all copies.
10
+ *
11
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
+ */
19
+
20
+#include "dprint.h"
21
+#include "mem/mem.h"
22
+#include "events.h"
23
+
24
+static sr_event_cb_t _sr_events_list;
25
+static int _sr_events_inited = 0;
26
+
27
+void sr_event_cb_init(void)
28
+{
29
+	if(_sr_events_inited == 0)
30
+	{
31
+		memset(&_sr_events_list, 0, sizeof(sr_event_cb_t));
32
+		_sr_events_inited = 1;
33
+	}
34
+}
35
+
36
+int sr_event_register_cb(int type, sr_event_cb_f f)
37
+{
38
+	sr_event_cb_init();
39
+	switch(type) {
40
+		case SREV_NET_DATA_IN:
41
+				if(_sr_events_list.net_data_in==0)
42
+					_sr_events_list.net_data_in = f;
43
+				else return -1;
44
+			break;
45
+		case SREV_NET_DATA_OUT:
46
+				if(_sr_events_list.net_data_out==0)
47
+					_sr_events_list.net_data_out = f;
48
+				else return -1;
49
+			break;
50
+		default:
51
+			return -1;
52
+	}
53
+	return 0;
54
+}
55
+
56
+int sr_event_exec(int type, void *data)
57
+{
58
+	int ret;
59
+	str *p;
60
+	switch(type) {
61
+		case SREV_NET_DATA_IN:
62
+				if(_sr_events_list.net_data_in!=0)
63
+				{
64
+					p = (str*)data;
65
+					LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n"
66
+							"%.*s\n+++++\n", p->len, p->s);
67
+					ret = _sr_events_list.net_data_in(data);
68
+					LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n"
69
+							"%.*s\n+++++\n", p->len, p->s);
70
+					return ret;
71
+				} else return 1;
72
+			break;
73
+		case SREV_NET_DATA_OUT:
74
+				if(_sr_events_list.net_data_out!=0)
75
+				{
76
+					p = (str*)data;
77
+					LM_DBG("PRE-OUT ++++++++++++++++++++\n"
78
+							"%.*s\n+++++++++++++++++++\n", p->len, p->s);
79
+					ret = _sr_events_list.net_data_out(data);
80
+					LM_DBG("POST-OUT ++++++++++++++++++++\n"
81
+							"%.*s\n+++++++++++++++++++\n", p->len, p->s);
82
+					return ret;
83
+				} else return 1;
84
+			break;
85
+		default:
86
+			return -1;
87
+	}
88
+}
89
+
0 90
new file mode 100644
... ...
@@ -0,0 +1,40 @@
0
+/**
1
+ * $Id$
2
+ *
3
+ * Copyright (C) 2009 SIP-Router.org
4
+ *
5
+ * This file is part of Extensible SIP Router, a free SIP server.
6
+ *
7
+ * Permission to use, copy, modify, and distribute this software for any
8
+ * purpose with or without fee is hereby granted, provided that the above
9
+ * copyright notice and this permission notice appear in all copies.
10
+ *
11
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
+ */
19
+
20
+#ifndef _SR_EVENTS_H_
21
+#define _SR_EVENTS_H_
22
+
23
+#include "parser/msg_parser.h"
24
+
25
+#define SREV_NET_DATA_IN	1
26
+#define SREV_NET_DATA_OUT	2
27
+
28
+typedef int (*sr_event_cb_f)(void *data);
29
+
30
+typedef struct sr_event_cb {
31
+	sr_event_cb_f net_data_in;
32
+	sr_event_cb_f net_data_out;
33
+} sr_event_cb_t;
34
+
35
+void sr_event_cb_init(void);
36
+int sr_event_register_cb(int type, sr_event_cb_f f);
37
+int sr_event_exec(int type, void *data);
38
+
39
+#endif