diff options
Diffstat (limited to 'src/gnunet_chat_file.c')
-rw-r--r-- | src/gnunet_chat_file.c | 219 |
1 files changed, 211 insertions, 8 deletions
diff --git a/src/gnunet_chat_file.c b/src/gnunet_chat_file.c index fdfbe4c..063448e 100644 --- a/src/gnunet_chat_file.c +++ b/src/gnunet_chat_file.c | |||
@@ -39,10 +39,6 @@ file_create_from_message (struct GNUNET_CHAT_Handle *handle, | |||
39 | GNUNET_memcpy(&(file->key), &(message->key), sizeof(file->key)); | 39 | GNUNET_memcpy(&(file->key), &(message->key), sizeof(file->key)); |
40 | GNUNET_memcpy(&(file->hash), &(message->hash), sizeof(file->hash)); | 40 | GNUNET_memcpy(&(file->hash), &(message->hash), sizeof(file->hash)); |
41 | 41 | ||
42 | file->published = 0; | ||
43 | file->downloaded = 0; | ||
44 | file->unindexed = 0; | ||
45 | |||
46 | file->meta = GNUNET_CONTAINER_meta_data_create(); | 42 | file->meta = GNUNET_CONTAINER_meta_data_create(); |
47 | 43 | ||
48 | file->uri = GNUNET_FS_uri_parse(message->uri, NULL); | 44 | file->uri = GNUNET_FS_uri_parse(message->uri, NULL); |
@@ -50,6 +46,17 @@ file_create_from_message (struct GNUNET_CHAT_Handle *handle, | |||
50 | file->publish = NULL; | 46 | file->publish = NULL; |
51 | file->unindex = NULL; | 47 | file->unindex = NULL; |
52 | 48 | ||
49 | file->upload_head = NULL; | ||
50 | file->upload_tail = NULL; | ||
51 | |||
52 | file->download_head = NULL; | ||
53 | file->download_tail = NULL; | ||
54 | |||
55 | file->unindex_head = NULL; | ||
56 | file->unindex_tail = NULL; | ||
57 | |||
58 | file->user_pointer = NULL; | ||
59 | |||
53 | return file; | 60 | return file; |
54 | } | 61 | } |
55 | 62 | ||
@@ -67,10 +74,6 @@ file_create_from_disk (struct GNUNET_CHAT_Handle *handle, | |||
67 | GNUNET_memcpy(&(file->key), key, sizeof(file->key)); | 74 | GNUNET_memcpy(&(file->key), key, sizeof(file->key)); |
68 | GNUNET_memcpy(&(file->hash), hash, sizeof(file->hash)); | 75 | GNUNET_memcpy(&(file->hash), hash, sizeof(file->hash)); |
69 | 76 | ||
70 | file->published = 0; | ||
71 | file->downloaded = 0; | ||
72 | file->unindexed = 0; | ||
73 | |||
74 | file->meta = GNUNET_CONTAINER_meta_data_create(); | 77 | file->meta = GNUNET_CONTAINER_meta_data_create(); |
75 | 78 | ||
76 | file->uri = NULL; | 79 | file->uri = NULL; |
@@ -78,12 +81,65 @@ file_create_from_disk (struct GNUNET_CHAT_Handle *handle, | |||
78 | file->publish = NULL; | 81 | file->publish = NULL; |
79 | file->unindex = NULL; | 82 | file->unindex = NULL; |
80 | 83 | ||
84 | file->upload_head = NULL; | ||
85 | file->upload_tail = NULL; | ||
86 | |||
87 | file->download_head = NULL; | ||
88 | file->download_tail = NULL; | ||
89 | |||
90 | file->unindex_head = NULL; | ||
91 | file->unindex_tail = NULL; | ||
92 | |||
93 | file->user_pointer = NULL; | ||
94 | |||
81 | return file; | 95 | return file; |
82 | } | 96 | } |
83 | 97 | ||
84 | void | 98 | void |
85 | file_destroy (struct GNUNET_CHAT_File* file) | 99 | file_destroy (struct GNUNET_CHAT_File* file) |
86 | { | 100 | { |
101 | struct GNUNET_CHAT_FileUpload *upload; | ||
102 | while (file->upload_head) | ||
103 | { | ||
104 | upload = file->upload_head; | ||
105 | |||
106 | GNUNET_CONTAINER_DLL_remove( | ||
107 | file->upload_head, | ||
108 | file->upload_tail, | ||
109 | upload | ||
110 | ); | ||
111 | |||
112 | GNUNET_free(upload); | ||
113 | } | ||
114 | |||
115 | struct GNUNET_CHAT_FileDownload *download; | ||
116 | while (file->download_head) | ||
117 | { | ||
118 | download = file->download_head; | ||
119 | |||
120 | GNUNET_CONTAINER_DLL_remove( | ||
121 | file->download_head, | ||
122 | file->download_tail, | ||
123 | download | ||
124 | ); | ||
125 | |||
126 | GNUNET_free(download); | ||
127 | } | ||
128 | |||
129 | struct GNUNET_CHAT_FileUnindex *unindex; | ||
130 | while (file->unindex_head) | ||
131 | { | ||
132 | unindex = file->unindex_head; | ||
133 | |||
134 | GNUNET_CONTAINER_DLL_remove( | ||
135 | file->unindex_head, | ||
136 | file->unindex_tail, | ||
137 | unindex | ||
138 | ); | ||
139 | |||
140 | GNUNET_free(unindex); | ||
141 | } | ||
142 | |||
87 | if (file->uri) | 143 | if (file->uri) |
88 | GNUNET_FS_uri_destroy(file->uri); | 144 | GNUNET_FS_uri_destroy(file->uri); |
89 | 145 | ||
@@ -95,3 +151,150 @@ file_destroy (struct GNUNET_CHAT_File* file) | |||
95 | 151 | ||
96 | GNUNET_free(file); | 152 | GNUNET_free(file); |
97 | } | 153 | } |
154 | |||
155 | void | ||
156 | file_bind_upload (struct GNUNET_CHAT_File* file, | ||
157 | GNUNET_CHAT_FileUploadCallback cb, void *cls) | ||
158 | { | ||
159 | struct GNUNET_CHAT_FileUpload *upload = GNUNET_new( | ||
160 | struct GNUNET_CHAT_FileUpload | ||
161 | ); | ||
162 | |||
163 | upload->callback = cb; | ||
164 | upload->cls = cls; | ||
165 | |||
166 | GNUNET_CONTAINER_DLL_insert( | ||
167 | file->upload_head, | ||
168 | file->upload_tail, | ||
169 | upload | ||
170 | ); | ||
171 | } | ||
172 | |||
173 | void | ||
174 | file_bind_downlaod (struct GNUNET_CHAT_File* file, | ||
175 | GNUNET_CHAT_FileDownloadCallback cb, void *cls) | ||
176 | { | ||
177 | struct GNUNET_CHAT_FileDownload *download = GNUNET_new( | ||
178 | struct GNUNET_CHAT_FileDownload | ||
179 | ); | ||
180 | |||
181 | download->callback = cb; | ||
182 | download->cls = cls; | ||
183 | |||
184 | GNUNET_CONTAINER_DLL_insert( | ||
185 | file->download_head, | ||
186 | file->download_tail, | ||
187 | download | ||
188 | ); | ||
189 | } | ||
190 | |||
191 | void | ||
192 | file_bind_unindex (struct GNUNET_CHAT_File* file, | ||
193 | GNUNET_CHAT_FileUnindexCallback cb, void *cls) | ||
194 | { | ||
195 | struct GNUNET_CHAT_FileUnindex *unindex = GNUNET_new( | ||
196 | struct GNUNET_CHAT_FileUnindex | ||
197 | ); | ||
198 | |||
199 | unindex->callback = cb; | ||
200 | unindex->cls = cls; | ||
201 | |||
202 | GNUNET_CONTAINER_DLL_insert( | ||
203 | file->unindex_head, | ||
204 | file->unindex_tail, | ||
205 | unindex | ||
206 | ); | ||
207 | } | ||
208 | |||
209 | void | ||
210 | file_update_upload (struct GNUNET_CHAT_File* file, uint64_t completed, | ||
211 | uint64_t size) | ||
212 | { | ||
213 | struct GNUNET_CHAT_FileUpload *upload = file->upload_head; | ||
214 | |||
215 | while (upload) | ||
216 | { | ||
217 | if (upload->callback) | ||
218 | upload->callback(upload->cls, file, completed, size); | ||
219 | |||
220 | upload = upload->next; | ||
221 | } | ||
222 | |||
223 | if (completed < size) | ||
224 | return; | ||
225 | |||
226 | while (file->upload_head) | ||
227 | { | ||
228 | upload = file->upload_head; | ||
229 | |||
230 | GNUNET_CONTAINER_DLL_remove( | ||
231 | file->upload_head, | ||
232 | file->upload_tail, | ||
233 | upload | ||
234 | ); | ||
235 | |||
236 | GNUNET_free(upload); | ||
237 | } | ||
238 | } | ||
239 | |||
240 | void | ||
241 | file_update_download (struct GNUNET_CHAT_File* file, uint64_t completed, | ||
242 | uint64_t size) | ||
243 | { | ||
244 | struct GNUNET_CHAT_FileDownload *download = file->download_head; | ||
245 | |||
246 | while (download) | ||
247 | { | ||
248 | if (download->callback) | ||
249 | download->callback(download->cls, file, completed, size); | ||
250 | |||
251 | download = download->next; | ||
252 | } | ||
253 | |||
254 | if (completed < size) | ||
255 | return; | ||
256 | |||
257 | while (file->download_head) | ||
258 | { | ||
259 | download = file->download_head; | ||
260 | |||
261 | GNUNET_CONTAINER_DLL_remove( | ||
262 | file->download_head, | ||
263 | file->download_tail, | ||
264 | download | ||
265 | ); | ||
266 | |||
267 | GNUNET_free(download); | ||
268 | } | ||
269 | } | ||
270 | |||
271 | void | ||
272 | file_update_unindex (struct GNUNET_CHAT_File* file, uint64_t completed, | ||
273 | uint64_t size) | ||
274 | { | ||
275 | struct GNUNET_CHAT_FileUnindex *unindex = file->unindex_head; | ||
276 | |||
277 | while (unindex) | ||
278 | { | ||
279 | if (unindex->callback) | ||
280 | unindex->callback(unindex->cls, file, completed, size); | ||
281 | |||
282 | unindex = unindex->next; | ||
283 | } | ||
284 | |||
285 | if (completed < size) | ||
286 | return; | ||
287 | |||
288 | while (file->unindex_head) | ||
289 | { | ||
290 | unindex = file->unindex_head; | ||
291 | |||
292 | GNUNET_CONTAINER_DLL_remove( | ||
293 | file->unindex_head, | ||
294 | file->unindex_tail, | ||
295 | unindex | ||
296 | ); | ||
297 | |||
298 | GNUNET_free(unindex); | ||
299 | } | ||
300 | } | ||