got rid of deprecated create_iterator_locked()
[strongswan.git] / src / libstrongswan / fetcher / fetcher_manager.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #define _GNU_SOURCE
19 #include <pthread.h>
20
21 #include "fetcher_manager.h"
22
23 #include <debug.h>
24 #include <utils/linked_list.h>
25 #include <utils/mutex.h>
26
27 typedef struct private_fetcher_manager_t private_fetcher_manager_t;
28
29 /**
30 * private data of fetcher_manager
31 */
32 struct private_fetcher_manager_t {
33
34 /**
35 * public functions
36 */
37 fetcher_manager_t public;
38
39 /**
40 * list of registered fetchers, as entry_t
41 */
42 linked_list_t *fetchers;
43
44 /**
45 * read write lock to list
46 */
47 pthread_rwlock_t lock;
48 };
49
50 typedef struct {
51 /** assocaited fetcher construction function */
52 fetcher_constructor_t create;
53 /** URL this fetcher support */
54 char *url;
55 } entry_t;
56
57 /**
58 * destroy an entry_t
59 */
60 static void entry_destroy(entry_t *entry)
61 {
62 free(entry->url);
63 free(entry);
64 }
65
66 /**
67 * Implementation of fetcher_manager_t.fetch.
68 */
69 static status_t fetch(private_fetcher_manager_t *this,
70 char *url, chunk_t *response, ...)
71 {
72 enumerator_t *enumerator;
73 status_t status = NOT_SUPPORTED;
74 entry_t *entry;
75 bool capable = FALSE;
76
77 pthread_rwlock_rdlock(&this->lock);
78 enumerator = this->fetchers->create_enumerator(this->fetchers);
79 while (enumerator->enumerate(enumerator, &entry))
80 {
81 fetcher_option_t opt;
82 fetcher_t *fetcher;
83 bool good = TRUE;
84 va_list args;
85
86 /* check URL support of fetcher */
87 if (strncasecmp(entry->url, url, strlen(entry->url)))
88 {
89 continue;
90 }
91 /* create fetcher instance and set options */
92 fetcher = entry->create();
93 if (!fetcher)
94 {
95 continue;
96 }
97 va_start(args, response);
98 while (good)
99 {
100 opt = va_arg(args, fetcher_option_t);
101 switch (opt)
102 {
103 case FETCH_REQUEST_DATA:
104 good = fetcher->set_option(fetcher, opt, va_arg(args, chunk_t));
105 continue;
106 case FETCH_REQUEST_TYPE:
107 good = fetcher->set_option(fetcher, opt, va_arg(args, char*));
108 continue;
109 case FETCH_TIMEOUT:
110 good = fetcher->set_option(fetcher, opt, va_arg(args, u_int));
111 continue;
112 case FETCH_END:
113 break;;
114 }
115 break;
116 }
117 va_end(args);
118 if (!good)
119 { /* fetcher does not support supplied options, try another */
120 fetcher->destroy(fetcher);
121 continue;
122 }
123
124 status = fetcher->fetch(fetcher, url, response);
125 fetcher->destroy(fetcher);
126 /* try another fetcher only if this one does not support that URL */
127 if (status == NOT_SUPPORTED)
128 {
129 continue;
130 }
131 capable = TRUE;
132 break;
133 }
134 enumerator->destroy(enumerator);
135 pthread_rwlock_unlock(&this->lock);
136 if (!capable)
137 {
138 DBG1("unable to fetch from %s, no capable fetcher found", url);
139 }
140 return status;
141 }
142
143 /**
144 * Implementation of fetcher_manager_t.add_fetcher.
145 */
146 static void add_fetcher(private_fetcher_manager_t *this,
147 fetcher_constructor_t create, char *url)
148 {
149 entry_t *entry = malloc_thing(entry_t);
150
151 entry->url = strdup(url);
152 entry->create = create;
153
154 pthread_rwlock_wrlock(&this->lock);
155 this->fetchers->insert_last(this->fetchers, entry);
156 pthread_rwlock_unlock(&this->lock);
157 }
158
159 /**
160 * Implementation of fetcher_manager_t.remove_fetcher.
161 */
162 static void remove_fetcher(private_fetcher_manager_t *this,
163 fetcher_constructor_t create)
164 {
165 enumerator_t *enumerator;
166 entry_t *entry;
167
168 pthread_rwlock_wrlock(&this->lock);
169 enumerator = this->fetchers->create_enumerator(this->fetchers);
170 while (enumerator->enumerate(enumerator, &entry))
171 {
172 if (entry->create == create)
173 {
174 this->fetchers->remove_at(this->fetchers, enumerator);
175 entry_destroy(entry);
176 }
177 }
178 enumerator->destroy(enumerator);
179 pthread_rwlock_unlock(&this->lock);
180 }
181
182 /**
183 * Implementation of fetcher_manager_t.destroy
184 */
185 static void destroy(private_fetcher_manager_t *this)
186 {
187 this->fetchers->destroy_function(this->fetchers, (void*)entry_destroy);
188 pthread_rwlock_destroy(&this->lock);
189 free(this);
190 }
191
192 /*
193 * see header file
194 */
195 fetcher_manager_t *fetcher_manager_create()
196 {
197 private_fetcher_manager_t *this = malloc_thing(private_fetcher_manager_t);
198
199 this->public.fetch = (status_t(*)(fetcher_manager_t*, char *url, chunk_t *response, ...))fetch;
200 this->public.add_fetcher = (void(*)(fetcher_manager_t*, fetcher_constructor_t,char*))add_fetcher;
201 this->public.remove_fetcher = (void(*)(fetcher_manager_t*, fetcher_constructor_t))remove_fetcher;
202 this->public.destroy = (void(*)(fetcher_manager_t*))destroy;
203
204 this->fetchers = linked_list_create();
205 pthread_rwlock_init(&this->lock, NULL);
206
207 return &this->public;
208 }
209