Browse code

htable: added iterator function to set expire of current item

Daniel-Constantin Mierla authored on 22/10/2021 18:44:30
Showing 4 changed files
... ...
@@ -1166,6 +1166,31 @@ sht_iterator_end("i1");
1166 1166
     sht_iterator_seti("i1", "20");
1167 1167
     sht_iterator_end("i1");
1168 1168
 ...
1169
+</programlisting>
1170
+		</example>
1171
+	</section>
1172
+	<section id="htable.f.sht_iterator_setex">
1173
+		<title>
1174
+		<function moreinfo="none">sht_iterator_setex(iname, exval)</function>
1175
+		</title>
1176
+		<para>
1177
+			Set the expire of the current item to the integer in the exval.
1178
+		</para>
1179
+		<para>
1180
+			The parameters can be dynamic strings or integers with variables.
1181
+		</para>
1182
+		<para>
1183
+			This function can be used from ANY_ROUTE.
1184
+		</para>
1185
+		<example>
1186
+		<title><function>sht_iterator_setex</function> usage</title>
1187
+		<programlisting format="linespecific">
1188
+...
1189
+    sht_iterator_start("i1", "h1");
1190
+    sht_iterator_next("i1");
1191
+    sht_iterator_setex("i1", "120");
1192
+    sht_iterator_end("i1");
1193
+...
1169 1194
 </programlisting>
1170 1195
 		</example>
1171 1196
 	</section>
... ...
@@ -1978,6 +1978,33 @@ int ht_iterator_seti(str *iname, int ival)
1978 1978
 	return 0;
1979 1979
 }
1980 1980
 
1981
+int ht_iterator_setex(str *iname, int exval)
1982
+{
1983
+	int k;
1984
+	ht_cell_t *itb;
1985
+
1986
+	k = ht_iterator_find(iname);
1987
+	if(k==-1) {
1988
+		LM_ERR("iterator not found [%.*s]\n", iname->len, iname->s);
1989
+		return -1;
1990
+	}
1991
+	if(_ht_iterators[k].ht==NULL) {
1992
+		LM_ERR("iterator not initialized [%.*s]\n", iname->len, iname->s);
1993
+		return -1;
1994
+	}
1995
+	if(_ht_iterators[k].it==NULL) {
1996
+		LM_ERR("iterator not used [%.*s]\n", iname->len, iname->s);
1997
+		return -1;
1998
+	}
1999
+
2000
+	itb = _ht_iterators[k].it;
2001
+
2002
+	/* update expire */
2003
+	itb->expire = time(NULL) + exval;
2004
+
2005
+	return 0;
2006
+}
2007
+
1981 2008
 ht_cell_t* ht_iterator_get_current(str *iname)
1982 2009
 {
1983 2010
 	int k;
... ...
@@ -132,6 +132,7 @@ int ht_iterator_end(str *iname);
132 132
 int ht_iterator_rm(str *iname);
133 133
 int ht_iterator_sets(str *iname, str *sval);
134 134
 int ht_iterator_seti(str *iname, int ival);
135
+int ht_iterator_setex(str *iname, int exval);
135 136
 ht_cell_t* ht_iterator_get_current(str *iname);
136 137
 
137 138
 void ht_slot_lock(ht_t *ht, int idx);
... ...
@@ -83,6 +83,7 @@ static int w_ht_iterator_end(struct sip_msg* msg, char* iname, char* foo);
83 83
 static int w_ht_iterator_rm(struct sip_msg* msg, char* iname, char* foo);
84 84
 static int w_ht_iterator_sets(struct sip_msg* msg, char* iname, char* val);
85 85
 static int w_ht_iterator_seti(struct sip_msg* msg, char* iname, char* val);
86
+static int w_ht_iterator_setex(struct sip_msg* msg, char* iname, char* val);
86 87
 
87 88
 int ht_param(modparam_t type, void* val);
88 89
 
... ...
@@ -149,6 +150,8 @@ static cmd_export_t cmds[]={
149 150
 		fixup_free_spve_spve, ANY_ROUTE},
150 151
 	{"sht_iterator_seti",	(cmd_function)w_ht_iterator_seti,	2, fixup_spve_igp,
151 152
 		fixup_free_spve_igp, ANY_ROUTE},
153
+	{"sht_iterator_setex",	(cmd_function)w_ht_iterator_setex,	2, fixup_spve_igp,
154
+		fixup_free_spve_igp, ANY_ROUTE},
152 155
 
153 156
 	{"bind_htable",     (cmd_function)bind_htable,     0, 0, 0,
154 157
 		ANY_ROUTE},
... ...
@@ -890,6 +893,39 @@ static int w_ht_iterator_seti(struct sip_msg* msg, char* iname, char* val)
890 893
 	return ki_ht_iterator_seti(msg, &siname, ival);
891 894
 }
892 895
 
896
+static int ki_ht_iterator_setex(sip_msg_t *msg, str *iname, int exval)
897
+{
898
+	int ret;
899
+
900
+	if(iname==NULL || iname->s==NULL || iname->len<=0) {
901
+		LM_ERR("invalid parameters\n");
902
+		return -1;
903
+	}
904
+
905
+	ret = ht_iterator_setex(iname, exval);
906
+
907
+	return (ret==0)?1:ret;
908
+}
909
+
910
+static int w_ht_iterator_setex(struct sip_msg* msg, char* iname, char* val)
911
+{
912
+	str siname;
913
+	int ival;
914
+
915
+	if(fixup_get_svalue(msg, (gparam_t*)iname, &siname)<0 || siname.len<=0)
916
+	{
917
+		LM_ERR("cannot get iterator name\n");
918
+		return -1;
919
+	}
920
+	if(fixup_get_ivalue(msg, (gparam_t*)val, &ival)<0)
921
+	{
922
+		LM_ERR("cannot get value\n");
923
+		return -1;
924
+	}
925
+
926
+	return ki_ht_iterator_seti(msg, &siname, ival);
927
+}
928
+
893 929
 static int ki_ht_slot_xlock(sip_msg_t *msg, str *htname, str *skey, int lmode)
894 930
 {
895 931
 	ht_t *ht;
... ...
@@ -1988,6 +2024,11 @@ static sr_kemi_t sr_kemi_htable_exports[] = {
1988 2024
 		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_NONE,
1989 2025
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1990 2026
 	},
2027
+	{ str_init("htable"), str_init("sht_iterator_setex"),
2028
+		SR_KEMIP_INT, ki_ht_iterator_setex,
2029
+		{ SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_NONE,
2030
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2031
+	},
1991 2032
 	{ str_init("htable"), str_init("sht_rm"),
1992 2033
 		SR_KEMIP_INT, ki_ht_rm,
1993 2034
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,