From 0d343345043a0313aeb452b15c3a2d066ac0a2c4 Mon Sep 17 00:00:00 2001 From: antognolli Date: Fri, 8 Jul 2011 18:06:12 +0000 Subject: [PATCH] ecore/ecore_con - Add simple examples of ecore_con_url usage. git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/ecore@61160 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/examples/Makefile.am | 12 ++- src/examples/ecore_con_url_download_example.c | 110 ++++++++++++++++++++++++++ src/examples/ecore_con_url_headers_example.c | 104 ++++++++++++++++++++++++ 3 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 src/examples/ecore_con_url_download_example.c create mode 100644 src/examples/ecore_con_url_headers_example.c diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am index 6e0f797..a470f4f 100644 --- a/src/examples/Makefile.am +++ b/src/examples/Makefile.am @@ -28,6 +28,8 @@ SRCS = \ ecore_time_functions_example.c \ ecore_job_example.c \ ecore_con_lookup_example.c \ + ecore_con_url_headers_example.c \ + ecore_con_url_download_example.c \ client_bench.c \ server_bench.c \ ecore_con_client_example.c \ @@ -57,9 +59,15 @@ pkglib_PROGRAMS += \ ecore_timer_example \ ecore_time_functions_example \ ecore_pipe_simple_example \ - ecore_con_lookup_example + ecore_con_lookup_example \ + ecore_con_url_headers_example \ + ecore_con_url_download_example -ecore_con_lookup_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la ecore_animator_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_evas/libecore_evas.la +ecore_con_lookup_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la +ecore_con_url_headers_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la +ecore_con_url_download_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la +ecore_con_server_simple_example_LDADD = $(ECOREBASELDADD) $(top_builddir)/src/lib/ecore_con/libecore_con.la + endif diff --git a/src/examples/ecore_con_url_download_example.c b/src/examples/ecore_con_url_download_example.c new file mode 100644 index 0000000..0cb81e1 --- /dev/null +++ b/src/examples/ecore_con_url_download_example.c @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include + +struct _request { + long size; +}; + +static Eina_Bool +_url_progress_cb(void *data, int type, void *event_info) +{ + Ecore_Con_Event_Url_Progress *url_progress = event_info; + float percent; + + if (url_progress->down.total > 0) + { + struct _request *req = ecore_con_url_data_get(url_progress->url_con); + req->size = url_progress->down.now; + + percent = (url_progress->down.now / url_progress->down.total) * 100; + printf("Total of download complete: %0.1f (%0.0f)%%\n", + percent, url_progress->down.now); + } + + return EINA_TRUE; +} + +static Eina_Bool +_url_complete_cb(void *data, int type, void *event_info) +{ + Ecore_Con_Event_Url_Complete *url_complete = event_info; + + struct _request *req = ecore_con_url_data_get(url_complete->url_con); + int nbytes = ecore_con_url_received_bytes_get(url_complete->url_con); + + printf("\n"); + printf("download completed with status code: %d\n", url_complete->status); + printf("Total size of downloaded file: %ld bytes\n", req->size); + printf("Total size of downloaded file: %ld bytes " + "(from received_bytes_get)\n", nbytes); + ecore_main_loop_quit(); + + return EINA_TRUE; +} + +int main(int argc, const char *argv[]) +{ + Ecore_Con_Url *ec_url = NULL; + struct _request *req; + int fd; + const char *filename = "downloadedfile.dat"; + + if (argc < 2) + { + printf("need one parameter: \n"); + return -1; + } + + fd = open(filename, O_CREAT|O_WRONLY|O_TRUNC, 0644); + + if (fd == -1) + { + printf("error: could not open file for writing: \"%s\"\n", + filename); + return -1; + } + + ecore_init(); + ecore_con_init(); + ecore_con_url_init(); + + ec_url = ecore_con_url_new(argv[1]); + if (!ec_url) + { + printf("error when creating ecore con url object.\n"); + goto end; + } + + req = malloc(sizeof(*req)); + req->size = 0; + ecore_con_url_data_set(ec_url, req); + + ecore_con_url_fd_set(ec_url, fd); + + ecore_event_handler_add(ECORE_CON_EVENT_URL_PROGRESS, _url_progress_cb, NULL); + ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_complete_cb, NULL); + + if (!ecore_con_url_get(ec_url)) + { + printf("could not realize request.\n"); + goto free_ec_url; + } + + ecore_main_loop_begin(); + +free_ec_url: + free(req); + ecore_con_url_free(ec_url); +end: + + close(fd); + ecore_con_url_shutdown(); + ecore_con_shutdown(); + ecore_shutdown(); + + return 0; +} diff --git a/src/examples/ecore_con_url_headers_example.c b/src/examples/ecore_con_url_headers_example.c new file mode 100644 index 0000000..262823b --- /dev/null +++ b/src/examples/ecore_con_url_headers_example.c @@ -0,0 +1,104 @@ +#include +#include +#include +#include + +static Eina_Bool +_url_data_cb(void *data, int type, void *event_info) +{ + Ecore_Con_Event_Url_Data *url_data = event_info; + int i; + + for (i = 0; i < url_data->size; i++) + printf("%c", url_data->data[i]); + + return EINA_TRUE; +} + +static Eina_Bool +_url_complete_cb(void *data, int type, void *event_info) +{ + Ecore_Con_Event_Url_Complete *url_complete = event_info; + const Eina_List *headers, *l; + char *str; + + printf("\n"); + printf("download completed with status code: %d\n", url_complete->status); + + headers = ecore_con_url_response_headers_get(url_complete->url_con); + + EINA_LIST_FOREACH(headers, l, str) + printf("header: %s\n", str); + + ecore_main_loop_quit(); + + return EINA_TRUE; +} + +int main(int argc, const char *argv[]) +{ + Ecore_Con_Url *ec_url = NULL; + const char *type; + Eina_Bool r; + + if (argc < 3) + { + printf("need at least two parameters: < POST|GET > \n"); + return -1; + } + + type = argv[1]; + + if (strcmp(type, "POST") && (strcmp(type, "GET"))) + { + printf("only POST or GET are supported by this example.\n"); + return -1; + } + + ecore_init(); + ecore_con_init(); + ecore_con_url_init(); + + // check if requests are being pipelined, and set them if not: + if (!ecore_con_url_pipeline_get()) + ecore_con_url_pipeline_set(EINA_TRUE); + + ec_url = ecore_con_url_custom_new(argv[2], type); + if (!ec_url) + { + printf("error when creating ecore con url object.\n"); + goto end; + } + + ecore_event_handler_add(ECORE_CON_EVENT_URL_DATA, _url_data_cb, NULL); + ecore_event_handler_add(ECORE_CON_EVENT_URL_COMPLETE, _url_complete_cb, NULL); + + ecore_con_url_additional_header_add(ec_url, "User-Agent", "blablabla"); + ecore_con_url_verbose_set(ec_url, EINA_TRUE); + + ecore_con_url_httpauth_set(ec_url, "user", "password", EINA_FALSE); + + ecore_con_url_time(ec_url, ECORE_CON_URL_TIME_IFMODSINCE, 0); + + if (!strcmp(type, "GET")) + r = ecore_con_url_get(ec_url); + else + r = ecore_con_url_post(ec_url, NULL, 0, NULL); + + if (!r) + { + printf("could not realize request.\n"); + goto free_ec_url; + } + + ecore_main_loop_begin(); + +free_ec_url: + ecore_con_url_free(ec_url); +end: + ecore_con_url_shutdown(); + ecore_con_shutdown(); + ecore_shutdown(); + + return 0; +} -- 2.7.4