diff options
Diffstat (limited to 'src/peerstore/peerstore_api.c')
-rw-r--r-- | src/peerstore/peerstore_api.c | 297 |
1 files changed, 132 insertions, 165 deletions
diff --git a/src/peerstore/peerstore_api.c b/src/peerstore/peerstore_api.c index 246bd24b4..df1971c4e 100644 --- a/src/peerstore/peerstore_api.c +++ b/src/peerstore/peerstore_api.c | |||
@@ -302,9 +302,9 @@ struct GNUNET_PEERSTORE_NotifyContext | |||
302 | void *callback_cls; | 302 | void *callback_cls; |
303 | 303 | ||
304 | /** | 304 | /** |
305 | * Iteration context to iterate through all the stored hellos. | 305 | * The watch for this context. |
306 | */ | 306 | */ |
307 | struct GNUNET_PEERSTORE_IterateContext *ic; | 307 | struct GNUNET_PEERSTORE_WatchContext *wc; |
308 | 308 | ||
309 | /** | 309 | /** |
310 | * Is this request canceled. | 310 | * Is this request canceled. |
@@ -333,19 +333,9 @@ struct GNUNET_PEERSTORE_StoreHelloContext | |||
333 | void *cont_cls; | 333 | void *cont_cls; |
334 | 334 | ||
335 | /** | 335 | /** |
336 | * Head of active STORE requests. | 336 | * Map with all store contexts started during adding hello. |
337 | */ | ||
338 | struct GNUNET_PEERSTORE_StoreContext *sc_head; | ||
339 | |||
340 | /** | ||
341 | * Tail of active STORE requests. | ||
342 | */ | 337 | */ |
343 | struct GNUNET_PEERSTORE_StoreContext *sc_tail; | 338 | struct GNUNET_CONTAINER_MultiPeerMap *store_context_map; |
344 | |||
345 | /** | ||
346 | * Iteration context to iterate through all the stored hellos. | ||
347 | */ | ||
348 | struct GNUNET_PEERSTORE_IterateContext *ic; | ||
349 | 339 | ||
350 | /** | 340 | /** |
351 | * Active watch to be notified about conflicting hello uri add requests. | 341 | * Active watch to be notified about conflicting hello uri add requests. |
@@ -355,7 +345,12 @@ struct GNUNET_PEERSTORE_StoreHelloContext | |||
355 | /** | 345 | /** |
356 | * Hello uri which was request for storing. | 346 | * Hello uri which was request for storing. |
357 | */ | 347 | */ |
358 | const struct GNUNET_MessageHeader *hello; | 348 | struct GNUNET_MessageHeader *hello; |
349 | |||
350 | /** | ||
351 | * The peer id for the hello. | ||
352 | */ | ||
353 | struct GNUNET_PeerIdentity *pid; | ||
359 | 354 | ||
360 | /** | 355 | /** |
361 | * Was this request successful. | 356 | * Was this request successful. |
@@ -1034,24 +1029,34 @@ watch_iterate (void *cls, | |||
1034 | if (NULL == record) | 1029 | if (NULL == record) |
1035 | { | 1030 | { |
1036 | struct GNUNET_MQ_Envelope *ev; | 1031 | struct GNUNET_MQ_Envelope *ev; |
1032 | const struct GNUNET_PeerIdentity *peer; | ||
1037 | 1033 | ||
1034 | if (NULL == wc->peer) | ||
1035 | peer = GNUNET_new (struct GNUNET_PeerIdentity); | ||
1036 | else | ||
1037 | peer = wc->peer; | ||
1038 | ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH); | 1038 | ev = GNUNET_MQ_msg (hm, GNUNET_MESSAGE_TYPE_PEERSTORE_WATCH); |
1039 | PEERSTORE_hash_key (wc->sub_system, wc->peer, wc->key, &hm->keyhash); | 1039 | PEERSTORE_hash_key (wc->sub_system, peer, wc->key, &hm->keyhash); |
1040 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
1041 | "Hash key we watch for %s\n", | ||
1042 | GNUNET_h2s_full (&hm->keyhash)); | ||
1040 | wc->keyhash = hm->keyhash; | 1043 | wc->keyhash = hm->keyhash; |
1041 | if (NULL == h->watches) | 1044 | if (NULL == h->watches) |
1042 | h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO); | 1045 | h->watches = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO); |
1043 | GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put ( | 1046 | GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put ( |
1044 | h->watches, | 1047 | h->watches, |
1045 | &wc->keyhash, | 1048 | &wc->keyhash, |
1046 | wc, | 1049 | wc, |
1047 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); | 1050 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); |
1048 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 1051 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1049 | "Sending a watch request for subsystem `%s', peer `%s', key `%s'.\n", | 1052 | "Sending a watch request for subsystem `%s', peer `%s', key `%s'.\n", |
1050 | wc->sub_system, | 1053 | wc->sub_system, |
1051 | GNUNET_i2s (wc->peer), | 1054 | GNUNET_i2s (peer), |
1052 | wc->key); | 1055 | wc->key); |
1053 | GNUNET_MQ_send (h->mq, ev); | 1056 | GNUNET_MQ_send (h->mq, ev); |
1054 | wc->ic = NULL; | 1057 | wc->ic = NULL; |
1058 | if (NULL != wc->callback) | ||
1059 | wc->callback (wc->callback_cls, record, NULL); | ||
1055 | return; | 1060 | return; |
1056 | } | 1061 | } |
1057 | 1062 | ||
@@ -1084,13 +1089,6 @@ GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h, | |||
1084 | struct GNUNET_PEERSTORE_IterateContext *ic; | 1089 | struct GNUNET_PEERSTORE_IterateContext *ic; |
1085 | struct GNUNET_PEERSTORE_WatchContext *wc; | 1090 | struct GNUNET_PEERSTORE_WatchContext *wc; |
1086 | 1091 | ||
1087 | ic = GNUNET_PEERSTORE_iterate (h, | ||
1088 | sub_system, | ||
1089 | peer, | ||
1090 | key, | ||
1091 | &watch_iterate, | ||
1092 | NULL); | ||
1093 | |||
1094 | wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext); | 1092 | wc = GNUNET_new (struct GNUNET_PEERSTORE_WatchContext); |
1095 | wc->callback = callback; | 1093 | wc->callback = callback; |
1096 | wc->callback_cls = callback_cls; | 1094 | wc->callback_cls = callback_cls; |
@@ -1100,6 +1098,13 @@ GNUNET_PEERSTORE_watch (struct GNUNET_PEERSTORE_Handle *h, | |||
1100 | wc->peer = peer; | 1098 | wc->peer = peer; |
1101 | wc->sub_system = sub_system; | 1099 | wc->sub_system = sub_system; |
1102 | 1100 | ||
1101 | ic = GNUNET_PEERSTORE_iterate (h, | ||
1102 | sub_system, | ||
1103 | peer, | ||
1104 | key, | ||
1105 | &watch_iterate, | ||
1106 | wc); | ||
1107 | |||
1103 | return wc; | 1108 | return wc; |
1104 | } | 1109 | } |
1105 | 1110 | ||
@@ -1117,61 +1122,10 @@ hello_updated (void *cls, | |||
1117 | struct GNUNET_PEERSTORE_NotifyContext *nc = cls; | 1122 | struct GNUNET_PEERSTORE_NotifyContext *nc = cls; |
1118 | struct GNUNET_PEERSTORE_Handle *h = nc->h; | 1123 | struct GNUNET_PEERSTORE_Handle *h = nc->h; |
1119 | const struct GNUNET_MessageHeader *hello; | 1124 | const struct GNUNET_MessageHeader *hello; |
1120 | const char *val; | 1125 | struct GNUNET_HELLO_Builder *builder; |
1121 | |||
1122 | if (NULL != emsg) | ||
1123 | { | ||
1124 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
1125 | "Got failure from PEERSTORE: %s\n", | ||
1126 | emsg); | ||
1127 | nc->callback (nc->callback_cls, NULL, NULL, emsg); | ||
1128 | return; | ||
1129 | } | ||
1130 | hello = record->value; | ||
1131 | if ((0 == record->value_size) || ('\0' != val[record->value_size - 1])) | ||
1132 | { | ||
1133 | GNUNET_break (0); | ||
1134 | return; | ||
1135 | } | ||
1136 | nc->callback (nc->callback_cls, &record->peer, hello, NULL); | ||
1137 | } | ||
1138 | |||
1139 | |||
1140 | static void | ||
1141 | watch_cancel (void *cls, | ||
1142 | const struct GNUNET_PEERSTORE_Record *record, | ||
1143 | const char *emsg) | ||
1144 | { | ||
1145 | struct GNUNET_PEERSTORE_NotifyContext *nc = cls; | ||
1146 | struct GNUNET_PEERSTORE_Handle *h = nc->h; | ||
1147 | struct GNUNET_HashCode keyhash; | ||
1148 | struct GNUNET_PEERSTORE_WatchContext *wc; | ||
1149 | |||
1150 | if (NULL != record) | ||
1151 | { | ||
1152 | PEERSTORE_hash_key ("peerstore", &record->peer, record->key, &keyhash); | ||
1153 | wc = GNUNET_CONTAINER_multihashmap_get (h->watches, &keyhash); | ||
1154 | if (NULL != wc) | ||
1155 | GNUNET_PEERSTORE_watch_cancel (wc); | ||
1156 | } | ||
1157 | else | ||
1158 | { | ||
1159 | GNUNET_free (nc); | ||
1160 | } | ||
1161 | } | ||
1162 | |||
1163 | |||
1164 | static void | ||
1165 | set_watch (void *cls, | ||
1166 | const struct GNUNET_PEERSTORE_Record *record, | ||
1167 | const char *emsg) | ||
1168 | { | ||
1169 | struct GNUNET_PEERSTORE_NotifyContext *nc = cls; | ||
1170 | struct GNUNET_PEERSTORE_Handle *h = nc->h; | ||
1171 | struct GNUNET_PEERSTORE_WatchContext *wc; | ||
1172 | const struct GNUNET_MessageHeader *hello; | ||
1173 | const char *val; | ||
1174 | 1126 | ||
1127 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1128 | "hello_updated\n"); | ||
1175 | if (NULL != emsg) | 1129 | if (NULL != emsg) |
1176 | { | 1130 | { |
1177 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1131 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
@@ -1182,19 +1136,20 @@ set_watch (void *cls, | |||
1182 | } | 1136 | } |
1183 | if (NULL == record) | 1137 | if (NULL == record) |
1184 | return; | 1138 | return; |
1185 | |||
1186 | wc = GNUNET_PEERSTORE_watch (h, | ||
1187 | "peerstore", | ||
1188 | &record->peer, | ||
1189 | GNUNET_PEERSTORE_HELLO_KEY, | ||
1190 | &hello_updated, | ||
1191 | nc); | ||
1192 | hello = record->value; | 1139 | hello = record->value; |
1193 | if ((0 == record->value_size) || ('\0' != val[record->value_size - 1])) | 1140 | builder = GNUNET_HELLO_builder_from_msg (hello); |
1141 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1142 | "hello_updated with expired %s and size %lu for peer %s\n", | ||
1143 | GNUNET_STRINGS_absolute_time_to_string (GNUNET_HELLO_builder_get_expiration_time (hello)), | ||
1144 | ntohs (hello->size), | ||
1145 | GNUNET_i2s (&record->peer)); | ||
1146 | if ((0 == record->value_size)) | ||
1194 | { | 1147 | { |
1195 | GNUNET_break (0); | 1148 | GNUNET_break (0); |
1196 | return; | 1149 | return; |
1197 | } | 1150 | } |
1151 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1152 | "hello_updated call callback\n"); | ||
1198 | nc->callback (nc->callback_cls, &record->peer, hello, NULL); | 1153 | nc->callback (nc->callback_cls, &record->peer, hello, NULL); |
1199 | } | 1154 | } |
1200 | 1155 | ||
@@ -1206,20 +1161,18 @@ GNUNET_PEERSTORE_hello_changed_notify (struct GNUNET_PEERSTORE_Handle *h, | |||
1206 | void *callback_cls) | 1161 | void *callback_cls) |
1207 | { | 1162 | { |
1208 | struct GNUNET_PEERSTORE_NotifyContext *nc; | 1163 | struct GNUNET_PEERSTORE_NotifyContext *nc; |
1209 | struct GNUNET_PEERSTORE_IterateContext *ic; | ||
1210 | 1164 | ||
1211 | nc = GNUNET_new (struct GNUNET_PEERSTORE_NotifyContext); | 1165 | nc = GNUNET_new (struct GNUNET_PEERSTORE_NotifyContext); |
1212 | nc->callback = callback; | 1166 | nc->callback = callback; |
1213 | nc->callback_cls = callback_cls; | 1167 | nc->callback_cls = callback_cls; |
1214 | nc->h = h; | 1168 | nc->h = h; |
1215 | 1169 | ||
1216 | ic = GNUNET_PEERSTORE_iterate (h, | 1170 | nc->wc = GNUNET_PEERSTORE_watch (h, |
1217 | "peerstore", | 1171 | "peerstore", |
1218 | NULL, | 1172 | NULL, |
1219 | GNUNET_PEERSTORE_HELLO_KEY, | 1173 | GNUNET_PEERSTORE_HELLO_KEY, |
1220 | &set_watch, | 1174 | &hello_updated, |
1221 | nc); | 1175 | nc); |
1222 | nc->ic = ic; | ||
1223 | 1176 | ||
1224 | return nc; | 1177 | return nc; |
1225 | } | 1178 | } |
@@ -1237,17 +1190,10 @@ GNUNET_PEERSTORE_hello_changed_notify_cancel (struct | |||
1237 | struct GNUNET_PEERSTORE_IterateContext *ic; | 1190 | struct GNUNET_PEERSTORE_IterateContext *ic; |
1238 | struct GNUNET_PEERSTORE_Handle *h = nc->h; | 1191 | struct GNUNET_PEERSTORE_Handle *h = nc->h; |
1239 | 1192 | ||
1240 | if (GNUNET_NO == nc->canceled && NULL != nc->ic) | 1193 | if (NULL != nc->wc) |
1241 | { | 1194 | { |
1242 | nc->canceled = GNUNET_YES; | 1195 | GNUNET_PEERSTORE_watch_cancel (nc->wc); |
1243 | GNUNET_PEERSTORE_iterate_cancel (nc->ic); | 1196 | nc->wc = NULL; |
1244 | ic = GNUNET_PEERSTORE_iterate (h, | ||
1245 | "peerstore", | ||
1246 | NULL, | ||
1247 | GNUNET_PEERSTORE_HELLO_KEY, | ||
1248 | &watch_cancel, | ||
1249 | nc); | ||
1250 | nc->ic = ic; | ||
1251 | } | 1197 | } |
1252 | } | 1198 | } |
1253 | 1199 | ||
@@ -1264,21 +1210,29 @@ merge_success (void *cls, int success) | |||
1264 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1210 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
1265 | "Storing hello uri failed\n"); | 1211 | "Storing hello uri failed\n"); |
1266 | huc->cont (huc->cont_cls, success); | 1212 | huc->cont (huc->cont_cls, success); |
1213 | GNUNET_free (huc->hello); | ||
1214 | GNUNET_free (huc->pid); | ||
1215 | GNUNET_free (huc); | ||
1267 | return; | 1216 | return; |
1268 | } | 1217 | } |
1269 | GNUNET_CONTAINER_DLL_remove (huc->sc_head, huc->sc_tail, shu_cls->sc); | 1218 | GNUNET_CONTAINER_multipeermap_remove (huc->store_context_map, huc->pid, shu_cls->sc); |
1270 | if (NULL == huc->sc_head) | 1219 | if (0 == GNUNET_CONTAINER_multipeermap_size (huc->store_context_map)) |
1271 | { | 1220 | { |
1272 | GNUNET_PEERSTORE_watch_cancel (huc->wc); | 1221 | GNUNET_PEERSTORE_watch_cancel (huc->wc); |
1273 | huc->wc = NULL; | 1222 | huc->wc = NULL; |
1274 | huc->cont (huc->cont_cls, GNUNET_OK); | 1223 | huc->cont (huc->cont_cls, GNUNET_OK); |
1275 | huc->success = GNUNET_OK; | 1224 | huc->success = GNUNET_OK; |
1276 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1225 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1277 | "Storing hello uri succeeded!\n"); | 1226 | "Storing hello uri succeeded for peer %s!\n", |
1227 | GNUNET_i2s (huc->pid)); | ||
1228 | GNUNET_free (huc->hello); | ||
1229 | GNUNET_free (huc->pid); | ||
1230 | GNUNET_free (huc); | ||
1278 | return; | 1231 | return; |
1279 | } | 1232 | } |
1280 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1233 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1281 | "Got notified during storing hello uri!\n"); | 1234 | "Got notified during storing hello uri for peer %s!\n", |
1235 | GNUNET_i2s (huc->pid)); | ||
1282 | } | 1236 | } |
1283 | 1237 | ||
1284 | 1238 | ||
@@ -1287,30 +1241,31 @@ store_hello (struct GNUNET_PEERSTORE_StoreHelloContext *huc, | |||
1287 | const struct GNUNET_MessageHeader *hello) | 1241 | const struct GNUNET_MessageHeader *hello) |
1288 | { | 1242 | { |
1289 | struct GNUNET_PEERSTORE_Handle *h = huc->h; | 1243 | struct GNUNET_PEERSTORE_Handle *h = huc->h; |
1290 | struct GNUNET_HELLO_Builder *builder; | ||
1291 | struct GNUNET_PeerIdentity *pid; | 1244 | struct GNUNET_PeerIdentity *pid; |
1292 | struct GNUNET_PEERSTORE_StoreContext *sc; | 1245 | struct GNUNET_PEERSTORE_StoreContext *sc; |
1293 | struct StoreHelloCls *shu_cls = GNUNET_new (struct StoreHelloCls); | 1246 | struct StoreHelloCls *shu_cls = GNUNET_new (struct StoreHelloCls); |
1294 | struct GNUNET_TIME_Absolute hello_exp; | 1247 | struct GNUNET_TIME_Absolute hello_exp; |
1295 | 1248 | ||
1296 | shu_cls->huc = huc; | 1249 | shu_cls->huc = huc; |
1297 | builder = GNUNET_HELLO_builder_from_msg (hello); | 1250 | hello_exp = GNUNET_HELLO_builder_get_expiration_time (hello); |
1298 | hello_exp = GNUNET_HELLO_builder_get_expiration_time (builder, | ||
1299 | hello); | ||
1300 | pid = GNUNET_HELLO_builder_get_id (builder); | ||
1301 | sc = GNUNET_PEERSTORE_store (h, | 1251 | sc = GNUNET_PEERSTORE_store (h, |
1302 | "peerstore", | 1252 | "peerstore", |
1303 | pid, | 1253 | huc->pid, |
1304 | GNUNET_PEERSTORE_HELLO_KEY, | 1254 | GNUNET_PEERSTORE_HELLO_KEY, |
1305 | hello, | 1255 | hello, |
1306 | sizeof(hello), | 1256 | ntohs (hello->size), |
1307 | hello_exp, | 1257 | hello_exp, |
1308 | GNUNET_PEERSTORE_STOREOPTION_REPLACE, | 1258 | GNUNET_PEERSTORE_STOREOPTION_MULTIPLE, |
1309 | merge_success, | 1259 | merge_success, |
1310 | shu_cls); | 1260 | shu_cls); |
1261 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
1262 | "store_hello with expiration %s\n", | ||
1263 | GNUNET_STRINGS_absolute_time_to_string (hello_exp)); | ||
1264 | GNUNET_CONTAINER_multipeermap_put (huc->store_context_map, | ||
1265 | huc->pid, | ||
1266 | sc, | ||
1267 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); | ||
1311 | shu_cls->sc = sc; | 1268 | shu_cls->sc = sc; |
1312 | GNUNET_CONTAINER_DLL_insert (huc->sc_head, huc->sc_tail, sc); | ||
1313 | GNUNET_HELLO_builder_free (builder); | ||
1314 | } | 1269 | } |
1315 | 1270 | ||
1316 | 1271 | ||
@@ -1334,48 +1289,33 @@ merge_uri (void *cls, | |||
1334 | emsg); | 1289 | emsg); |
1335 | return; | 1290 | return; |
1336 | } | 1291 | } |
1337 | if (NULL == record) | ||
1338 | return; | ||
1339 | 1292 | ||
1340 | if (NULL == huc->wc && GNUNET_NO == huc->success) | 1293 | if (NULL == record && GNUNET_NO == huc->success) |
1341 | { | 1294 | { |
1342 | wc = GNUNET_PEERSTORE_watch (h, | 1295 | huc_hello_exp_time = GNUNET_HELLO_builder_get_expiration_time (huc->hello); |
1343 | "peerstore", | 1296 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1344 | &record->peer, | 1297 | "merge_uri just store for peer %s with expiration %s\n", |
1345 | GNUNET_PEERSTORE_HELLO_KEY, | 1298 | GNUNET_i2s (huc->pid), |
1346 | &merge_uri, | 1299 | GNUNET_STRINGS_absolute_time_to_string (huc_hello_exp_time)); |
1347 | huc); | 1300 | store_hello (huc, huc->hello); |
1348 | huc->wc = wc; | ||
1349 | } | 1301 | } |
1350 | 1302 | else if (GNUNET_NO == huc->success && 0 == GNUNET_memcmp (huc->pid, &record->peer)) | |
1351 | if (NULL != record) | ||
1352 | { | 1303 | { |
1353 | struct GNUNET_HELLO_Builder *builder; | 1304 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1354 | struct GNUNET_HELLO_Builder *huc_builder; | 1305 | "merge_uri record for peer %s\n", |
1355 | 1306 | GNUNET_i2s (&record->peer)); | |
1356 | hello = record->value; | 1307 | hello = record->value; |
1357 | builder = GNUNET_HELLO_builder_from_msg (hello); | ||
1358 | huc_builder = GNUNET_HELLO_builder_from_msg (huc->hello); | ||
1359 | if ((0 == record->value_size)) | 1308 | if ((0 == record->value_size)) |
1360 | { | 1309 | { |
1361 | GNUNET_break (0); | 1310 | GNUNET_break (0); |
1362 | return; | 1311 | return; |
1363 | } | 1312 | } |
1364 | 1313 | ||
1365 | huc_hello_exp_time = GNUNET_HELLO_builder_get_expiration_time (huc_builder, | 1314 | huc_hello_exp_time = GNUNET_HELLO_builder_get_expiration_time (huc->hello); |
1366 | huc->hello); | 1315 | record_hello_exp_time = GNUNET_HELLO_builder_get_expiration_time (hello); |
1367 | record_hello_exp_time = GNUNET_HELLO_builder_get_expiration_time (builder, | ||
1368 | hello); | ||
1369 | 1316 | ||
1370 | if (GNUNET_TIME_absolute_cmp (huc_hello_exp_time, >, record_hello_exp_time)) | 1317 | if (GNUNET_TIME_absolute_cmp (huc_hello_exp_time, >, record_hello_exp_time)) |
1371 | store_hello (huc, huc->hello); | 1318 | store_hello (huc, huc->hello); |
1372 | |||
1373 | GNUNET_HELLO_builder_free (builder); | ||
1374 | GNUNET_HELLO_builder_free (huc_builder); | ||
1375 | } | ||
1376 | else | ||
1377 | { | ||
1378 | store_hello (huc, huc->hello); | ||
1379 | } | 1319 | } |
1380 | } | 1320 | } |
1381 | 1321 | ||
@@ -1388,45 +1328,72 @@ GNUNET_PEERSTORE_hello_add (struct GNUNET_PEERSTORE_Handle *h, | |||
1388 | { | 1328 | { |
1389 | struct GNUNET_HELLO_Builder *builder = GNUNET_HELLO_builder_from_msg (msg); | 1329 | struct GNUNET_HELLO_Builder *builder = GNUNET_HELLO_builder_from_msg (msg); |
1390 | struct GNUNET_PEERSTORE_StoreHelloContext *huc; | 1330 | struct GNUNET_PEERSTORE_StoreHelloContext *huc; |
1391 | struct GNUNET_PEERSTORE_IterateContext *ic; | ||
1392 | struct GNUNET_PeerIdentity *pid; | 1331 | struct GNUNET_PeerIdentity *pid; |
1393 | struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); | 1332 | struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); |
1394 | struct GNUNET_TIME_Absolute hello_exp = | 1333 | struct GNUNET_TIME_Absolute hello_exp = |
1395 | GNUNET_HELLO_builder_get_expiration_time (builder, | 1334 | GNUNET_HELLO_builder_get_expiration_time (msg); |
1396 | msg); | 1335 | struct GNUNET_TIME_Absolute huc_exp; |
1336 | uint16_t pid_size; | ||
1337 | uint16_t size_msg = ntohs (msg->size); | ||
1397 | 1338 | ||
1339 | if (NULL == builder) | ||
1340 | return NULL; | ||
1398 | if (GNUNET_TIME_absolute_cmp (hello_exp, <, now)) | 1341 | if (GNUNET_TIME_absolute_cmp (hello_exp, <, now)) |
1399 | return NULL; | 1342 | return NULL; |
1400 | 1343 | ||
1401 | huc = GNUNET_new (struct GNUNET_PEERSTORE_StoreHelloContext); | 1344 | huc = GNUNET_new (struct GNUNET_PEERSTORE_StoreHelloContext); |
1345 | huc->store_context_map = GNUNET_CONTAINER_multipeermap_create (1, GNUNET_NO); | ||
1402 | huc->h = h; | 1346 | huc->h = h; |
1403 | huc->cont = cont; | 1347 | huc->cont = cont; |
1404 | huc->cont_cls = cont_cls; | 1348 | huc->cont_cls = cont_cls; |
1405 | huc->hello = msg; | 1349 | huc->hello = GNUNET_malloc (size_msg); |
1350 | GNUNET_memcpy (huc->hello, msg, size_msg); | ||
1351 | huc_exp = | ||
1352 | GNUNET_HELLO_builder_get_expiration_time (huc->hello); | ||
1406 | pid = GNUNET_HELLO_builder_get_id (builder); | 1353 | pid = GNUNET_HELLO_builder_get_id (builder); |
1407 | ic = GNUNET_PEERSTORE_iterate (h, | 1354 | pid_size = sizeof (struct GNUNET_PeerIdentity); |
1408 | "peerstore", | 1355 | huc->pid = GNUNET_malloc (pid_size); |
1409 | pid, | 1356 | GNUNET_memcpy (huc->pid, pid, pid_size); |
1410 | GNUNET_PEERSTORE_HELLO_KEY, | 1357 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
1411 | &merge_uri, | 1358 | "Adding hello for peer %s with expiration %s msg size %lu\n", |
1412 | huc); | 1359 | GNUNET_i2s (huc->pid), |
1360 | GNUNET_STRINGS_absolute_time_to_string (huc_exp), | ||
1361 | size_msg); | ||
1362 | huc->wc = GNUNET_PEERSTORE_watch (h, | ||
1363 | "peerstore", | ||
1364 | NULL, | ||
1365 | GNUNET_PEERSTORE_HELLO_KEY, | ||
1366 | &merge_uri, | ||
1367 | huc); | ||
1413 | GNUNET_HELLO_builder_free (builder); | 1368 | GNUNET_HELLO_builder_free (builder); |
1414 | huc->ic = ic; | ||
1415 | 1369 | ||
1416 | return huc; | 1370 | return huc; |
1417 | } | 1371 | } |
1418 | 1372 | ||
1419 | 1373 | ||
1374 | static enum GNUNET_GenericReturnValue | ||
1375 | free_store_context(void *cls, | ||
1376 | const struct GNUNET_PeerIdentity *key, | ||
1377 | void *value) | ||
1378 | { | ||
1379 | (void *) cls; | ||
1380 | |||
1381 | GNUNET_PEERSTORE_store_cancel ((struct GNUNET_PEERSTORE_StoreContext *) value); | ||
1382 | } | ||
1383 | |||
1384 | |||
1420 | void | 1385 | void |
1421 | GNUNET_PEERSTORE_hello_add_cancel (struct | 1386 | GNUNET_PEERSTORE_hello_add_cancel (struct |
1422 | GNUNET_PEERSTORE_StoreHelloContext *huc) | 1387 | GNUNET_PEERSTORE_StoreHelloContext *huc) |
1423 | { | 1388 | { |
1424 | struct GNUNET_PEERSTORE_StoreContext *sc; | 1389 | struct GNUNET_PEERSTORE_StoreContext *sc; |
1425 | 1390 | ||
1426 | GNUNET_PEERSTORE_iterate_cancel (huc->ic); | ||
1427 | GNUNET_PEERSTORE_watch_cancel (huc->wc); | 1391 | GNUNET_PEERSTORE_watch_cancel (huc->wc); |
1428 | while (NULL != (sc = huc->sc_head)) | 1392 | GNUNET_CONTAINER_multipeermap_iterate (huc->store_context_map, |
1429 | GNUNET_PEERSTORE_store_cancel (sc); | 1393 | free_store_context, |
1394 | NULL); | ||
1395 | GNUNET_free (huc->hello); | ||
1396 | GNUNET_free (huc->pid); | ||
1430 | GNUNET_free (huc); | 1397 | GNUNET_free (huc); |
1431 | } | 1398 | } |
1432 | 1399 | ||