2 * Copyright (C) 2010-2013 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <json/json.h>
31 #include "ttrss_wsasync.h"
33 struct async_request {
35 struct json_object *request;
36 void (*callback)(struct json_object *, void *);
39 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
41 static struct async_request **requests;
43 static pthread_t *thread;
45 static void async_request_free(struct async_request *rq)
49 json_object_put(rq->request);
56 struct json_object *rp;
57 struct async_request **tmp, **cur, *rq;
62 pthread_mutex_lock(&lock);
69 pthread_mutex_unlock(&lock);
75 rp = ws_execute(rq->request);
78 rq->callback(rp, rq->data);
80 async_request_free(rq);
90 log_debug("loop() done");
97 static struct async_request *
98 add_async_request(struct json_object *rq,
99 void (*callback)(struct json_object *, void *data),
102 struct async_request *async_rq;
103 struct async_request **tmp;
105 pthread_mutex_lock(&lock);
107 thread = malloc(sizeof(pthread_t));
108 pthread_create(thread, NULL, loop, NULL);
110 pthread_mutex_unlock(&lock);
112 async_rq = malloc(sizeof(struct async_request));
113 async_rq->request = rq;
114 async_rq->data = data;
115 async_rq->callback = callback;
117 pthread_mutex_lock(&lock);
118 tmp = (struct async_request **)list_add((void **)requests, async_rq);
119 list_free((void **)requests);
121 pthread_mutex_unlock(&lock);
126 void ws_async_set_article_unread(int id, int unread)
128 struct json_object *rq;
130 log_debug("ws_async_set_article_unread(%d,%d)", id, unread);
132 rq = ws_request_new_set_article_unread(id, unread);
134 add_async_request(rq, NULL, NULL);
136 log_debug("ws_async_set_article_unread(%d,%d) done", id, unread);
141 void (*callback)(int id, const char *);
144 void async_article_content_cbk(struct json_object *rp, void *userdata)
146 struct cbk_data *data;
150 log_debug("async_article_content_cbk %d\n", data->id);
153 data->callback(data->id, ws_reply_get_article_content(rp));
160 ws_async_get_article_content(int id, void (*callback)(int id, const char *))
162 struct json_object *rq;
163 struct cbk_data *data;
165 rq = ws_request_new_get_article_content(id);
167 data = malloc(sizeof(struct cbk_data));
169 data->callback = callback;
171 add_async_request(rq, async_article_content_cbk, data);