diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-06-29 17:20:17 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-06-29 17:20:17 +0000 |
commit | 2a313fb65fe21e5ac6a1ef268fdf43c2ba46a330 (patch) | |
tree | 3714f986ecad9659448cf417d2795f2b19ddcdd4 /src/fs/fs_list_indexed.c | |
parent | db62b73f216cbf6c955e6f1b9eceffa7beae7339 (diff) | |
download | gnunet-2a313fb65fe21e5ac6a1ef268fdf43c2ba46a330.tar.gz gnunet-2a313fb65fe21e5ac6a1ef268fdf43c2ba46a330.zip |
convert fs_list_indexed to MQ API
Diffstat (limited to 'src/fs/fs_list_indexed.c')
-rw-r--r-- | src/fs/fs_list_indexed.c | 170 |
1 files changed, 105 insertions, 65 deletions
diff --git a/src/fs/fs_list_indexed.c b/src/fs/fs_list_indexed.c index e10260d7b..21385d40b 100644 --- a/src/fs/fs_list_indexed.c +++ b/src/fs/fs_list_indexed.c | |||
@@ -32,19 +32,15 @@ | |||
32 | 32 | ||
33 | 33 | ||
34 | /** | 34 | /** |
35 | * Context for "GNUNET_FS_get_indexed_files". | 35 | * Context for #GNUNET_FS_get_indexed_files(). |
36 | */ | 36 | */ |
37 | struct GNUNET_FS_GetIndexedContext | 37 | struct GNUNET_FS_GetIndexedContext |
38 | { | 38 | { |
39 | /** | ||
40 | * Handle to global FS context. | ||
41 | */ | ||
42 | struct GNUNET_FS_Handle *h; | ||
43 | 39 | ||
44 | /** | 40 | /** |
45 | * Connection to the FS service. | 41 | * Connection to the FS service. |
46 | */ | 42 | */ |
47 | struct GNUNET_CLIENT_Connection *client; | 43 | struct GNUNET_MQ_Handle *mq; |
48 | 44 | ||
49 | /** | 45 | /** |
50 | * Function to call for each indexed file. | 46 | * Function to call for each indexed file. |
@@ -52,7 +48,7 @@ struct GNUNET_FS_GetIndexedContext | |||
52 | GNUNET_FS_IndexedFileProcessor iterator; | 48 | GNUNET_FS_IndexedFileProcessor iterator; |
53 | 49 | ||
54 | /** | 50 | /** |
55 | * Closure for iterator. | 51 | * Closure for @e iterator. |
56 | */ | 52 | */ |
57 | void *iterator_cls; | 53 | void *iterator_cls; |
58 | 54 | ||
@@ -62,7 +58,7 @@ struct GNUNET_FS_GetIndexedContext | |||
62 | GNUNET_SCHEDULER_TaskCallback cont; | 58 | GNUNET_SCHEDULER_TaskCallback cont; |
63 | 59 | ||
64 | /** | 60 | /** |
65 | * Closure for cont. | 61 | * Closure for @e cont. |
66 | */ | 62 | */ |
67 | void *cont_cls; | 63 | void *cont_cls; |
68 | }; | 64 | }; |
@@ -72,58 +68,91 @@ struct GNUNET_FS_GetIndexedContext | |||
72 | * Function called on each response from the FS | 68 | * Function called on each response from the FS |
73 | * service with information about indexed files. | 69 | * service with information about indexed files. |
74 | * | 70 | * |
75 | * @param cls closure (of type "struct GNUNET_FS_GetIndexedContext*") | 71 | * @param cls closure (of type `struct GNUNET_FS_GetIndexedContext *`) |
76 | * @param msg message with indexing information | 72 | * @param msg message with indexing information |
77 | */ | 73 | */ |
78 | static void | 74 | static void |
79 | handle_index_info (void *cls, const struct GNUNET_MessageHeader *msg) | 75 | handle_index_info_end (void *cls, |
76 | const struct GNUNET_MessageHeader *msg) | ||
80 | { | 77 | { |
81 | struct GNUNET_FS_GetIndexedContext *gic = cls; | 78 | struct GNUNET_FS_GetIndexedContext *gic = cls; |
82 | const struct IndexInfoMessage *iim; | 79 | |
83 | uint16_t msize; | 80 | (void) gic->iterator (gic->iterator_cls, |
81 | NULL, | ||
82 | NULL); | ||
83 | GNUNET_FS_get_indexed_files_cancel (gic); | ||
84 | } | ||
85 | |||
86 | |||
87 | /** | ||
88 | * Check validity of response from the FS | ||
89 | * service with information about indexed files. | ||
90 | * | ||
91 | * @param cls closure (of type `struct GNUNET_FS_GetIndexedContext *`) | ||
92 | * @param iim message with indexing information | ||
93 | */ | ||
94 | static int | ||
95 | check_index_info (void *cls, | ||
96 | const struct IndexInfoMessage *iim) | ||
97 | { | ||
98 | uint16_t msize = ntohs (iim->header.size) - sizeof (*iim); | ||
84 | const char *filename; | 99 | const char *filename; |
85 | 100 | ||
86 | if (NULL == msg) | ||
87 | { | ||
88 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
89 | _ | ||
90 | ("Failed to receive response for `%s' request from `%s' service.\n"), | ||
91 | "GET_INDEXED", "fs"); | ||
92 | (void) gic->iterator (gic->iterator_cls, NULL, NULL); | ||
93 | GNUNET_FS_get_indexed_files_cancel (gic); | ||
94 | return; | ||
95 | } | ||
96 | if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END) | ||
97 | { | ||
98 | /* normal end-of-list */ | ||
99 | (void) gic->iterator (gic->iterator_cls, NULL, NULL); | ||
100 | GNUNET_FS_get_indexed_files_cancel (gic); | ||
101 | return; | ||
102 | } | ||
103 | msize = ntohs (msg->size); | ||
104 | iim = (const struct IndexInfoMessage *) msg; | ||
105 | filename = (const char *) &iim[1]; | 101 | filename = (const char *) &iim[1]; |
106 | if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY) || | 102 | if (filename[msize - 1] != '\0') |
107 | (msize <= sizeof (struct IndexInfoMessage)) || | ||
108 | (filename[msize - sizeof (struct IndexInfoMessage) - 1] != '\0')) | ||
109 | { | 103 | { |
110 | /* bogus reply */ | 104 | GNUNET_break (0); |
111 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 105 | return GNUNET_SYSERR; |
112 | _ | ||
113 | ("Failed to receive valid response for `%s' request from `%s' service.\n"), | ||
114 | "GET_INDEXED", "fs"); | ||
115 | (void) gic->iterator (gic->iterator_cls, NULL, NULL); | ||
116 | GNUNET_FS_get_indexed_files_cancel (gic); | ||
117 | return; | ||
118 | } | 106 | } |
119 | if (GNUNET_OK != gic->iterator (gic->iterator_cls, filename, &iim->file_id)) | 107 | return GNUNET_OK; |
108 | } | ||
109 | |||
110 | |||
111 | /** | ||
112 | * Function called on each response from the FS | ||
113 | * service with information about indexed files. | ||
114 | * | ||
115 | * @param cls closure (of type `struct GNUNET_FS_GetIndexedContext *`) | ||
116 | * @param iim message with indexing information | ||
117 | */ | ||
118 | static void | ||
119 | handle_index_info (void *cls, | ||
120 | const struct IndexInfoMessage *iim) | ||
121 | { | ||
122 | struct GNUNET_FS_GetIndexedContext *gic = cls; | ||
123 | const char *filename; | ||
124 | |||
125 | filename = (const char *) &iim[1]; | ||
126 | if (GNUNET_OK != | ||
127 | gic->iterator (gic->iterator_cls, | ||
128 | filename, | ||
129 | &iim->file_id)) | ||
120 | { | 130 | { |
121 | GNUNET_FS_get_indexed_files_cancel (gic); | 131 | GNUNET_FS_get_indexed_files_cancel (gic); |
122 | return; | 132 | return; |
123 | } | 133 | } |
124 | /* get more */ | 134 | } |
125 | GNUNET_CLIENT_receive (gic->client, &handle_index_info, gic, | 135 | |
126 | GNUNET_CONSTANTS_SERVICE_TIMEOUT); | 136 | |
137 | /** | ||
138 | * Generic error handler, called with the appropriate error code and | ||
139 | * the same closure specified at the creation of the message queue. | ||
140 | * Not every message queue implementation supports an error handler. | ||
141 | * | ||
142 | * @param cls closure with the `struct GNUNET_FS_GetIndexedContent *` | ||
143 | * @param error error code | ||
144 | */ | ||
145 | static void | ||
146 | mq_error_handler (void *cls, | ||
147 | enum GNUNET_MQ_Error error) | ||
148 | { | ||
149 | struct GNUNET_FS_GetIndexedContext *gic = cls; | ||
150 | |||
151 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
152 | _("Failed to receive response from `%s' service.\n"), | ||
153 | "fs"); | ||
154 | (void) gic->iterator (gic->iterator_cls, NULL, NULL); | ||
155 | GNUNET_FS_get_indexed_files_cancel (gic); | ||
127 | } | 156 | } |
128 | 157 | ||
129 | 158 | ||
@@ -140,30 +169,41 @@ GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h, | |||
140 | GNUNET_FS_IndexedFileProcessor iterator, | 169 | GNUNET_FS_IndexedFileProcessor iterator, |
141 | void *iterator_cls) | 170 | void *iterator_cls) |
142 | { | 171 | { |
143 | struct GNUNET_CLIENT_Connection *client; | 172 | GNUNET_MQ_hd_fixed_size (index_info_end, |
144 | struct GNUNET_FS_GetIndexedContext *gic; | 173 | GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END, |
145 | struct GNUNET_MessageHeader msg; | 174 | struct GNUNET_MessageHeader); |
146 | 175 | GNUNET_MQ_hd_var_size (index_info, | |
147 | client = GNUNET_CLIENT_connect ("fs", h->cfg); | 176 | GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY, |
148 | if (NULL == client) | 177 | struct IndexInfoMessage); |
178 | struct GNUNET_FS_GetIndexedContext *gic | ||
179 | = GNUNET_new (struct GNUNET_FS_GetIndexedContext); | ||
180 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
181 | make_index_info_end_handler (gic), | ||
182 | make_index_info_handler (gic), | ||
183 | GNUNET_MQ_handler_end () | ||
184 | }; | ||
185 | struct GNUNET_MQ_Envelope *env; | ||
186 | struct GNUNET_MessageHeader *msg; | ||
187 | |||
188 | gic->mq = GNUNET_CLIENT_connecT (h->cfg, | ||
189 | "fs", | ||
190 | handlers, | ||
191 | &mq_error_handler, | ||
192 | h); | ||
193 | if (NULL == gic->mq) | ||
149 | { | 194 | { |
150 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 195 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
151 | _("Failed to not connect to `%s' service.\n"), "fs"); | 196 | _("Failed to not connect to `%s' service.\n"), |
197 | "fs"); | ||
198 | GNUNET_free (gic); | ||
152 | return NULL; | 199 | return NULL; |
153 | } | 200 | } |
154 | gic = GNUNET_new (struct GNUNET_FS_GetIndexedContext); | ||
155 | gic->h = h; | ||
156 | gic->client = client; | ||
157 | gic->iterator = iterator; | 201 | gic->iterator = iterator; |
158 | gic->iterator_cls = iterator_cls; | 202 | gic->iterator_cls = iterator_cls; |
159 | msg.size = htons (sizeof (struct GNUNET_MessageHeader)); | 203 | env = GNUNET_MQ_msg (msg, |
160 | msg.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET); | 204 | GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET); |
161 | GNUNET_assert (GNUNET_OK == | 205 | GNUNET_MQ_send (gic->mq, |
162 | GNUNET_CLIENT_transmit_and_get_response (client, &msg, | 206 | env); |
163 | GNUNET_CONSTANTS_SERVICE_TIMEOUT, | ||
164 | GNUNET_YES, | ||
165 | &handle_index_info, | ||
166 | gic)); | ||
167 | return gic; | 207 | return gic; |
168 | } | 208 | } |
169 | 209 | ||
@@ -176,7 +216,7 @@ GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h, | |||
176 | void | 216 | void |
177 | GNUNET_FS_get_indexed_files_cancel (struct GNUNET_FS_GetIndexedContext *gic) | 217 | GNUNET_FS_get_indexed_files_cancel (struct GNUNET_FS_GetIndexedContext *gic) |
178 | { | 218 | { |
179 | GNUNET_CLIENT_disconnect (gic->client); | 219 | GNUNET_MQ_destroy (gic->mq); |
180 | GNUNET_free (gic); | 220 | GNUNET_free (gic); |
181 | } | 221 | } |
182 | 222 | ||