c - Perform CURL only if last CURL is more than 10s ago -


i have procedure in smartmeter daemon log counter of gas consumption:

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(); } 

i want execute if last call more 10s ago. thought of global variable last_time remember last timestamp , compare actual timestamp in if ... construct around curl stuff. direct use of unixtime() should buffered in variable current_time used compared last_time.

can me please? not used use c...

thank you!

your approach good. can this:

#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 */    last_time = cur_time;    /* ... */ } 

i not use global, static variable, hold last time of last request.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -