diff options
author | Gabor X Toth <*@tg-x.net> | 2014-07-23 16:19:49 +0000 |
---|---|---|
committer | Gabor X Toth <*@tg-x.net> | 2014-07-23 16:19:49 +0000 |
commit | 3cf8ba0b60f8495892fa76635e9c23555d0a304c (patch) | |
tree | 5f27648bdb3cf3409628e4e5edc26f811cbd03a5 /src/psyc/psyc_api.c | |
parent | 252b5599987b7ba03b879a8c2d1c455ad4c9834a (diff) | |
download | gnunet-3cf8ba0b60f8495892fa76635e9c23555d0a304c.tar.gz gnunet-3cf8ba0b60f8495892fa76635e9c23555d0a304c.zip |
social: implement enter/leave/messaging; psyc: improvements and fixes
- social: implement enter/leave, send/receive messages, slicer
- psyc, social: add struct GNUNET_PSYC_Message for single-fragment join messages
- psyc: add message callback in addition to message part callback
- client_manager, social, psyc, multicast: add disconnect callback
Diffstat (limited to 'src/psyc/psyc_api.c')
-rw-r--r-- | src/psyc/psyc_api.c | 133 |
1 files changed, 109 insertions, 24 deletions
diff --git a/src/psyc/psyc_api.c b/src/psyc/psyc_api.c index 88b007a0f..a43b1ef5f 100644 --- a/src/psyc/psyc_api.c +++ b/src/psyc/psyc_api.c | |||
@@ -74,6 +74,16 @@ struct GNUNET_PSYC_Channel | |||
74 | struct GNUNET_MessageHeader *connect_msg; | 74 | struct GNUNET_MessageHeader *connect_msg; |
75 | 75 | ||
76 | /** | 76 | /** |
77 | * Function called after disconnected from the service. | ||
78 | */ | ||
79 | GNUNET_ContinuationCallback disconnect_cb; | ||
80 | |||
81 | /** | ||
82 | * Closure for @a disconnect_cb. | ||
83 | */ | ||
84 | void *disconnect_cls; | ||
85 | |||
86 | /** | ||
77 | * Are we polling for incoming messages right now? | 87 | * Are we polling for incoming messages right now? |
78 | */ | 88 | */ |
79 | uint8_t in_receive; | 89 | uint8_t in_receive; |
@@ -82,6 +92,12 @@ struct GNUNET_PSYC_Channel | |||
82 | * Is this a master or slave channel? | 92 | * Is this a master or slave channel? |
83 | */ | 93 | */ |
84 | uint8_t is_master; | 94 | uint8_t is_master; |
95 | |||
96 | /** | ||
97 | * Is this channel in the process of disconnecting from the service? | ||
98 | * #GNUNET_YES or #GNUNET_NO | ||
99 | */ | ||
100 | uint8_t is_disconnecting; | ||
85 | }; | 101 | }; |
86 | 102 | ||
87 | 103 | ||
@@ -232,19 +248,26 @@ master_recv_join_request (void *cls, | |||
232 | struct GNUNET_PSYC_Master * | 248 | struct GNUNET_PSYC_Master * |
233 | mst = GNUNET_CLIENT_MANAGER_get_user_context_ (client, | 249 | mst = GNUNET_CLIENT_MANAGER_get_user_context_ (client, |
234 | sizeof (struct GNUNET_PSYC_Channel)); | 250 | sizeof (struct GNUNET_PSYC_Channel)); |
235 | 251 | if (NULL == mst->join_req_cb) | |
236 | const struct MasterJoinRequest *req = (const struct MasterJoinRequest *) msg; | 252 | return; |
237 | 253 | ||
238 | struct GNUNET_PSYC_MessageHeader *pmsg = NULL; | 254 | const struct GNUNET_PSYC_JoinRequestMessage * |
239 | if (ntohs (req->header.size) <= sizeof (*req) + sizeof (*pmsg)) | 255 | req = (const struct GNUNET_PSYC_JoinRequestMessage *) msg; |
240 | pmsg = (struct GNUNET_PSYC_MessageHeader *) &req[1]; | 256 | const struct GNUNET_PSYC_Message *join_msg = NULL; |
257 | if (sizeof (*req) + sizeof (*join_msg) <= ntohs (req->header.size)) | ||
258 | { | ||
259 | join_msg = (struct GNUNET_PSYC_Message *) &req[1]; | ||
260 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
261 | "Received join_msg of type %u and size %u.\n", | ||
262 | ntohs (join_msg->header.type), ntohs (join_msg->header.size)); | ||
263 | } | ||
241 | 264 | ||
242 | struct GNUNET_PSYC_JoinHandle *jh = GNUNET_malloc (sizeof (*jh)); | 265 | struct GNUNET_PSYC_JoinHandle *jh = GNUNET_malloc (sizeof (*jh)); |
243 | jh->mst = mst; | 266 | jh->mst = mst; |
244 | jh->slave_key = req->slave_key; | 267 | jh->slave_key = req->slave_key; |
245 | 268 | ||
246 | if (NULL != mst->join_req_cb) | 269 | if (NULL != mst->join_req_cb) |
247 | mst->join_req_cb (mst->cb_cls, &req->slave_key, pmsg, jh); | 270 | mst->join_req_cb (mst->cb_cls, req, &req->slave_key, join_msg, jh); |
248 | } | 271 | } |
249 | 272 | ||
250 | 273 | ||
@@ -273,13 +296,12 @@ slave_recv_join_decision (void *cls, | |||
273 | const struct GNUNET_PSYC_JoinDecisionMessage * | 296 | const struct GNUNET_PSYC_JoinDecisionMessage * |
274 | dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg; | 297 | dcsn = (const struct GNUNET_PSYC_JoinDecisionMessage *) msg; |
275 | 298 | ||
276 | struct GNUNET_PSYC_MessageHeader *pmsg = NULL; | 299 | struct GNUNET_PSYC_Message *pmsg = NULL; |
277 | if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg)) | 300 | if (ntohs (dcsn->header.size) <= sizeof (*dcsn) + sizeof (*pmsg)) |
278 | pmsg = (struct GNUNET_PSYC_MessageHeader *) &dcsn[1]; | 301 | pmsg = (struct GNUNET_PSYC_Message *) &dcsn[1]; |
279 | 302 | ||
280 | struct GNUNET_PSYC_JoinHandle *jh = GNUNET_malloc (sizeof (*jh)); | ||
281 | if (NULL != slv->join_dcsn_cb) | 303 | if (NULL != slv->join_dcsn_cb) |
282 | slv->join_dcsn_cb (slv->cb_cls, ntohl (dcsn->is_admitted), pmsg); | 304 | slv->join_dcsn_cb (slv->cb_cls, dcsn, ntohl (dcsn->is_admitted), pmsg); |
283 | } | 305 | } |
284 | 306 | ||
285 | 307 | ||
@@ -299,7 +321,7 @@ static struct GNUNET_CLIENT_MANAGER_MessageHandler master_handlers[] = | |||
299 | 321 | ||
300 | { &master_recv_join_request, NULL, | 322 | { &master_recv_join_request, NULL, |
301 | GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST, | 323 | GNUNET_MESSAGE_TYPE_PSYC_JOIN_REQUEST, |
302 | sizeof (struct MasterJoinRequest), GNUNET_YES }, | 324 | sizeof (struct GNUNET_PSYC_JoinRequestMessage), GNUNET_YES }, |
303 | 325 | ||
304 | { &channel_recv_disconnect, NULL, 0, 0, GNUNET_NO }, | 326 | { &channel_recv_disconnect, NULL, 0, 0, GNUNET_NO }, |
305 | 327 | ||
@@ -331,6 +353,35 @@ static struct GNUNET_CLIENT_MANAGER_MessageHandler slave_handlers[] = | |||
331 | }; | 353 | }; |
332 | 354 | ||
333 | 355 | ||
356 | static void | ||
357 | channel_cleanup (struct GNUNET_PSYC_Channel *chn) | ||
358 | { | ||
359 | GNUNET_PSYC_transmit_destroy (chn->tmit); | ||
360 | GNUNET_PSYC_receive_destroy (chn->recv); | ||
361 | GNUNET_free (chn->connect_msg); | ||
362 | if (NULL != chn->disconnect_cb) | ||
363 | chn->disconnect_cb (chn->disconnect_cls); | ||
364 | } | ||
365 | |||
366 | |||
367 | static void | ||
368 | master_cleanup (void *cls) | ||
369 | { | ||
370 | struct GNUNET_PSYC_Master *mst = cls; | ||
371 | channel_cleanup (&mst->chn); | ||
372 | GNUNET_free (mst); | ||
373 | } | ||
374 | |||
375 | |||
376 | static void | ||
377 | slave_cleanup (void *cls) | ||
378 | { | ||
379 | struct GNUNET_PSYC_Slave *slv = cls; | ||
380 | channel_cleanup (&slv->chn); | ||
381 | GNUNET_free (slv); | ||
382 | } | ||
383 | |||
384 | |||
334 | /** | 385 | /** |
335 | * Start a PSYC master channel. | 386 | * Start a PSYC master channel. |
336 | * | 387 | * |
@@ -367,6 +418,7 @@ GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
367 | GNUNET_PSYC_MasterStartCallback start_cb, | 418 | GNUNET_PSYC_MasterStartCallback start_cb, |
368 | GNUNET_PSYC_JoinRequestCallback join_request_cb, | 419 | GNUNET_PSYC_JoinRequestCallback join_request_cb, |
369 | GNUNET_PSYC_MessageCallback message_cb, | 420 | GNUNET_PSYC_MessageCallback message_cb, |
421 | GNUNET_PSYC_MessagePartCallback message_part_cb, | ||
370 | void *cls) | 422 | void *cls) |
371 | { | 423 | { |
372 | struct GNUNET_PSYC_Master *mst = GNUNET_malloc (sizeof (*mst)); | 424 | struct GNUNET_PSYC_Master *mst = GNUNET_malloc (sizeof (*mst)); |
@@ -390,7 +442,7 @@ GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
390 | GNUNET_CLIENT_MANAGER_set_user_context_ (chn->client, mst, sizeof (*chn)); | 442 | GNUNET_CLIENT_MANAGER_set_user_context_ (chn->client, mst, sizeof (*chn)); |
391 | 443 | ||
392 | chn->tmit = GNUNET_PSYC_transmit_create (chn->client); | 444 | chn->tmit = GNUNET_PSYC_transmit_create (chn->client); |
393 | chn->recv = GNUNET_PSYC_receive_create (message_cb, message_cb, cls); | 445 | chn->recv = GNUNET_PSYC_receive_create (message_cb, message_part_cb, cls); |
394 | 446 | ||
395 | channel_send_connect_msg (chn); | 447 | channel_send_connect_msg (chn); |
396 | return mst; | 448 | return mst; |
@@ -404,10 +456,21 @@ GNUNET_PSYC_master_start (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
404 | * @param keep_active FIXME | 456 | * @param keep_active FIXME |
405 | */ | 457 | */ |
406 | void | 458 | void |
407 | GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *mst) | 459 | GNUNET_PSYC_master_stop (struct GNUNET_PSYC_Master *mst, |
460 | int keep_active, | ||
461 | GNUNET_ContinuationCallback stop_cb, | ||
462 | void *stop_cls) | ||
408 | { | 463 | { |
409 | GNUNET_CLIENT_MANAGER_disconnect (mst->chn.client, GNUNET_YES); | 464 | struct GNUNET_PSYC_Channel *chn = &mst->chn; |
410 | GNUNET_free (mst); | 465 | |
466 | /* FIXME: send msg to service */ | ||
467 | |||
468 | chn->is_disconnecting = GNUNET_YES; | ||
469 | chn->disconnect_cb = stop_cb; | ||
470 | chn->disconnect_cls = stop_cls; | ||
471 | |||
472 | GNUNET_CLIENT_MANAGER_disconnect (mst->chn.client, GNUNET_YES, | ||
473 | &master_cleanup, mst); | ||
411 | } | 474 | } |
412 | 475 | ||
413 | 476 | ||
@@ -439,7 +502,7 @@ GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh, | |||
439 | int is_admitted, | 502 | int is_admitted, |
440 | uint32_t relay_count, | 503 | uint32_t relay_count, |
441 | const struct GNUNET_PeerIdentity *relays, | 504 | const struct GNUNET_PeerIdentity *relays, |
442 | const struct GNUNET_PSYC_MessageHeader *join_resp) | 505 | const struct GNUNET_PSYC_Message *join_resp) |
443 | { | 506 | { |
444 | struct GNUNET_PSYC_Channel *chn = &jh->mst->chn; | 507 | struct GNUNET_PSYC_Channel *chn = &jh->mst->chn; |
445 | struct GNUNET_PSYC_JoinDecisionMessage *dcsn; | 508 | struct GNUNET_PSYC_JoinDecisionMessage *dcsn; |
@@ -461,6 +524,7 @@ GNUNET_PSYC_join_decision (struct GNUNET_PSYC_JoinHandle *jh, | |||
461 | memcpy (&dcsn[1], join_resp, join_resp_size); | 524 | memcpy (&dcsn[1], join_resp, join_resp_size); |
462 | 525 | ||
463 | GNUNET_CLIENT_MANAGER_transmit (chn->client, &dcsn->header); | 526 | GNUNET_CLIENT_MANAGER_transmit (chn->client, &dcsn->header); |
527 | GNUNET_free (jh); | ||
464 | return GNUNET_OK; | 528 | return GNUNET_OK; |
465 | } | 529 | } |
466 | 530 | ||
@@ -576,15 +640,19 @@ GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
576 | uint32_t relay_count, | 640 | uint32_t relay_count, |
577 | const struct GNUNET_PeerIdentity *relays, | 641 | const struct GNUNET_PeerIdentity *relays, |
578 | GNUNET_PSYC_MessageCallback message_cb, | 642 | GNUNET_PSYC_MessageCallback message_cb, |
643 | GNUNET_PSYC_MessagePartCallback message_part_cb, | ||
579 | GNUNET_PSYC_SlaveConnectCallback connect_cb, | 644 | GNUNET_PSYC_SlaveConnectCallback connect_cb, |
580 | GNUNET_PSYC_JoinDecisionCallback join_decision_cb, | 645 | GNUNET_PSYC_JoinDecisionCallback join_decision_cb, |
581 | void *cls, | 646 | void *cls, |
582 | const struct GNUNET_MessageHeader *join_msg) | 647 | const struct GNUNET_PSYC_Message *join_msg) |
583 | { | 648 | { |
584 | struct GNUNET_PSYC_Slave *slv = GNUNET_malloc (sizeof (*slv)); | 649 | struct GNUNET_PSYC_Slave *slv = GNUNET_malloc (sizeof (*slv)); |
585 | struct GNUNET_PSYC_Channel *chn = &slv->chn; | 650 | struct GNUNET_PSYC_Channel *chn = &slv->chn; |
651 | |||
652 | uint16_t relay_size = relay_count * sizeof (*relays); | ||
653 | uint16_t join_msg_size = ntohs (join_msg->header.size); | ||
586 | struct SlaveJoinRequest *req | 654 | struct SlaveJoinRequest *req |
587 | = GNUNET_malloc (sizeof (*req) + relay_count * sizeof (*relays)); | 655 | = GNUNET_malloc (sizeof (*req) + relay_size + join_msg_size); |
588 | req->header.size = htons (sizeof (*req) | 656 | req->header.size = htons (sizeof (*req) |
589 | + relay_count * sizeof (*relays)); | 657 | + relay_count * sizeof (*relays)); |
590 | req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN); | 658 | req->header.type = htons (GNUNET_MESSAGE_TYPE_PSYC_SLAVE_JOIN); |
@@ -592,7 +660,12 @@ GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
592 | req->slave_key = *slave_key; | 660 | req->slave_key = *slave_key; |
593 | req->origin = *origin; | 661 | req->origin = *origin; |
594 | req->relay_count = htonl (relay_count); | 662 | req->relay_count = htonl (relay_count); |
595 | memcpy (&req[1], relays, relay_count * sizeof (*relays)); | 663 | |
664 | if (0 < relay_size) | ||
665 | memcpy (&req[1], relays, relay_size); | ||
666 | |||
667 | if (0 < join_msg_size) | ||
668 | memcpy ((char *) &req[1] + relay_size, join_msg, join_msg_size); | ||
596 | 669 | ||
597 | chn->connect_msg = (struct GNUNET_MessageHeader *) req; | 670 | chn->connect_msg = (struct GNUNET_MessageHeader *) req; |
598 | chn->cfg = cfg; | 671 | chn->cfg = cfg; |
@@ -605,7 +678,7 @@ GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
605 | chn->client = GNUNET_CLIENT_MANAGER_connect (cfg, "psyc", slave_handlers); | 678 | chn->client = GNUNET_CLIENT_MANAGER_connect (cfg, "psyc", slave_handlers); |
606 | GNUNET_CLIENT_MANAGER_set_user_context_ (chn->client, slv, sizeof (*chn)); | 679 | GNUNET_CLIENT_MANAGER_set_user_context_ (chn->client, slv, sizeof (*chn)); |
607 | 680 | ||
608 | chn->recv = GNUNET_PSYC_receive_create (message_cb, message_cb, cls); | 681 | chn->recv = GNUNET_PSYC_receive_create (message_cb, message_part_cb, cls); |
609 | chn->tmit = GNUNET_PSYC_transmit_create (chn->client); | 682 | chn->tmit = GNUNET_PSYC_transmit_create (chn->client); |
610 | 683 | ||
611 | channel_send_connect_msg (chn); | 684 | channel_send_connect_msg (chn); |
@@ -622,10 +695,21 @@ GNUNET_PSYC_slave_join (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
622 | * @param slave Slave handle. | 695 | * @param slave Slave handle. |
623 | */ | 696 | */ |
624 | void | 697 | void |
625 | GNUNET_PSYC_slave_part (struct GNUNET_PSYC_Slave *slv) | 698 | GNUNET_PSYC_slave_part (struct GNUNET_PSYC_Slave *slv, |
699 | int keep_active, | ||
700 | GNUNET_ContinuationCallback part_cb, | ||
701 | void *part_cls) | ||
626 | { | 702 | { |
627 | GNUNET_CLIENT_MANAGER_disconnect (slv->chn.client, GNUNET_YES); | 703 | struct GNUNET_PSYC_Channel *chn = &slv->chn; |
628 | GNUNET_free (slv); | 704 | |
705 | /* FIXME: send msg to service */ | ||
706 | |||
707 | chn->is_disconnecting = GNUNET_YES; | ||
708 | chn->disconnect_cb = part_cb; | ||
709 | chn->disconnect_cls = part_cls; | ||
710 | |||
711 | GNUNET_CLIENT_MANAGER_disconnect (slv->chn.client, GNUNET_YES, | ||
712 | &slave_cleanup, slv); | ||
629 | } | 713 | } |
630 | 714 | ||
631 | 715 | ||
@@ -796,6 +880,7 @@ GNUNET_PSYC_channel_story_tell (struct GNUNET_PSYC_Channel *channel, | |||
796 | uint64_t start_message_id, | 880 | uint64_t start_message_id, |
797 | uint64_t end_message_id, | 881 | uint64_t end_message_id, |
798 | GNUNET_PSYC_MessageCallback message_cb, | 882 | GNUNET_PSYC_MessageCallback message_cb, |
883 | GNUNET_PSYC_MessagePartCallback message_part_cb, | ||
799 | GNUNET_PSYC_FinishCallback finish_cb, | 884 | GNUNET_PSYC_FinishCallback finish_cb, |
800 | void *cls) | 885 | void *cls) |
801 | { | 886 | { |