2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file conversation/test_conversation_api_twocalls.c
22 * @brief testcase for conversation_api.c
24 * This test performs the operations of TWO calls made to a phone
25 * where the phone user picks up one, suspends it, picks up the
26 * second one; eventually, the initiator hangs up, the callee
27 * resumes the first call, and then the initiator hangs up the
31 #include "gnunet_util_lib.h"
32 #include "gnunet_testing_lib.h"
33 #include "gnunet_gnsrecord_lib.h"
34 #include "gnunet_conversation_service.h"
35 #include "gnunet_identity_service.h"
36 #include "gnunet_namestore_service.h"
38 #define FREQ GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 25)
42 #define LOG(kind,...) \
43 GNUNET_log (kind, __VA_ARGS__)
45 #define LOG_DEBUG(...) \
46 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
48 static const struct GNUNET_CONFIGURATION_Handle *cfg;
50 static struct GNUNET_IDENTITY_Handle *id;
52 static struct GNUNET_IDENTITY_Operation *op;
54 static struct GNUNET_CONVERSATION_Phone *phone;
56 static struct GNUNET_NAMESTORE_Handle *ns;
58 static struct GNUNET_CONVERSATION_Call *call1;
60 static struct GNUNET_CONVERSATION_Call *call2;
62 static struct GNUNET_NAMESTORE_QueueEntry *qe;
64 static struct GNUNET_CONVERSATION_Caller *active_caller1;
66 static struct GNUNET_CONVERSATION_Caller *active_caller2;
68 static char *gns_name;
70 static char *gns_caller_id;
72 static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
74 static void *phone_rdc_cls;
76 static struct GNUNET_SCHEDULER_Task * phone_task;
79 * Variable for recognizing caller1
81 static const char *caller1 = "caller1";
84 * Variable for recognizing caller2
86 static const char *caller2 = "caller2";
89 * Variable for recognizing callee
91 static const char *phone0 = "phone";
94 #define CALLER1 &caller1
95 #define CALLER2 &caller2
96 #define PHONE0 &phone0
98 #define CLS_STR(caller) (*((char **)caller))
102 * Did caller1 call finish successfully
104 static int call1_finished;
107 * Did caller2 call finish successfully
109 static int call2_finished;
113 GNUNET_MICROPHONE_RecordedDataCallback rdc;
117 struct GNUNET_SCHEDULER_Task * call_task;
121 static struct MicContext call1_mic_ctx;
122 static struct MicContext call2_mic_ctx;
123 //static struct MicContext phone_mic_ctx;
127 phone_send (void *cls)
131 GNUNET_assert (NULL != phone_rdc);
132 GNUNET_snprintf (buf, sizeof (buf), "phone");
133 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
134 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
140 call_send (void *cls)
142 struct MicContext *mc = cls;
145 GNUNET_assert (NULL != mc->rdc);
146 GNUNET_snprintf (buf, sizeof (buf), "call");
147 mc->rdc (mc->rdc_cls, strlen (buf) + 1, buf);
148 mc->call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
154 enable_speaker (void *cls)
156 const char *origin = CLS_STR (cls);
158 LOG_DEBUG ("Speaker %s enabled\n",
165 disable_speaker (void *cls)
167 const char *origin = CLS_STR (cls);
169 LOG_DEBUG ("Speaker %s disabled\n",
179 static unsigned int phone_i;
180 static unsigned int call_i;
182 if (0 == strncmp ("call", data, data_size))
184 else if (0 == strncmp ("phone", data, data_size))
188 LOG_DEBUG ("Received unexpected data %.*s\n",
190 (const char *) data);
193 if ( (20 < call_i) &&
197 /* time to hang up ... */
198 GNUNET_CONVERSATION_call_stop (call2);
203 call2_finished = GNUNET_YES;
205 if ( (20 < call_i) &&
209 /* time to hang up ... */
210 GNUNET_CONVERSATION_call_stop (call1);
214 call1_finished = GNUNET_YES;
220 destroy_speaker (void *cls)
222 const char *origin = CLS_STR (cls);
224 LOG_DEBUG ("Speaker %s destroyed\n", origin);
228 static struct GNUNET_SPEAKER_Handle call1_speaker = {
237 static struct GNUNET_SPEAKER_Handle call2_speaker = {
246 static struct GNUNET_SPEAKER_Handle phone_speaker = {
256 enable_mic (void *cls,
257 GNUNET_MICROPHONE_RecordedDataCallback rdc,
260 const char *origin = CLS_STR (cls);
261 struct MicContext *mc;
263 LOG_DEBUG ("Mic %s enabled\n",
268 phone_rdc_cls = rdc_cls;
269 GNUNET_break (NULL == phone_task);
270 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
273 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
275 mc->rdc_cls = rdc_cls;
276 GNUNET_break (NULL == mc->call_task);
277 mc->call_task = GNUNET_SCHEDULER_add_now (&call_send, mc);
283 disable_mic (void *cls)
285 const char *origin = CLS_STR (cls);
286 struct MicContext *mc;
288 LOG_DEBUG ("Mic %s disabled\n",
293 phone_rdc_cls = NULL;
294 GNUNET_SCHEDULER_cancel (phone_task);
298 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
301 GNUNET_SCHEDULER_cancel (mc->call_task);
302 mc->call_task = NULL;
307 destroy_mic (void *cls)
309 const char *origin = CLS_STR (cls);
311 LOG_DEBUG ("Mic %s destroyed\n",
316 static struct GNUNET_MICROPHONE_Handle call1_mic = {
324 static struct GNUNET_MICROPHONE_Handle call2_mic = {
332 static struct GNUNET_MICROPHONE_Handle phone_mic = {
341 * Signature of the main function of a task.
348 GNUNET_SCHEDULER_shutdown ();
351 GNUNET_IDENTITY_cancel (op);
356 GNUNET_CONVERSATION_call_stop (call1);
361 GNUNET_CONVERSATION_call_stop (call2);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
367 GNUNET_CONVERSATION_phone_destroy (phone);
372 GNUNET_IDENTITY_disconnect (id);
377 GNUNET_NAMESTORE_cancel (qe);
382 GNUNET_NAMESTORE_disconnect (ns);
389 caller_event_handler (void *cls,
390 enum GNUNET_CONVERSATION_CallerEventCode code)
394 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
395 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
396 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected caller code: %d\n", code);
403 phone_event_handler (void *cls,
404 enum GNUNET_CONVERSATION_PhoneEventCode code,
405 struct GNUNET_CONVERSATION_Caller *caller,
406 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
412 case GNUNET_CONVERSATION_EC_PHONE_RING:
413 if (NULL == active_caller1)
415 active_caller1 = caller;
417 GNUNET_CONVERSATION_caller_pick_up (caller,
418 &caller_event_handler,
425 GNUNET_CONVERSATION_caller_suspend (active_caller1);
426 active_caller2 = caller;
428 GNUNET_CONVERSATION_caller_pick_up (caller,
429 &caller_event_handler,
435 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
436 if (caller == active_caller2)
438 active_caller2 = NULL;
439 GNUNET_CONVERSATION_caller_resume (active_caller1,
443 else if (caller == active_caller1)
445 active_caller1 = NULL;
446 GNUNET_break (NULL == active_caller2);
447 GNUNET_SCHEDULER_shutdown ();
451 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected phone code: %d\n", code);
458 call_event_handler (void *cls,
459 enum GNUNET_CONVERSATION_CallEventCode code)
461 const char *cid = cls;
465 case GNUNET_CONVERSATION_EC_CALL_RINGING:
467 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
468 LOG_DEBUG ("Call %s picked\n", cid);
470 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
471 LOG_DEBUG ("Call %s GNS lookup failed \n", cid);
473 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
474 LOG_DEBUG ("Call %s hungup\n", cid);
475 if (0 == strcmp (cid, "call1"))
480 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
481 LOG_DEBUG ("Call %s suspended\n", cid);
483 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
484 LOG_DEBUG ("Call %s resumed\n", cid);
486 case GNUNET_CONVERSATION_EC_CALL_ERROR:
494 caller_ego_create_cont (void *cls,
498 GNUNET_assert (NULL == emsg);
503 namestore_put_cont (void *cls,
508 GNUNET_assert (GNUNET_YES == success);
509 GNUNET_assert (NULL == emsg);
510 GNUNET_assert (NULL == op);
511 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
516 identity_cb (void *cls,
517 struct GNUNET_IDENTITY_Ego *ego,
521 struct GNUNET_GNSRECORD_Data rd;
522 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
528 if (0 == strcmp (name, "phone-ego"))
530 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
531 GNUNET_asprintf (&gns_name,
533 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
534 phone = GNUNET_CONVERSATION_phone_create (cfg,
536 &phone_event_handler,
538 GNUNET_assert (NULL != phone);
539 memset (&rd, 0, sizeof (rd));
540 GNUNET_CONVERSATION_phone_get_record (phone,
542 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
543 rd.expiration_time = UINT64_MAX;
544 qe = GNUNET_NAMESTORE_records_store (ns,
545 GNUNET_IDENTITY_ego_get_private_key (ego),
546 "phone" /* GNS label */,
553 if (0 == strcmp (name, "caller-ego"))
555 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
556 GNUNET_asprintf (&gns_caller_id,
558 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
559 call1 = GNUNET_CONVERSATION_call_start (cfg,
567 call2 = GNUNET_CONVERSATION_call_start (cfg,
581 phone_ego_create_cont (void *cls,
585 GNUNET_assert (NULL == emsg);
591 const struct GNUNET_CONFIGURATION_Handle *c,
592 struct GNUNET_TESTING_Peer *peer)
595 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test,
597 id = GNUNET_IDENTITY_connect (cfg,
600 op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
601 ns = GNUNET_NAMESTORE_connect (cfg);
606 main (int argc, char *argv[])
609 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api_twocalls",
610 "test_conversation.conf",
613 if (call1_finished && call2_finished)
618 /* end of test_conversation_api_twocalls.c */