cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Progress function trigger time

From: Rob Ward <rob_at_rob-ward.co.uk>
Date: Wed, 30 Nov 2011 11:52:45 +0000

On 30 November 2011 11:04, Saidus Bounderra <bsaidus_at_gmail.com> wrote:

> Hi !! excuse me [?] for my confusion but if you can be more specific
> telling me
> where to put the timestamp & in which way !!
> thanks you
>
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html
>

I think Daniel is referring to a mechanism such as the following(note this
is a hacky implementation and needs some cleaning up, you may also want to
use another source for the time etc but is does give a basic idea for how
tit can be implemented so something only happens in the progress function
every so often).

#include <stdio.h>
#include <curl/curl.h>
#include <sys/time.h>

#define PROGRESS_INTERVAL 5
struct timeval lastrun;

static int progress(void *p,
                    double dltotal, double dlnow,
                    double ultotal, double ulnow)
{
  struct timeval tv;
  gettimeofday(&tv,NULL);

  if( (tv.tv_sec - lastrun.tv_sec) >= PROGRESS_INTERVAL)
  {
    gettimeofday(&lastrun,NULL);

    fprintf(stderr, "UP: %g of %g DOWN: %g of %g\r\n",
          ulnow, ultotal, dlnow, dltotal);
  }
  else
  {
    //printf("\nnot doing anything\n");
  }

  return 0;
}

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
  int written = fwrite(ptr, size, nmemb, (FILE *)stream);
  return written;
}

int main(void)
{
  CURL *curl;
  CURLcode res=0;
  static const char *bodyfilename = "body.out";
  FILE *bodyfile;

  curl = curl_easy_init();
  if(curl) {

    bodyfile = fopen(bodyfilename,"w");
    if (bodyfile == NULL) {
      curl_easy_cleanup(curl);
      return -1;
    }
    curl_easy_setopt(curl, CURLOPT_URL, "http://rob-ward.net/testfile1.txt
");
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, bodyfile);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)5000);
    gettimeofday(&lastrun,NULL);

    res = curl_easy_perform(curl);

    if(res)
      fprintf(stderr, "%s\n", curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  fclose(bodyfile);

  return (int)res;
}

-- 
------------------------------
Rob Ward
www.rob-ward.co.uk

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html

32F.png
Received on 2011-11-30