执行卷曲只有最后卷曲超过10秒前

问题描述:

我有一个智能电表守护这个过程登录天然气消费量的计数器:执行卷曲只有最后卷曲超过10秒前

void http_post(const char *vzuuid) { 

sprintf(url, "http://%s:%d/%s/data/%s.json?ts=%llu", vzserver, vzport, vzpath, vzuuid, unixtime()); 

CURL *curl; 
CURLcode curl_res; 

curl_global_init(CURL_GLOBAL_ALL); 

curl = curl_easy_init(); 

if(curl) 
{ 
    FILE* devnull = NULL; 
    devnull = fopen("/dev/null", "w+"); 

    curl_easy_setopt(curl, CURLOPT_USERAGENT, DAEMON_NAME " " DAEMON_VERSION); 
    curl_easy_setopt(curl, CURLOPT_URL, url); 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, ""); 

    curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull); 

     if((curl_res = curl_easy_perform(curl)) != CURLE_OK) { 
     syslog(LOG_INFO, "HTTP_POST(): %s", curl_easy_strerror(curl_res)); 
     } 

    curl_easy_cleanup(curl); 
    fclose (devnull); 

} 

curl_global_cleanup(); 
} 

我想执行这个只有在最后一次通话超过10s前。我想到了一个全局变量last_time来记住最后一个时间戳,并将它与实际的时间戳进行比较,然后在if ... then构造所有卷曲的东西。为此,直接使用unixtime()应该在另一个变量current_time中进行缓冲,以便与last_time进行比较。

有人可以帮我吗?我不习惯使用C ...

谢谢!

你的方法很好。你可以做这样的事情:

#include <time.h> 

void http_post(const char *vzuuid) 
{ 
    static time_t last_time = 0; 
    time_t cur_time; 

    cur_time = time(NULL); 
    if (cur_time - last_time < 10) 
    return; /* nothing to do */ 

    last_time = cur_time; 

    /* ... */ 
} 

我不使用一个全球性的,但一个静态变量,来保存最后的请求的最后一次。

+0

Xtof谢谢!按设计工作! – 2013-05-04 21:06:54

+0

不客气 - 这并不难,我只是在C语言中翻译了你已经说过的内容=) – 2013-05-05 12:49:08