From c6fa04234bdb4ad62f0ed0e3f9a619a2ff007e8a Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Fri, 3 May 2013 00:16:39 +0000 Subject: [PATCH] asynchronous download of the article content --- src/ttrss.c | 16 +++++++++++++--- src/ttrss_cache.c | 12 ++++++------ src/ttrss_cache.h | 2 +- src/ttrss_ws.c | 49 ++++++++++++++++++++++++++++++++++--------------- src/ttrss_ws.h | 3 +++ src/ttrss_wsasync.c | 38 +++++++++++++++++++++++++++++++++++++- src/ttrss_wsasync.h | 2 ++ 7 files changed, 96 insertions(+), 26 deletions(-) diff --git a/src/ttrss.c b/src/ttrss.c index 2e50e08..d77007a 100644 --- a/src/ttrss.c +++ b/src/ttrss.c @@ -46,7 +46,7 @@ char *ttrss_get_headline_content(struct headline *h) } else { log_debug("ttrss_get_headline_content: cache miss"); content = ws_get_article_content(h->id); - cache_put(h, content); + cache_put(h->id, content); } return content; @@ -93,6 +93,14 @@ struct headline *ttrss_get_headline(int id) return feeds_get_headline(data, id); } +static void get_article_content_cbk(int id, const char *content) +{ + printf("get_article_content_cbk %d\n", id); + + if (content) + cache_put(id, content); +} + void ttrs_download_headline_content(struct feed **feeds) { struct feed **fcur; @@ -107,8 +115,10 @@ void ttrs_download_headline_content(struct feed **feeds) (*fcur)->title); while (hcur && *hcur) { - if (!cache_exists(*hcur)) - free(ttrss_get_headline_content(*hcur)); + if (!cache_exists(*hcur)) { + ws_async_get_article_content + ((*hcur)->id, get_article_content_cbk); + } hcur++; } } diff --git a/src/ttrss_cache.c b/src/ttrss_cache.c index d13b534..a2af0b0 100644 --- a/src/ttrss_cache.c +++ b/src/ttrss_cache.c @@ -51,13 +51,13 @@ static const char *get_cache_dir() return cache_dir; } -static gchar *content_get_path(const struct headline *h) +static gchar *content_get_path(int id) { const char *cache_dir; cache_dir = get_cache_dir(); if (cache_dir) - return g_strdup_printf("%s/%d", cache_dir, h->id); + return g_strdup_printf("%s/%d", cache_dir, id); return NULL; } @@ -75,11 +75,11 @@ static void file_set_content(const char *path, const char *content) } } -void cache_put(const struct headline *h, const char *content) +void cache_put(int id, const char *content) { char *path; - path = content_get_path(h); + path = content_get_path(id); if (path) { pthread_mutex_lock(&lock); @@ -96,7 +96,7 @@ int cache_exists(const struct headline *h) char *path; int result; - path = content_get_path(h); + path = content_get_path(h->id); pthread_mutex_lock(&lock); if (stat(path, &s) == -1) @@ -115,7 +115,7 @@ char *cache_get(const struct headline *h) char *content, *path; - path = content_get_path(h); + path = content_get_path(h->id); if (path) { pthread_mutex_lock(&lock); content = file_get_content(path); diff --git a/src/ttrss_cache.h b/src/ttrss_cache.h index caf4ca5..c63ad2d 100644 --- a/src/ttrss_cache.h +++ b/src/ttrss_cache.h @@ -24,6 +24,6 @@ int cache_exists(const struct headline *h); char *cache_get(const struct headline *h); -void cache_put(const struct headline *h, const char *content); +void cache_put(int id, const char *content); #endif diff --git a/src/ttrss_ws.c b/src/ttrss_ws.c index 2c0da73..e9680f4 100644 --- a/src/ttrss_ws.c +++ b/src/ttrss_ws.c @@ -259,32 +259,56 @@ int ws_open_session() return result; } -char *ws_get_article_content(int id) +struct json_object *ws_request_new_get_article_content(int id) { - struct json_object *rp, *rq, *content, *item; - char *str; + struct json_object *rq; rq = ws_request_new("getArticle"); ws_request_add_att_int(rq, "article_id", id); - rp = ws_execute(rq); - - json_object_put(rq); + return rq; +} - str = NULL; +const char *ws_reply_get_article_content(struct json_object *rp) +{ + struct json_object *item, *content; if (rp) { item = json_object_array_get_idx(rp, 0); if (item) { content = json_object_object_get(item, "content"); - str = strdup(json_object_get_string(content)); + return json_object_get_string(content); } + } - json_object_put(rp); + return NULL; +} + +char *ws_get_article_content(int id) +{ + struct json_object *rp, *rq; + const char *content; + char *str; + + rq = ws_request_new("getArticle"); + ws_request_add_att_int(rq, "article_id", id); + + rp = ws_execute(rq); + + json_object_put(rq); + + if (rp) { + content = ws_reply_get_article_content(rp); + + if (content) { + str = strdup(content); + json_object_put(rp); + return str; + } } - return str; + return NULL; } int ws_update_headlines(struct feed *feed) @@ -296,7 +320,6 @@ int ws_update_headlines(struct feed *feed) rq = ws_request_new("getHeadlines"); ws_request_add_att_int(rq, "feed_id", feed->id); - ws_request_add_att_bool(rq, "show_excerpt", 1); rp = ws_execute(rq); @@ -321,10 +344,6 @@ int ws_update_headlines(struct feed *feed) h = headline_new(hid, url, title); j = json_object_object_get(jheadline, - "excerpt"); - h->excerpt = strdup(json_object_get_string(j)); - - j = json_object_object_get(jheadline, "updated"); h->date = json_object_get_int(j); diff --git a/src/ttrss_ws.h b/src/ttrss_ws.h index cd09241..264bbfd 100644 --- a/src/ttrss_ws.h +++ b/src/ttrss_ws.h @@ -33,6 +33,9 @@ struct feed **ws_update_feeds(struct feed **feeds); int ws_update_headlines(struct feed *feed); char *ws_get_article_content(int id); void ws_set_article_unread(int id, int unread); + struct json_object *ws_request_new_set_article_unread(int id, int unread); +struct json_object *ws_request_new_get_article_content(int id); +const char *ws_reply_get_article_content(struct json_object *); #endif diff --git a/src/ttrss_wsasync.c b/src/ttrss_wsasync.c index f3c3d84..35e42e7 100644 --- a/src/ttrss_wsasync.c +++ b/src/ttrss_wsasync.c @@ -17,6 +17,7 @@ * 02110-1301 USA */ +#include #include #include @@ -81,7 +82,7 @@ static void *loop() json_object_put(rp); cur++; - usleep(100000); + usleep(500000); } free(tmp); } @@ -134,3 +135,38 @@ void ws_async_set_article_unread(int id, int unread) log_debug("ws_async_set_article_unread(%d,%d) done", id, unread); } + +struct cbk_data { + int id; + void (*callback)(int id, const char *); +}; + +void async_article_content_cbk(struct json_object *rp, void *userdata) +{ + struct cbk_data *data; + + data = userdata; + + log_debug("async_article_content_cbk %d\n", data->id); + + if (data->callback) + data->callback(data->id, ws_reply_get_article_content(rp)); + + free(data); +} + + +void +ws_async_get_article_content(int id, void (*callback)(int id, const char *)) +{ + struct json_object *rq; + struct cbk_data *data; + + rq = ws_request_new_get_article_content(id); + + data = malloc(sizeof(struct cbk_data)); + data->id = id; + data->callback = callback; + + add_async_request(rq, async_article_content_cbk, data); +} diff --git a/src/ttrss_wsasync.h b/src/ttrss_wsasync.h index 40835c7..ef6604e 100644 --- a/src/ttrss_wsasync.h +++ b/src/ttrss_wsasync.h @@ -23,5 +23,7 @@ #include "ttrss_ws.h" void ws_async_set_article_unread(int id, int unread); +void +ws_async_get_article_content(int id, void (*callback)(int id, const char *)); #endif -- 2.7.4