Browse code

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

Daniel-Constantin Mierla authored on 20/05/2020 19:33:17
Showing 2 changed files
... ...
@@ -271,5 +271,6 @@ void ds_avl_destroy(ds_set_t **node);
271 271
 int ds_manage_routes(sip_msg_t *msg, ds_select_state_t *rstate);
272 272
 
273 273
 ds_rctx_t* ds_get_rctx(void);
274
+unsigned int ds_get_hash(str *x, str *y);
274 275
 
275 276
 #endif
... ...
@@ -1816,6 +1816,49 @@ static void dispatcher_rpc_remove(rpc_t *rpc, void *ctx)
1816 1816
 	return;
1817 1817
 }
1818 1818
 
1819
+static const char *dispatcher_rpc_hash_doc[2] = {
1820
+		"Compute the hash if the values", 0};
1821
+
1822
+
1823
+/*
1824
+ * RPC command to compute the hash of the values
1825
+ */
1826
+static void dispatcher_rpc_hash(rpc_t *rpc, void *ctx)
1827
+{
1828
+	int n = 0;
1829
+	unsigned int hashid = 0;
1830
+	int nslots = 0;
1831
+	str val1 = STR_NULL;
1832
+	str val2 = STR_NULL;
1833
+	void *th;
1834
+
1835
+	n = rpc->scan(ctx, "dS*S", &nslots, &val1, &val2);
1836
+	if(n < 2) {
1837
+		rpc->fault(ctx, 500, "Invalid Parameters");
1838
+		return;
1839
+	}
1840
+	if(n==2) {
1841
+		val2.s = NULL;
1842
+		val2.s = 0;
1843
+	}
1844
+
1845
+	hashid = ds_get_hash(&val1, &val2);
1846
+
1847
+	/* add entry node */
1848
+	if(rpc->add(ctx, "{", &th) < 0) {
1849
+		rpc->fault(ctx, 500, "Internal error root reply");
1850
+		return;
1851
+	}
1852
+	if(rpc->struct_add(th, "uu", "hashid", hashid,
1853
+				"slot", (nslots>0)?(hashid%nslots):0)
1854
+			< 0) {
1855
+		rpc->fault(ctx, 500, "Internal error reply structure");
1856
+		return;
1857
+	}
1858
+
1859
+	return;
1860
+}
1861
+
1819 1862
 /* clang-format off */
1820 1863
 rpc_export_t dispatcher_rpc_cmds[] = {
1821 1864
 	{"dispatcher.reload", dispatcher_rpc_reload,
... ...
@@ -1830,6 +1873,8 @@ rpc_export_t dispatcher_rpc_cmds[] = {
1830 1873
 		dispatcher_rpc_add_doc, 0},
1831 1874
 	{"dispatcher.remove",   dispatcher_rpc_remove,
1832 1875
 		dispatcher_rpc_remove_doc, 0},
1876
+	{"dispatcher.hash",   dispatcher_rpc_hash,
1877
+		dispatcher_rpc_hash_doc, 0},
1833 1878
 	{0, 0, 0, 0}
1834 1879
 };
1835 1880
 /* clang-format on */