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