[sr-dev] git:master: pv: time PVs use the value from sip_msg_t structure

Daniel-Constantin Mierla miconda at gmail.com
Wed Apr 18 20:03:08 CEST 2012


Module: sip-router
Branch: master
Commit: 9654c70212db15f77beedbb874f3e153fdeaf41b
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=9654c70212db15f77beedbb874f3e153fdeaf41b

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Wed Apr 18 19:52:22 2012 +0200

pv: time PVs use the value from sip_msg_t structure

- it solves the time of the message coherence, by returning same value,
  no matter where is used
- reported by Klaus Darilion

---

 modules_k/pv/pv_time.c |   82 ++++++++++--------------------------------------
 1 files changed, 17 insertions(+), 65 deletions(-)

diff --git a/modules_k/pv/pv_time.c b/modules_k/pv/pv_time.c
index 9cbc375..80e6b31 100644
--- a/modules_k/pv/pv_time.c
+++ b/modules_k/pv/pv_time.c
@@ -35,26 +35,6 @@
 
 #include "pv_time.h"
 
-static msg_ctx_id_t _pv_time_msgid = { 0 };
-static time_t _pv_msg_tm = 0;
-
-static int pv_update_time(sip_msg_t *msg, time_t *t)
-{
-	if(msg_ctx_id_match(msg, &_pv_time_msgid)!=1 || _pv_msg_tm==0)
-	{
-		_pv_msg_tm = time(NULL);
-		msg_ctx_id_set(msg, &_pv_time_msgid);
-		
-		if(t!=NULL)
-			*t=_pv_msg_tm;
-
-		return 1;
-	}
-	if(t!=NULL)
-		*t=_pv_msg_tm;
-	return 0;
-}
-
 int pv_parse_time_name(pv_spec_p sp, str *in)
 {
 	if(sp==NULL || in==NULL || in->len<=0)
@@ -130,16 +110,14 @@ static msg_ctx_id_t _cfgutils_msgid = { 0 };
 int pv_get_time(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res)
 {
-	time_t t;
-
 	if(msg==NULL || param==NULL)
 		return -1;
 
 	if(msg_ctx_id_match(msg, &_cfgutils_msgid)!=1)
 	{
-		pv_update_time(msg, &t);
+		msg_set_time(msg);
 		msg_ctx_id_set(msg, &_cfgutils_msgid);
-		if(localtime_r(&t, &_cfgutils_ts) == NULL)
+		if(localtime_r(&msg->tval.tv_sec, &_cfgutils_ts) == NULL)
 		{
 			LM_ERR("unable to break time to attributes\n");
 			return -1;
@@ -179,7 +157,6 @@ int pv_get_time(struct sip_msg *msg, pv_param_t *param,
 int pv_get_strftime(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res)
 {
-	time_t t;
 	str s;
 #define PV_STRFTIME_BUF_SIZE	64
 	static char _pv_strftime_buf[PV_STRFTIME_BUF_SIZE];
@@ -189,9 +166,9 @@ int pv_get_strftime(struct sip_msg *msg, pv_param_t *param,
 
 	if(msg_ctx_id_match(msg, &_cfgutils_msgid)!=1)
 	{
-		pv_update_time(msg, &t);
+		msg_set_time(msg);
 		msg_ctx_id_set(msg, &_cfgutils_msgid);
-		if(localtime_r(&t, &_cfgutils_ts) == NULL)
+		if(localtime_r(&msg->tval.tv_sec, &_cfgutils_ts) == NULL)
 		{
 			LM_ERR("unable to break time to attributes\n");
 			return -1;
@@ -230,13 +207,11 @@ int pv_get_timenowf(struct sip_msg *msg, pv_param_t *param,
 int pv_get_times(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res)
 {
-	time_t t;
 	if(msg==NULL)
 		return -1;
 
-	pv_update_time(msg, &t);
-
-	return pv_get_uintval(msg, param, res, (unsigned int)t);
+	msg_set_time(msg);
+	return pv_get_uintval(msg, param, res, (unsigned int)msg->tval.tv_sec);
 }
 
 
@@ -244,16 +219,15 @@ int pv_get_timef(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res)
 {
 	str s;
-	time_t t;
 	
 	if(msg==NULL)
 		return -1;
 
-	pv_update_time(msg, &t);
+	msg_set_time(msg);
 	
-	s.s = ctime(&t);
+	s.s = ctime(&msg->tval.tv_sec);
 	s.len = strlen(s.s)-1;
-	return pv_get_strintval(msg, param, res, &s, (int)t);
+	return pv_get_strintval(msg, param, res, &s, (int)msg->tval.tv_sec);
 }
 
 int pv_get_timeb(struct sip_msg *msg, pv_param_t *param,
@@ -264,8 +238,7 @@ int pv_get_timeb(struct sip_msg *msg, pv_param_t *param,
 	return pv_get_uintval(msg, param, res, (unsigned int)up_since);
 }
 
-static struct timeval _timeval_ts;
-static msg_ctx_id_t _timeval_msgid = { 0 };
+static struct timeval _timeval_ts = {0};
 static char _timeval_ts_buf[32];
 
 int pv_get_timeval(struct sip_msg *msg, pv_param_t *param,
@@ -280,30 +253,17 @@ int pv_get_timeval(struct sip_msg *msg, pv_param_t *param,
 	switch(param->pvn.u.isname.name.n)
 	{
 		case 1:
-			if(msg_ctx_id_match(msg, &_timeval_msgid)!=1)
-			{
-				if(gettimeofday(&_timeval_ts, NULL)!=0)
-				{
-					LM_ERR("unable to get time val attributes\n");
-					return -1;
-				}
-				msg_ctx_id_set(msg, &_timeval_msgid);
-			}
-			return pv_get_uintval(msg, param, res, (unsigned int)_timeval_ts.tv_usec);
+			msg_set_time(msg);
+			return pv_get_uintval(msg, param, res, (unsigned int)msg->tval.tv_usec);
 		case 2:
-			if(gettimeofday(&tv, NULL)!=0)
+			if(gettimeofday(&_timeval_ts, NULL)!=0)
 			{
 				LM_ERR("unable to get time val attributes\n");
 				return pv_get_null(msg, param, res);
 			}
-			return pv_get_uintval(msg, param, res, (unsigned int)tv.tv_sec);
+			return pv_get_uintval(msg, param, res, (unsigned int)_timeval_ts.tv_sec);
 		case 3:
-			if(gettimeofday(&tv, NULL)!=0)
-			{
-				LM_ERR("unable to get time val attributes\n");
-				return pv_get_null(msg, param, res);
-			}
-			return pv_get_uintval(msg, param, res, (unsigned int)tv.tv_usec);
+			return pv_get_uintval(msg, param, res, (unsigned int)_timeval_ts.tv_usec);
 		case 4:
 			if(gettimeofday(&tv, NULL)!=0)
 			{
@@ -317,16 +277,8 @@ int pv_get_timeval(struct sip_msg *msg, pv_param_t *param,
 			s.s = _timeval_ts_buf;
 			return pv_get_strval(msg, param, res, &s);
 		default:
-			if(msg_ctx_id_match(msg, &_timeval_msgid)!=1)
-			{
-				if(gettimeofday(&_timeval_ts, NULL)!=0)
-				{
-					LM_ERR("unable to get time val attributes\n");
-					return -1;
-				}
-				msg_ctx_id_set(msg, &_timeval_msgid);
-			}
-			return pv_get_uintval(msg, param, res, (unsigned int)_timeval_ts.tv_sec);
+			msg_set_time(msg);
+			return pv_get_uintval(msg, param, res, (unsigned int)msg->tval.tv_sec);
 	}
 }
 




More information about the sr-dev mailing list