2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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)
44 static const struct GNUNET_CONFIGURATION_Handle *cfg;
46 static struct GNUNET_IDENTITY_Handle *id;
48 static struct GNUNET_IDENTITY_Operation *op;
50 static struct GNUNET_CONVERSATION_Phone *phone;
52 static struct GNUNET_NAMESTORE_Handle *ns;
54 static struct GNUNET_CONVERSATION_Call *call1;
56 static struct GNUNET_CONVERSATION_Call *call2;
58 static struct GNUNET_NAMESTORE_QueueEntry *qe;
60 static struct GNUNET_CONVERSATION_Caller *active_caller1;
62 static struct GNUNET_CONVERSATION_Caller *active_caller2;
64 static char *gns_name;
66 static char *gns_caller_id;
68 static GNUNET_MICROPHONE_RecordedDataCallback phone_rdc;
70 static void *phone_rdc_cls;
72 static GNUNET_MICROPHONE_RecordedDataCallback call_rdc;
74 static void *call_rdc_cls;
76 static GNUNET_SCHEDULER_TaskIdentifier phone_task;
78 static GNUNET_SCHEDULER_TaskIdentifier call_task;
82 phone_send (void *cls,
83 const struct GNUNET_SCHEDULER_TaskContext *tc)
87 GNUNET_assert (NULL != phone_rdc);
88 GNUNET_snprintf (buf, sizeof (buf), "phone");
89 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
90 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
97 const struct GNUNET_SCHEDULER_TaskContext *tc)
101 GNUNET_assert (NULL != call_rdc);
102 GNUNET_snprintf (buf, sizeof (buf), "call");
103 call_rdc (call_rdc_cls, strlen (buf) + 1, buf);
104 call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
110 enable_speaker (void *cls)
112 const char *origin = cls;
115 "Speaker %s enabled\n",
122 disable_speaker (void *cls)
124 const char *origin = cls;
127 "Speaker %s disabled\n",
137 static unsigned int phone_i;
138 static unsigned int call_i;
140 if (0 == strncmp ("call", data, data_size))
142 else if (0 == strncmp ("phone", data, data_size))
147 "Received unexpected data %.*s\n",
149 (const char *) data);
152 if ( (20 < call_i) &&
156 /* time to hang up ... */
157 GNUNET_CONVERSATION_call_stop (call2);
163 if ( (20 < call_i) &&
167 /* time to hang up ... */
168 GNUNET_CONVERSATION_call_stop (call1);
175 destroy_speaker (void *cls)
177 const char *origin = cls;
179 fprintf (stderr, "Speaker %s destroyed\n", origin);
183 static struct GNUNET_SPEAKER_Handle call_speaker = {
192 static struct GNUNET_SPEAKER_Handle phone_speaker = {
202 enable_mic (void *cls,
203 GNUNET_MICROPHONE_RecordedDataCallback rdc,
206 const char *origin = cls;
211 if (0 == strcmp (origin, "phone"))
214 phone_rdc_cls = rdc_cls;
215 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == phone_task);
216 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
221 call_rdc_cls = rdc_cls;
222 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == call_task);
223 call_task = GNUNET_SCHEDULER_add_now (&call_send, NULL);
230 disable_mic (void *cls)
232 const char *origin = cls;
237 if (0 == strcmp (origin, "phone"))
240 phone_rdc_cls = NULL;
241 GNUNET_SCHEDULER_cancel (phone_task);
242 phone_task = GNUNET_SCHEDULER_NO_TASK;
248 GNUNET_SCHEDULER_cancel (call_task);
249 call_task = GNUNET_SCHEDULER_NO_TASK;
255 destroy_mic (void *cls)
257 const char *origin = cls;
260 "Mic %s destroyed\n",
265 static struct GNUNET_MICROPHONE_Handle call_mic = {
273 static struct GNUNET_MICROPHONE_Handle phone_mic = {
282 * Signature of the main function of a task.
285 * @param tc context information (why was this task triggered now)
288 end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
290 GNUNET_SCHEDULER_shutdown ();
293 GNUNET_IDENTITY_cancel (op);
298 GNUNET_CONVERSATION_call_stop (call1);
303 GNUNET_CONVERSATION_call_stop (call2);
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
309 GNUNET_CONVERSATION_phone_destroy (phone);
314 GNUNET_IDENTITY_disconnect (id);
319 GNUNET_NAMESTORE_cancel (qe);
324 GNUNET_NAMESTORE_disconnect (ns);
331 caller_event_handler (void *cls,
332 enum GNUNET_CONVERSATION_CallerEventCode code)
336 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
337 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
338 fprintf (stderr, "Unexpected caller code: %d\n", code);
345 phone_event_handler (void *cls,
346 enum GNUNET_CONVERSATION_PhoneEventCode code,
347 struct GNUNET_CONVERSATION_Caller *caller,
348 const char *caller_id)
352 GNUNET_break (0 == strcmp (caller_id,
356 case GNUNET_CONVERSATION_EC_PHONE_RING:
357 if (NULL == active_caller1)
359 active_caller1 = caller;
361 GNUNET_CONVERSATION_caller_pick_up (caller,
362 &caller_event_handler,
369 GNUNET_CONVERSATION_caller_suspend (active_caller1);
370 active_caller2 = caller;
372 GNUNET_CONVERSATION_caller_pick_up (caller,
373 &caller_event_handler,
379 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
380 if (caller == active_caller2)
382 active_caller2 = NULL;
383 GNUNET_CONVERSATION_caller_resume (active_caller1,
387 else if (caller == active_caller1)
389 active_caller1 = NULL;
390 GNUNET_break (NULL == active_caller2);
392 GNUNET_SCHEDULER_shutdown ();
396 fprintf (stderr, "Unexpected phone code: %d\n", code);
403 call_event_handler (void *cls,
404 enum GNUNET_CONVERSATION_CallEventCode code)
406 const char *cid = cls;
410 case GNUNET_CONVERSATION_EC_CALL_RINGING:
412 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
414 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
415 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
416 if (0 == strcmp (cid, "call1"))
421 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
422 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
429 caller_ego_create_cont (void *cls,
433 GNUNET_assert (NULL == emsg);
438 namestore_put_cont (void *cls,
443 GNUNET_assert (GNUNET_YES == success);
444 GNUNET_assert (NULL == emsg);
445 GNUNET_assert (NULL == op);
446 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
451 identity_cb (void *cls,
452 struct GNUNET_IDENTITY_Ego *ego,
456 struct GNUNET_GNSRECORD_Data rd;
457 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
463 if (0 == strcmp (name, "phone-ego"))
465 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
466 GNUNET_asprintf (&gns_name,
468 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
469 phone = GNUNET_CONVERSATION_phone_create (cfg,
471 &phone_event_handler,
473 GNUNET_assert (NULL != phone);
474 memset (&rd, 0, sizeof (rd));
475 GNUNET_CONVERSATION_phone_get_record (phone,
477 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
478 rd.expiration_time = UINT64_MAX;
479 qe = GNUNET_NAMESTORE_records_store (ns,
480 GNUNET_IDENTITY_ego_get_private_key (ego),
481 "phone" /* GNS label */,
488 if (0 == strcmp (name, "caller-ego"))
490 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
491 GNUNET_asprintf (&gns_caller_id,
493 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
494 call1 = GNUNET_CONVERSATION_call_start (cfg,
501 call2 = GNUNET_CONVERSATION_call_start (cfg,
514 phone_ego_create_cont (void *cls,
518 GNUNET_assert (NULL == emsg);
524 const struct GNUNET_CONFIGURATION_Handle *c,
525 struct GNUNET_TESTING_Peer *peer)
528 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test,
530 id = GNUNET_IDENTITY_connect (cfg,
533 op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
534 ns = GNUNET_NAMESTORE_connect (cfg);
539 main (int argc, char *argv[])
541 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api_twocalls",
542 "test_conversation.conf",
548 /* end of test_conversation_api_twocalls.c */