Browse code

core: kemi - added the index for callback names

- will keep the mapping between the embedded api callabcks and
transaction events

Daniel-Constantin Mierla authored on 13/04/2016 14:19:39
Showing 2 changed files
... ...
@@ -24,6 +24,8 @@
24 24
 #include <stdlib.h>
25 25
 
26 26
 #include "dprint.h"
27
+#include "locking.h"
28
+#include "mem/shm.h"
27 29
 
28 30
 #include "kemi.h"
29 31
 
... ...
@@ -92,7 +94,6 @@ static int lua_sr_kemi_info(sip_msg_t *msg, str *txt)
92 94
 	return 0;
93 95
 }
94 96
 
95
-
96 97
 /**
97 98
  *
98 99
  */
... ...
@@ -208,6 +209,10 @@ int sr_kemi_eng_set(str *ename, str *cpath)
208 209
 		return 0;
209 210
 	}
210 211
 
212
+	if(sr_kemi_cbname_list_init()<0) {
213
+		return -1;
214
+	}
215
+
211 216
 	for(i=0; i<_sr_kemi_eng_list_size; i++) {
212 217
 		if(_sr_kemi_eng_list[i].ename.len==ename->len
213 218
 				&& strncasecmp(_sr_kemi_eng_list[i].ename.s, ename->s,
... ...
@@ -240,8 +245,127 @@ int sr_kemi_eng_setz(char *ename, char *cpath)
240 245
 	}
241 246
 }
242 247
 
243
-
248
+/**
249
+ *
250
+ */
244 251
 sr_kemi_eng_t* sr_kemi_eng_get(void)
245 252
 {
246 253
 	return _sr_kemi_eng;
247 254
 }
255
+
256
+/**
257
+ *
258
+ */
259
+#define KEMI_CBNAME_MAX_LEN	128
260
+#define KEMI_CBNAME_LIST_SIZE	256
261
+
262
+typedef struct sr_kemi_cbname {
263
+	str name;
264
+	char bname[KEMI_CBNAME_MAX_LEN];
265
+} sr_kemi_cbname_t;
266
+
267
+static gen_lock_t *_sr_kemi_cbname_lock = 0;
268
+static sr_kemi_cbname_t *_sr_kemi_cbname_list = NULL;
269
+static int _sr_kemi_cbname_list_size = 0;
270
+
271
+/**
272
+ *
273
+ */
274
+int sr_kemi_cbname_list_init(void)
275
+{
276
+	if(_sr_kemi_cbname_list!=NULL) {
277
+		return 0;
278
+	}
279
+	if ( (_sr_kemi_cbname_lock=lock_alloc())==0) {
280
+		LM_CRIT("failed to alloc lock\n");
281
+		return -1;
282
+	}
283
+	if (lock_init(_sr_kemi_cbname_lock)==0 ) {
284
+		LM_CRIT("failed to init lock\n");
285
+		lock_dealloc(_sr_kemi_cbname_lock);
286
+		_sr_kemi_cbname_lock = NULL;
287
+		return -1;
288
+	}
289
+	_sr_kemi_cbname_list
290
+			= shm_malloc(KEMI_CBNAME_LIST_SIZE*sizeof(sr_kemi_cbname_t));
291
+	if(_sr_kemi_cbname_list==NULL) {
292
+		LM_ERR("no more shared memory\n");
293
+		lock_destroy(_sr_kemi_cbname_lock);
294
+		lock_dealloc(_sr_kemi_cbname_lock);
295
+		_sr_kemi_cbname_lock = NULL;
296
+		return -1;
297
+	}
298
+	memset(_sr_kemi_cbname_list, 0,
299
+			KEMI_CBNAME_LIST_SIZE*sizeof(sr_kemi_cbname_t));
300
+	return 0;
301
+}
302
+
303
+/**
304
+ *
305
+ */
306
+int sr_kemi_cbname_lookup_name(str *name)
307
+{
308
+	int n;
309
+	int i;
310
+
311
+	if(_sr_kemi_cbname_list==NULL) {
312
+		return 0;
313
+	}
314
+	if(name->len >= KEMI_CBNAME_MAX_LEN) {
315
+		LM_ERR("callback name is too long [%.*s] (max: %d)\n",
316
+				name->len, name->s, KEMI_CBNAME_MAX_LEN);
317
+		return 0;
318
+	}
319
+	n = _sr_kemi_cbname_list_size;
320
+
321
+	for(i=0; i<n; i++) {
322
+		if(_sr_kemi_cbname_list[i].name.len==name->len
323
+				&& strncmp(_sr_kemi_cbname_list[i].name.s,
324
+						name->s, name->len)==0) {
325
+			return i+1;
326
+		}
327
+	}
328
+
329
+	/* not found -- add it */
330
+	lock_get(_sr_kemi_cbname_lock);
331
+
332
+	/* check if new callback were indexed meanwhile */
333
+	for(; i<_sr_kemi_cbname_list_size; i++) {
334
+		if(_sr_kemi_cbname_list[i].name.len==name->len
335
+				&& strncmp(_sr_kemi_cbname_list[i].name.s,
336
+						name->s, name->len)==0) {
337
+			return i+1;
338
+		}
339
+	}
340
+	if(_sr_kemi_cbname_list_size>=KEMI_CBNAME_LIST_SIZE) {
341
+		lock_release(_sr_kemi_cbname_lock);
342
+		LM_ERR("no more space to index callbacks\n");
343
+		return 0;
344
+	}
345
+	strncpy(_sr_kemi_cbname_list[i].bname, name->s, name->len);
346
+	_sr_kemi_cbname_list[i].bname[name->len] = '\0';
347
+	_sr_kemi_cbname_list[i].name.s = _sr_kemi_cbname_list[i].bname;
348
+	_sr_kemi_cbname_list[i].name.len = name->len;
349
+	_sr_kemi_cbname_list_size++;
350
+	n = _sr_kemi_cbname_list_size;
351
+	lock_release(_sr_kemi_cbname_lock);
352
+	return n;
353
+}
354
+
355
+/**
356
+ *
357
+ */
358
+str* sr_kemi_cbname_lookup_idx(int idx)
359
+{
360
+	int n;
361
+
362
+	if(_sr_kemi_cbname_list==NULL) {
363
+		return NULL;
364
+	}
365
+	n = _sr_kemi_cbname_list_size;
366
+	if(idx<1 || idx>n) {
367
+		LM_ERR("index %d is out of range\n", idx);
368
+		return NULL;
369
+	}
370
+	return &_sr_kemi_cbname_list[idx-1].name;
371
+}
... ...
@@ -103,4 +103,8 @@ int sr_kemi_eng_set(str *ename, str *cpath);
103 103
 int sr_kemi_eng_setz(char *ename, char *cpath);
104 104
 sr_kemi_eng_t* sr_kemi_eng_get(void);
105 105
 
106
+int sr_kemi_cbname_list_init(void);
107
+int sr_kemi_cbname_lookup_name(str *name);
108
+str* sr_kemi_cbname_lookup_idx(int idx);
109
+
106 110
 #endif