Contents of /trunk/src/gcvote.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 160 - (hide annotations)
Thu Nov 5 07:00:52 2009 UTC (14 years, 7 months ago) by harbaum
File MIME type: text/plain
File size: 11050 byte(s)
GCVote disk cache
1 harbaum 157 /*
2     * This file is part of GPXView.
3     *
4     * GPXView is free software: you can redistribute it and/or modify
5     * it under the terms of the GNU General Public License as published by
6     * the Free Software Foundation, either version 3 of the License, or
7     * (at your option) any later version.
8     *
9     * GPXView is distributed in the hope that it will be useful,
10     * but WITHOUT ANY WARRANTY; without even the implied warranty of
11     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     * GNU General Public License for more details.
13     *
14     * You should have received a copy of the GNU General Public License
15     * along with GPXView. If not, see <http://www.gnu.org/licenses/>.
16     */
17 harbaum 152
18 harbaum 157 #include <curl/curl.h>
19     #include <curl/types.h>
20     #include <curl/easy.h>
21    
22     #include <libxml/parser.h>
23     #include <libxml/tree.h>
24    
25 harbaum 159 #include <glib/gstdio.h>
26    
27     #include <fcntl.h>
28    
29 harbaum 157 #ifndef LIBXML_TREE_ENABLED
30     #error "Tree not enabled in libxml"
31     #endif
32    
33     #include "gpxview.h"
34    
35     #define ID_PATTERN "guid="
36    
37     static size_t mem_write(void *ptr, size_t size, size_t nmemb,
38     void *stream) {
39     curl_mem_t *p = (curl_mem_t*)stream;
40    
41     p->ptr = g_realloc(p->ptr, p->len + size*nmemb + 1);
42     if(p->ptr) {
43     memcpy(p->ptr+p->len, ptr, size*nmemb);
44     p->len += size*nmemb;
45     p->ptr[p->len] = 0;
46     }
47     return nmemb;
48     }
49    
50 harbaum 158 /* return list of all votes in this file */
51     static gcvote_t *votes_parse(xmlDocPtr doc, xmlNode *a_node) {
52     xmlNode *cur_node = NULL;
53 harbaum 157
54 harbaum 158 gcvote_t *votes = NULL, **cur = &votes;
55 harbaum 157
56 harbaum 158 for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
57     if (cur_node->type == XML_ELEMENT_NODE) {
58 harbaum 157
59 harbaum 158 if(strcasecmp((char*)cur_node->name, "vote") == 0) {
60     *cur = g_new0(gcvote_t, 1);
61     (*cur)->quality = GCVOTE_NONE;
62 harbaum 157
63 harbaum 158 /* unhandled attributes: */
64     /* userName, voteAvg, voteUser, waypoint, vote1-5 */
65 harbaum 157
66 harbaum 158 /* parse votes attributes */
67     char *prop_str = (char*)xmlGetProp(cur_node, BAD_CAST "voteMedian");
68     if(prop_str) {
69     (*cur)->quality = atoi(prop_str);
70     xmlFree(prop_str);
71     }
72 harbaum 157
73 harbaum 158 prop_str = (char*)xmlGetProp(cur_node, BAD_CAST "voteCnt");
74     if(prop_str) {
75     (*cur)->votes = atoi(prop_str);
76     xmlFree(prop_str);
77     }
78 harbaum 157
79 harbaum 158 prop_str = (char*)xmlGetProp(cur_node, BAD_CAST "cacheId");
80     if(prop_str) {
81     (*cur)->id = g_strdup(prop_str);
82     xmlFree(prop_str);
83     }
84 harbaum 157
85 harbaum 158 cur = &(*cur)->next;
86     } else if(strcasecmp((char*)cur_node->name, "errorstring") == 0) {
87     /* ignore for now ... */
88     } else
89     printf("found unhandled votes/%s\n", cur_node->name);
90 harbaum 157 }
91 harbaum 158 }
92     return votes;
93 harbaum 157 }
94    
95     /* parse root element */
96 harbaum 158 static gcvote_t *parse_root(xmlDocPtr doc, xmlNode *a_node) {
97 harbaum 157 xmlNode *cur_node = NULL;
98 harbaum 158 gcvote_t *votes = NULL;
99 harbaum 157
100     for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
101     if (cur_node->type == XML_ELEMENT_NODE) {
102    
103 harbaum 158 if(strcasecmp((char*)cur_node->name, "votes") == 0) {
104     if(!votes)
105     votes = votes_parse(doc, cur_node);
106     else
107     printf("gcvote: ignoring multiple votes lists\n");
108 harbaum 157 } else
109     printf("found unhandled %s\n", cur_node->name);
110     }
111     }
112 harbaum 158 return votes;
113 harbaum 157 }
114    
115 harbaum 158 static gcvote_t *parse_doc(xmlDocPtr doc) {
116 harbaum 157 /* Get the root element node */
117     xmlNode *root_element = xmlDocGetRootElement(doc);
118 harbaum 158 gcvote_t *votes = parse_root(doc, root_element);
119 harbaum 157
120     /*free the document */
121     xmlFreeDoc(doc);
122    
123     /*
124     * Free the global variables that may
125     * have been allocated by the parser.
126     */
127     xmlCleanupParser();
128 harbaum 158
129     return votes;
130 harbaum 157 }
131    
132 harbaum 158 static void curl_set_proxy(CURL *curl, proxy_t *proxy) {
133     if(proxy) {
134     if(proxy->ignore_hosts)
135     printf("WARNING: Proxy \"ignore_hosts\" unsupported!\n");
136 harbaum 157
137 harbaum 158 printf("gcvote: using proxy %s:%d\n", proxy->host, proxy->port);
138 harbaum 157
139 harbaum 158 curl_easy_setopt(curl, CURLOPT_PROXY, proxy->host);
140     curl_easy_setopt(curl, CURLOPT_PROXYPORT, proxy->port);
141 harbaum 157
142 harbaum 158 if(proxy->use_authentication) {
143     printf("gcvote: use auth for user %s\n", proxy->authentication_user);
144 harbaum 157
145 harbaum 158 char *cred = g_strdup_printf("%s:%s",
146     proxy->authentication_user,
147     proxy->authentication_password);
148 harbaum 157
149 harbaum 158 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, cred);
150     g_free(cred);
151     }
152     }
153     }
154 harbaum 157
155 harbaum 158 void gcvote_request_free(gcvote_request_t *request) {
156     /* decrease refcount and only free structure if no references are left */
157     request->refcount--;
158     if(request->refcount) {
159     printf("gcvote: still %d references, keeping request\n",
160     request->refcount);
161     return;
162     }
163    
164     printf("gcvote: no references left, freeing request\n");
165     if(request->url) g_free(request->url);
166    
167 harbaum 159 if(request->mem.ptr)
168     g_free(request->mem.ptr);
169    
170 harbaum 158 g_free(request);
171     }
172    
173     /* gcvote net result handler */
174     static gboolean gcvote_result_handler(gpointer data) {
175     gcvote_request_t *request = (gcvote_request_t*)data;
176    
177     printf("gcvote: result handler\n");
178    
179     /* worker thread has already reduced its refcounter */
180     if(request->refcount < 1) {
181     printf("gcvote: main app isn't listening anymore\n");
182     return FALSE;
183     }
184    
185     if(request->res) {
186     printf("gcvote: curl failed\n");
187     request->cb(NULL, request->userdata);
188     return FALSE;
189     }
190    
191     /* parse the reply */
192     /* nothing could be parsed, just give up */
193     if(!request->mem.ptr || !request->mem.len) {
194     printf("gcvote: ignoring zero length reply\n");
195     request->cb(NULL, request->userdata);
196     return FALSE;
197     }
198    
199     /* start XML parser */
200     LIBXML_TEST_VERSION;
201    
202     /* parse the file and get the DOM */
203     xmlDoc *doc = xmlReadMemory(request->mem.ptr, request->mem.len,
204     NULL, NULL, 0);
205    
206     /* nothing could be parsed, just give up */
207     if(!doc) {
208     request->cb(NULL, request->userdata);
209     return FALSE;
210     }
211    
212     vote_t *vote = NULL;
213     gcvote_t *votes = parse_doc(doc);
214    
215     /* search for requested cache in reply and free chain */
216     while(votes) {
217     gcvote_t *next = votes->next;
218    
219     if(!vote && !strcmp(votes->id, request->id) && (votes->quality > 0)) {
220    
221     vote = g_new0(vote_t, 1);
222     vote->quality = votes->quality;
223     vote->votes = votes->votes;
224     }
225    
226     if(votes->id) g_free(votes->id);
227     g_free(votes);
228     votes = next;
229     }
230    
231     if(vote) {
232     printf("gcvote: worker found vote %d/%d\n", vote->quality, vote->votes);
233     request->cb(vote, request->userdata);
234     } else
235     printf("gcvote: no vote found\n");
236    
237     return FALSE;
238     }
239    
240 harbaum 160 static void gcvotes_free(gcvote_t *votes) {
241     while(votes) {
242     gcvote_t *next = votes->next;
243     if(votes->id) g_free(votes->id);
244     g_free(votes);
245     votes = next;
246     }
247     }
248    
249 harbaum 159 void gcvote_save(appdata_t *appdata, cache_t *cache, curl_mem_t *mem) {
250     if(!mem->len || !mem->ptr) return;
251    
252     /* save data to disk */
253     char *filename = g_strdup_printf("%s%s/gcvote.xml",
254     appdata->image_path,
255     cache->id);
256     if(checkdir(filename) != 0)
257     printf("gcvote: unable to create file path\n");
258     else {
259     printf("gcvote: write %d bytes to %s\n", mem->len, filename);
260    
261     int handle = g_open(filename, O_WRONLY | O_CREAT, 0644);
262     if(handle >= 0) {
263     int len = write(handle, mem->ptr, mem->len);
264     close(handle);
265    
266     /* if write failed, then remove the file */
267     if(len != mem->len)
268     g_remove(filename);
269     }
270     }
271    
272     free(filename);
273     }
274    
275 harbaum 158 static void *worker_thread(void *ptr) {
276     gcvote_request_t *request = (gcvote_request_t*)ptr;
277     struct curl_httppost *formpost=NULL;
278     struct curl_httppost *lastptr=NULL;
279    
280     printf("gcvote: worker thread running\n");
281    
282     request->mem.ptr = NULL;
283     request->mem.len = 0;
284    
285     printf("gcvote: request to get votes for id %s\n", request->id);
286    
287 harbaum 157 curl_formadd(&formpost, &lastptr,
288 harbaum 158 CURLFORM_COPYNAME, "version",
289     CURLFORM_COPYCONTENTS, APP VERSION,
290     CURLFORM_END);
291 harbaum 157
292     curl_formadd(&formpost, &lastptr,
293 harbaum 158 CURLFORM_COPYNAME, "cacheIds",
294     CURLFORM_COPYCONTENTS, request->id,
295     CURLFORM_END);
296    
297     /* try to init curl */
298     CURL *curl = curl_easy_init();
299     if(!curl) {
300     curl_formfree(formpost);
301     gcvote_request_free(request);
302 harbaum 157
303 harbaum 158 /* callback anyway, so main loop can also clean up */
304     g_idle_add(gcvote_result_handler, request);
305    
306     return NULL;
307     }
308    
309     curl_set_proxy(curl, request->proxy);
310    
311 harbaum 157 /* play nice and report some user agent */
312     curl_easy_setopt(curl, CURLOPT_USERAGENT, APP " " VERSION);
313 harbaum 158
314 harbaum 157 /* download to memory */
315 harbaum 158 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &request->mem);
316 harbaum 157 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, mem_write);
317    
318     /* what URL that receives this POST */
319 harbaum 158 curl_easy_setopt(curl, CURLOPT_URL,
320     "http://dosensuche.de/GCVote/getVotes.php");
321 harbaum 157 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
322    
323 harbaum 158 request->res = curl_easy_perform(curl);
324     printf("gcvote: curl perform returned with %d\n", request->res);
325    
326     // printf("received %d bytes\n", mem.len);
327     // printf("data: %s\n", mem.ptr);
328    
329 harbaum 157 /* always cleanup */
330     curl_easy_cleanup(curl);
331    
332 harbaum 158 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &request->response);
333    
334 harbaum 157 /* then cleanup the formpost chain */
335     curl_formfree(formpost);
336 harbaum 160
337 harbaum 158 printf("gcvote: worker thread done\n");
338     gcvote_request_free(request);
339 harbaum 159
340 harbaum 158 /* cause gtk main loop to handle result */
341     g_idle_add(gcvote_result_handler, request);
342    
343     printf("gcvote: thread terminating\n");
344    
345     return NULL;
346     }
347 harbaum 157
348 harbaum 158 gcvote_request_t *gcvote_request(appdata_t *appdata, gcvote_cb cb,
349     char *url, gpointer data) {
350     if(!url) return NULL;
351    
352     /* ------ prepare request for worker thread --------- */
353     gcvote_request_t *request = g_new0(gcvote_request_t, 1);
354     request->url = g_strdup(url);
355     request->id = strstr(request->url, ID_PATTERN);
356     if(!request->id) {
357     printf("gcvote: unable to find id\n");
358     gcvote_request_free(request);
359     return NULL;
360     }
361    
362     request->id += strlen(ID_PATTERN);
363     request->refcount = 2; // master and worker hold a reference
364     request->cb = cb;
365     request->userdata = data;
366 harbaum 157
367 harbaum 158 if(!g_thread_create(&worker_thread, request, FALSE, NULL) != 0) {
368     g_warning("gcvote: failed to create the worker thread");
369    
370     /* free request and return error */
371     request->refcount--; /* decrease by one for dead worker thread */
372     gcvote_request_free(request);
373     return NULL;
374 harbaum 157 }
375    
376 harbaum 158 return request;
377     }
378 harbaum 157
379 harbaum 160 vote_t *gcvote_restore(appdata_t *appdata, cache_t *cache) {
380     /* load data from disk */
381     char *filename = g_strdup_printf("%s%s/gcvote.xml",
382     appdata->image_path,
383     cache->id);
384    
385     printf("gcvote: trying to restore from %s\n", filename);
386    
387     /* no such file? */
388     if(!g_file_test(filename, G_FILE_TEST_EXISTS)) {
389     printf("gcvote: no such file\n");
390     free(filename);
391     return NULL;
392     }
393    
394     LIBXML_TEST_VERSION;
395     xmlDoc *doc = xmlReadFile(filename, NULL, 0);
396    
397     if(doc == NULL) {
398     printf("gcvote: error, could not parse file %s\n", filename);
399     free(filename);
400     return NULL;
401     }
402    
403     free(filename);
404    
405     /* in this case this will sure only return one result */
406     gcvote_t *votes = parse_doc(doc);
407    
408     if(!votes) {
409     printf("gcvote: error, no vote found\n");
410     free(filename);
411     return NULL;
412     }
413    
414     vote_t *vote = g_new0(vote_t, 1);
415     vote->quality = votes->quality;
416     vote->votes = votes->votes;
417    
418     printf("gcvote: found vote %d/%d\n", vote->quality, vote->votes);
419    
420     gcvotes_free(votes);
421    
422     return vote;
423     }