Browse code

core: allow up to three callback functions for net_data_in and net_data_out

- they are added normal for net_data_in and reverse for net_data_out,
allowing nested execution (e.g., first module registering the two
callbacks with have the first net_data_in and the last net_data_out)
- allow topoh module can be used with other functionalities needed the same
callbacks

Daniel-Constantin Mierla authored on 05/09/2014 14:11:51
Showing 2 changed files
... ...
@@ -95,17 +95,27 @@ void sr_event_cb_init(void)
95 95
  */
96 96
 int sr_event_register_cb(int type, sr_event_cb_f f)
97 97
 {
98
+	int i;
99
+
98 100
 	sr_event_cb_init();
99 101
 	switch(type) {
100 102
 		case SREV_NET_DATA_IN:
101
-				if(_sr_events_list.net_data_in==0)
102
-					_sr_events_list.net_data_in = f;
103
-				else return -1;
103
+				for(i=0; i<SREV_CB_LIST_SIZE; i++) {
104
+					if(_sr_events_list.net_data_in[i]==0) {
105
+						_sr_events_list.net_data_in[i] = f;
106
+						break;
107
+					}
108
+				}
109
+				if(i==SREV_CB_LIST_SIZE) return -1;
104 110
 			break;
105 111
 		case SREV_NET_DATA_OUT:
106
-				if(_sr_events_list.net_data_out==0)
107
-					_sr_events_list.net_data_out = f;
108
-				else return -1;
112
+				for(i=SREV_CB_LIST_SIZE-1; i>=0; i--) {
113
+					if(_sr_events_list.net_data_out[i]==0) {
114
+						_sr_events_list.net_data_out[i] = f;
115
+						break;
116
+					}
117
+				}
118
+				if(i<0) return -1;
109 119
 			break;
110 120
 		case SREV_CORE_STATS:
111 121
 				if(_sr_events_list.core_stats==0)
... ...
@@ -169,19 +179,24 @@ int sr_event_register_cb(int type, sr_event_cb_f f)
169 169
 int sr_event_exec(int type, void *data)
170 170
 {
171 171
 	int ret;
172
+	int i;
172 173
 #ifdef EXTRA_DEBUG
173 174
 	str *p;
174 175
 #endif /* EXTRA_DEBUG */
175 176
 	switch(type) {
176 177
 		case SREV_NET_DATA_IN:
177
-				if(unlikely(_sr_events_list.net_data_in!=0))
178
+				if(unlikely(_sr_events_list.net_data_in[0]!=0))
178 179
 				{
179 180
 #ifdef EXTRA_DEBUG
180 181
 					p = (str*)data;
181 182
 					LM_DBG("PRE-IN ++++++++++++++++++++++++++++++++\n"
182 183
 							"%.*s\n+++++\n", p->len, p->s);
183 184
 #endif /* EXTRA_DEBUG */
184
-					ret = _sr_events_list.net_data_in(data);
185
+					ret = 0;
186
+					for(i=0; i<SREV_CB_LIST_SIZE
187
+							&& _sr_events_list.net_data_out[i]; i++) {
188
+						ret |= _sr_events_list.net_data_in[i](data);
189
+					}
185 190
 #ifdef EXTRA_DEBUG
186 191
 					LM_DBG("POST-IN ++++++++++++++++++++++++++++++++\n"
187 192
 							"%.*s\n+++++\n", p->len, p->s);
... ...
@@ -190,14 +205,18 @@ int sr_event_exec(int type, void *data)
190 190
 				} else return 1;
191 191
 			break;
192 192
 		case SREV_NET_DATA_OUT:
193
-				if(unlikely(_sr_events_list.net_data_out!=0))
193
+				if(unlikely(_sr_events_list.net_data_out[SREV_CB_LIST_SIZE-1]!=0))
194 194
 				{
195 195
 #ifdef EXTRA_DEBUG
196 196
 					p = (str*)data;
197 197
 					LM_DBG("PRE-OUT ++++++++++++++++++++\n"
198 198
 							"%.*s\n+++++++++++++++++++\n", p->len, p->s);
199 199
 #endif /* EXTRA_DEBUG */
200
-					ret = _sr_events_list.net_data_out(data);
200
+					ret = 0;
201
+					for(i=SREV_CB_LIST_SIZE-1;
202
+							i>=0 && _sr_events_list.net_data_out[i]; i--) {
203
+						ret |= _sr_events_list.net_data_out[i](data);
204
+					}
201 205
 #ifdef EXTRA_DEBUG
202 206
 					LM_DBG("POST-OUT ++++++++++++++++++++\n"
203 207
 							"%.*s\n+++++++++++++++++++\n", p->len, p->s);
... ...
@@ -278,9 +297,9 @@ int sr_event_enabled(int type)
278 278
 {
279 279
 	switch(type) {
280 280
 		case SREV_NET_DATA_IN:
281
-				return (_sr_events_list.net_data_in!=0)?1:0;
281
+				return (_sr_events_list.net_data_in[0]!=0)?1:0;
282 282
 		case SREV_NET_DATA_OUT:
283
-				return (_sr_events_list.net_data_out!=0)?1:0;
283
+				return (_sr_events_list.net_data_out[SREV_CB_LIST_SIZE-1]!=0)?1:0;
284 284
 		case SREV_CORE_STATS:
285 285
 				return (_sr_events_list.core_stats!=0)?1:0;
286 286
 		case SREV_CFG_RUN_ACTION:
... ...
@@ -36,12 +36,13 @@
36 36
 #define SREV_TCP_WS_FRAME_OUT		11
37 37
 #define SREV_STUN_IN			12
38 38
 
39
+#define SREV_CB_LIST_SIZE	3
39 40
 
40 41
 typedef int (*sr_event_cb_f)(void *data);
41 42
 
42 43
 typedef struct sr_event_cb {
43
-	sr_event_cb_f net_data_in;
44
-	sr_event_cb_f net_data_out;
44
+	sr_event_cb_f net_data_in[SREV_CB_LIST_SIZE];
45
+	sr_event_cb_f net_data_out[SREV_CB_LIST_SIZE];
45 46
 	sr_event_cb_f core_stats;
46 47
 	sr_event_cb_f run_action;
47 48
 	sr_event_cb_f pkg_update_stats;