[sr-dev] git:master:aac6c77c: dispatcher: added rpc command dispatcher.hash

Daniel-Constantin Mierla miconda at gmail.com
Wed May 20 21:49:15 CEST 2020


Module: kamailio
Branch: master
Commit: aac6c77cac36ea28897fcec539d750e8bf652d55
URL: https://github.com/kamailio/kamailio/commit/aac6c77cac36ea28897fcec539d750e8bf652d55

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date: 2020-05-20T21:33:17+02:00

dispatcher: added rpc command dispatcher.hash

- prototype: dispatcher.hash nslots val1 [val2]
- compute the hash id of the value and do modulo operation if first
parameter is not 0
- val2 is optional

---

Modified: src/modules/dispatcher/dispatch.h
Modified: src/modules/dispatcher/dispatcher.c

---

Diff:  https://github.com/kamailio/kamailio/commit/aac6c77cac36ea28897fcec539d750e8bf652d55.diff
Patch: https://github.com/kamailio/kamailio/commit/aac6c77cac36ea28897fcec539d750e8bf652d55.patch

---

diff --git a/src/modules/dispatcher/dispatch.h b/src/modules/dispatcher/dispatch.h
index 6c167a9551..59937b9994 100644
--- a/src/modules/dispatcher/dispatch.h
+++ b/src/modules/dispatcher/dispatch.h
@@ -271,5 +271,6 @@ void ds_avl_destroy(ds_set_t **node);
 int ds_manage_routes(sip_msg_t *msg, ds_select_state_t *rstate);
 
 ds_rctx_t* ds_get_rctx(void);
+unsigned int ds_get_hash(str *x, str *y);
 
 #endif
diff --git a/src/modules/dispatcher/dispatcher.c b/src/modules/dispatcher/dispatcher.c
index 6ac01bbd2a..9bd51a2544 100644
--- a/src/modules/dispatcher/dispatcher.c
+++ b/src/modules/dispatcher/dispatcher.c
@@ -1816,6 +1816,49 @@ static void dispatcher_rpc_remove(rpc_t *rpc, void *ctx)
 	return;
 }
 
+static const char *dispatcher_rpc_hash_doc[2] = {
+		"Compute the hash if the values", 0};
+
+
+/*
+ * RPC command to compute the hash of the values
+ */
+static void dispatcher_rpc_hash(rpc_t *rpc, void *ctx)
+{
+	int n = 0;
+	unsigned int hashid = 0;
+	int nslots = 0;
+	str val1 = STR_NULL;
+	str val2 = STR_NULL;
+	void *th;
+
+	n = rpc->scan(ctx, "dS*S", &nslots, &val1, &val2);
+	if(n < 2) {
+		rpc->fault(ctx, 500, "Invalid Parameters");
+		return;
+	}
+	if(n==2) {
+		val2.s = NULL;
+		val2.s = 0;
+	}
+
+	hashid = ds_get_hash(&val1, &val2);
+
+	/* add entry node */
+	if(rpc->add(ctx, "{", &th) < 0) {
+		rpc->fault(ctx, 500, "Internal error root reply");
+		return;
+	}
+	if(rpc->struct_add(th, "uu", "hashid", hashid,
+				"slot", (nslots>0)?(hashid%nslots):0)
+			< 0) {
+		rpc->fault(ctx, 500, "Internal error reply structure");
+		return;
+	}
+
+	return;
+}
+
 /* clang-format off */
 rpc_export_t dispatcher_rpc_cmds[] = {
 	{"dispatcher.reload", dispatcher_rpc_reload,
@@ -1830,6 +1873,8 @@ rpc_export_t dispatcher_rpc_cmds[] = {
 		dispatcher_rpc_add_doc, 0},
 	{"dispatcher.remove",   dispatcher_rpc_remove,
 		dispatcher_rpc_remove_doc, 0},
+	{"dispatcher.hash",   dispatcher_rpc_hash,
+		dispatcher_rpc_hash_doc, 0},
 	{0, 0, 0, 0}
 };
 /* clang-format on */




More information about the sr-dev mailing list