Browse code

- update_stat() and reset_stat() functions accept PV for the name of the statistic. Both function are able to recognize at startup if the parmeter is string or PV and accordingly fix it in the most efficient way (for runtime). Based on an idea from patch 1984901

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@4316 689a6050-402a-0410-94f2-e92a70836424

Bogdan-Andrei Iancu authored on 05/06/2008 16:15:38
Showing 1 changed files
... ...
@@ -34,6 +34,7 @@
34 34
 #include "../../sr_module.h"
35 35
 #include "../../dprint.h"
36 36
 #include "../../ut.h"
37
+#include "../../mod_fix.h"
37 38
 #include "../../statistics.h"
38 39
 #include "../../mem/mem.h"
39 40
 #include "stats_funcs.h"
... ...
@@ -46,6 +47,12 @@ static int w_update_stat(struct sip_msg* msg, char* stat, char* n);
46 47
 static int w_reset_stat(struct sip_msg* msg, char* stat, char* foo);
47 48
 static int fixup_stat(void** param, int param_no);
48 49
 
50
+struct stat_or_pv {
51
+	stat_var   *stat;
52
+	pv_spec_t  *pv;
53
+};
54
+
55
+
49 56
 
50 57
 static cmd_export_t cmds[]={
51 58
 	{"update_stat",  (cmd_function)w_update_stat,  2, fixup_stat, 0,
... ...
@@ -100,7 +107,7 @@ static int mod_init(void)
100 107
 
101 108
 static int fixup_stat(void** param, int param_no)
102 109
 {
103
-	stat_var *stat;
110
+	struct stat_or_pv *sopv;
104 111
 	str s;
105 112
 	long n;
106 113
 	int err;
... ...
@@ -108,15 +115,30 @@ static int fixup_stat(void** param, int param_no)
108 115
 	s.s = (char*)*param;
109 116
 	s.len = strlen(s.s);
110 117
 	if (param_no==1) {
111
-		/* var name - string */
112
-		stat = get_stat( &s );
113
-		if (stat==0) {
114
-			LM_ERR("fixup_stat: variable <%s> not "
115
-				"defined\n", s.s);
116
-			return E_CFG;
118
+		/* var name - string or pv */
119
+		sopv = (struct stat_or_pv *)pkg_malloc(sizeof(struct stat_or_pv));
120
+		if (sopv==NULL) {
121
+			LM_ERR("no more pkg mem\n");
122
+			return E_OUT_OF_MEM;
123
+		}
124
+		memset( sopv, 0 , sizeof(struct stat_or_pv) );
125
+		/* is it pv? */
126
+		if (s.s[0]=='$') {
127
+			if (fixup_pvar(param)!=0) {
128
+				LM_ERR("invalid pv %.s as parameter\n",s.s);
129
+				return E_CFG;
130
+			}
131
+			sopv->pv = (pv_spec_t*)(*param);
132
+		} else {
133
+			/* it is string */
134
+			sopv->stat = get_stat( &s );
135
+			if (sopv->stat==0) {
136
+				LM_ERR("variable <%s> not defined\n", s.s);
137
+				return E_CFG;
138
+			}
117 139
 		}
118
-		pkg_free(*param);
119
-		*param=(void*)stat;
140
+		pkg_free(s.s);
141
+		*param=(void*)sopv;
120 142
 		return 0;
121 143
 	} else if (param_no==2) {
122 144
 		/* update value - integer */
... ...
@@ -144,16 +166,57 @@ static int fixup_stat(void** param, int param_no)
144 166
 }
145 167
 
146 168
 
147
-static int w_update_stat(struct sip_msg *msg, char *stat, char *n)
169
+static int w_update_stat(struct sip_msg *msg, char *stat_p, char *n)
148 170
 {
149
-	update_stat( (stat_var*)stat, (long)n);
171
+	struct stat_or_pv *sopv = (struct stat_or_pv *)stat_p;
172
+	pv_value_t pv_val;
173
+	stat_var *stat;
174
+
175
+	if (sopv->stat) {
176
+		update_stat( sopv->stat, (long)n);
177
+	} else {
178
+		if (pv_get_spec_value(msg, sopv->pv, &pv_val)!=0 ||
179
+		(pv_val.flags & PV_VAL_STR)==0 ) {
180
+			LM_ERR("failed to get pv string value\n");
181
+			return -1;
182
+		}
183
+		stat = get_stat( &(pv_val.rs) );
184
+		if ( stat == 0 ) {
185
+			LM_ERR("variable <%.*s> not defined\n",
186
+				pv_val.rs.len, pv_val.rs.s);
187
+			return -1;
188
+		}
189
+		update_stat( stat, (long)n);
190
+	}
191
+
150 192
 	return 1;
151 193
 }
152 194
 
153 195
 
154
-static int w_reset_stat(struct sip_msg *msg, char* stat, char *foo)
196
+static int w_reset_stat(struct sip_msg *msg, char* stat_p, char *foo)
155 197
 {
156
-	reset_stat( (stat_var*)stat );
198
+	struct stat_or_pv *sopv = (struct stat_or_pv *)stat_p;
199
+	pv_value_t pv_val;
200
+	stat_var *stat;
201
+
202
+	if (sopv->stat) {
203
+		reset_stat( sopv->stat );
204
+	} else {
205
+		if (pv_get_spec_value(msg, sopv->pv, &pv_val)!=0 ||
206
+		(pv_val.flags & PV_VAL_STR)==0 ) {
207
+			LM_ERR("failed to get pv string value\n");
208
+			return -1;
209
+		}
210
+		stat = get_stat( &(pv_val.rs) );
211
+		if ( stat == 0 ) {
212
+			LM_ERR("variable <%.*s> not defined\n",
213
+				pv_val.rs.len, pv_val.rs.s);
214
+			return -1;
215
+		}
216
+		reset_stat( stat );
217
+	}
218
+
219
+
157 220
 	return 1;
158 221
 }
159 222