From 402ff3411c52ee70e6cc97e1b559dd515159b146 Mon Sep 17 00:00:00 2001 From: Razvan Crainea Date: Tue, 6 Jun 2017 12:57:15 +0300 Subject: [PATCH] cgrates: add id in request Add an identifier for the requests done to cgrates. Requested by cgrates/cgrates#674 --- modules/cgrates/cgrates.c | 3 +-- modules/cgrates/cgrates_common.c | 33 ++++++++++++++++++++++++++++++++ modules/cgrates/cgrates_common.h | 3 +++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/modules/cgrates/cgrates.c b/modules/cgrates/cgrates.c index 1d443052314..409ecf14bbd 100644 --- a/modules/cgrates/cgrates.c +++ b/modules/cgrates/cgrates.c @@ -274,8 +274,7 @@ static int child_init(int rank) cgrc_start_listen(c); } } - - return 0; + return cgr_init_common(); } diff --git a/modules/cgrates/cgrates_common.c b/modules/cgrates/cgrates_common.c index fa5181a5a9e..95a620a8759 100644 --- a/modules/cgrates/cgrates_common.c +++ b/modules/cgrates/cgrates_common.c @@ -20,6 +20,7 @@ */ #include +#include #include "../../dprint.h" #include "../../str.h" #include "../../async.h" @@ -149,6 +150,35 @@ int cgrates_set_reply(int type, int_str *value) return 0; } +static char cgr_id_buffer[5 /* pid */ + 1 /* '-' */ + INT2STR_MAX_LEN]; +static int cgr_id_len = 0; +static unsigned long cgr_id_index = 0; + +int cgr_init_common(void) +{ + /* add the pid */ + char *p = int2str(my_pid(), &cgr_id_len); + memcpy(cgr_id_buffer, p, cgr_id_len); + + /* add the dash */ + cgr_id_buffer[cgr_id_len++] = '-'; + + /* init the index */ + cgr_id_index = (unsigned long)rand(); + + return 0; +} + + +static inline char *cgr_unique_id(void) +{ + int len; + char *p = int2str(cgr_id_index++, &len); + memcpy(cgr_id_buffer + cgr_id_len, p, len); + cgr_id_buffer[cgr_id_len + len] = '\0'; + return cgr_id_buffer; +} + #define JSON_CHECK(_c, _s) \ do { \ if (!(_c)) { \ @@ -171,6 +201,9 @@ struct cgr_msg *cgr_get_generic_msg(str *method, struct cgr_session *s) JSON_CHECK(jtmp = json_object_new_string_len(method->s, method->len), "method"); json_object_object_add(cmsg.msg,"method", jtmp); + JSON_CHECK(jtmp = json_object_new_string(cgr_unique_id()), "id"); + json_object_object_add(cmsg.msg, "id", jtmp); + JSON_CHECK(jarr = json_object_new_array(), "params array"); json_object_object_add(cmsg.msg,"params", jarr); diff --git a/modules/cgrates/cgrates_common.h b/modules/cgrates/cgrates_common.h index fb1e014d2be..5bfc9a47163 100644 --- a/modules/cgrates/cgrates_common.h +++ b/modules/cgrates/cgrates_common.h @@ -104,6 +104,9 @@ struct cgr_msg { json_object *params; }; +/* init common variables */ +int cgr_init_common(void); + /* message builder */ int cgrates_set_reply(int type, int_str *value); struct cgr_msg *cgr_get_generic_msg(str *method, struct cgr_session *sess);