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_SCHEDULER_TaskIdentifier phone_task;
75 * Variable for recognizing caller1
77 static const char *caller1 = "caller1";
80 * Variable for recognizing caller2
82 static const char *caller2 = "caller2";
85 * Variable for recognizing callee
87 static const char *phone0 = "phone";
89 #define CALLER1 &caller1
90 #define CALLER2 &caller2
91 #define PHONE0 &phone0
93 #define CLS_STR(caller) (*((char **)caller))
97 GNUNET_MICROPHONE_RecordedDataCallback rdc;
101 GNUNET_SCHEDULER_TaskIdentifier call_task;
106 static struct MicContext call1_mic_ctx;
107 static struct MicContext call2_mic_ctx;
108 //static struct MicContext phone_mic_ctx;
112 phone_send (void *cls,
113 const struct GNUNET_SCHEDULER_TaskContext *tc)
117 GNUNET_assert (NULL != phone_rdc);
118 GNUNET_snprintf (buf, sizeof (buf), "phone");
119 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
120 fprintf (stderr, "+");
121 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
127 call_send (void *cls,
128 const struct GNUNET_SCHEDULER_TaskContext *tc)
130 struct MicContext *mc = cls;
133 GNUNET_assert (NULL != mc->rdc);
134 GNUNET_snprintf (buf, sizeof (buf), "call");
135 mc->rdc (mc->rdc_cls, strlen (buf) + 1, buf);
136 fprintf (stderr, "%c", mc->sym);
137 mc->call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
143 enable_speaker (void *cls)
145 const char *origin = CLS_STR (cls);
148 "Speaker %s enabled\n",
155 disable_speaker (void *cls)
157 const char *origin = CLS_STR (cls);
160 "Speaker %s disabled\n",
170 static unsigned int phone_i;
171 static unsigned int call_i;
173 write (2, data, data_size);
174 if (0 == strncmp ("call", data, data_size))
176 else if (0 == strncmp ("phone", data, data_size))
181 "Received unexpected data %.*s\n",
183 (const char *) data);
186 if ( (20 < call_i) &&
190 /* time to hang up ... */
191 GNUNET_CONVERSATION_call_stop (call2);
197 if ( (20 < call_i) &&
201 /* time to hang up ... */
202 GNUNET_CONVERSATION_call_stop (call1);
211 destroy_speaker (void *cls)
213 const char *origin = CLS_STR (cls);
215 fprintf (stderr, "Speaker %s destroyed\n", origin);
219 static struct GNUNET_SPEAKER_Handle call1_speaker = {
228 static struct GNUNET_SPEAKER_Handle call2_speaker = {
237 static struct GNUNET_SPEAKER_Handle phone_speaker = {
247 enable_mic (void *cls,
248 GNUNET_MICROPHONE_RecordedDataCallback rdc,
251 const char *origin = CLS_STR (cls);
252 struct MicContext *mc;
260 phone_rdc_cls = rdc_cls;
261 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == phone_task);
262 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
265 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
266 mc->sym = (CALLER1 == cls) ? '1': '2';
268 mc->rdc_cls = rdc_cls;
269 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == mc->call_task);
270 mc->call_task = GNUNET_SCHEDULER_add_now (&call_send, mc);
276 disable_mic (void *cls)
278 const char *origin = CLS_STR (cls);
279 struct MicContext *mc;
287 phone_rdc_cls = NULL;
288 GNUNET_SCHEDULER_cancel (phone_task);
289 phone_task = GNUNET_SCHEDULER_NO_TASK;
292 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
295 GNUNET_SCHEDULER_cancel (mc->call_task);
296 mc->call_task = GNUNET_SCHEDULER_NO_TASK;
301 destroy_mic (void *cls)
303 const char *origin = CLS_STR (cls);
306 "Mic %s destroyed\n",
311 static struct GNUNET_MICROPHONE_Handle call1_mic = {
319 static struct GNUNET_MICROPHONE_Handle call2_mic = {
327 static struct GNUNET_MICROPHONE_Handle phone_mic = {
336 * Signature of the main function of a task.
339 * @param tc context information (why was this task triggered now)
342 end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
344 GNUNET_SCHEDULER_shutdown ();
347 GNUNET_IDENTITY_cancel (op);
352 GNUNET_CONVERSATION_call_stop (call1);
357 GNUNET_CONVERSATION_call_stop (call2);
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
363 GNUNET_CONVERSATION_phone_destroy (phone);
368 GNUNET_IDENTITY_disconnect (id);
373 GNUNET_NAMESTORE_cancel (qe);
378 GNUNET_NAMESTORE_disconnect (ns);
385 caller_event_handler (void *cls,
386 enum GNUNET_CONVERSATION_CallerEventCode code)
390 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
391 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
392 fprintf (stderr, "Unexpected caller code: %d\n", code);
399 phone_event_handler (void *cls,
400 enum GNUNET_CONVERSATION_PhoneEventCode code,
401 struct GNUNET_CONVERSATION_Caller *caller,
402 const char *caller_id)
406 GNUNET_break (0 == strcmp (caller_id,
410 case GNUNET_CONVERSATION_EC_PHONE_RING:
411 if (NULL == active_caller1)
413 active_caller1 = caller;
415 GNUNET_CONVERSATION_caller_pick_up (caller,
416 &caller_event_handler,
423 GNUNET_CONVERSATION_caller_suspend (active_caller1);
424 active_caller2 = caller;
426 GNUNET_CONVERSATION_caller_pick_up (caller,
427 &caller_event_handler,
433 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
434 if (caller == active_caller2)
436 active_caller2 = NULL;
437 GNUNET_CONVERSATION_caller_resume (active_caller1,
441 else if (caller == active_caller1)
443 active_caller1 = NULL;
444 GNUNET_break (NULL == active_caller2);
446 GNUNET_SCHEDULER_shutdown ();
450 fprintf (stderr, "Unexpected phone code: %d\n", code);
457 call_event_handler (void *cls,
458 enum GNUNET_CONVERSATION_CallEventCode code)
460 const char *cid = cls;
464 case GNUNET_CONVERSATION_EC_CALL_RINGING:
466 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
467 fprintf (stderr, "\t Call %s picked\n", cid);
469 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
470 fprintf (stderr, "\t Call %s GNS lookup failed \n", cid);
471 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
472 fprintf (stderr, "\t Call %s hungup\n", cid);
473 if (0 == strcmp (cid, "call1"))
478 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
479 fprintf (stderr, "\t Call %s suspended\n", cid);
481 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
482 fprintf (stderr, "\t Call %s resumed\n", cid);
489 caller_ego_create_cont (void *cls,
493 GNUNET_assert (NULL == emsg);
498 namestore_put_cont (void *cls,
503 GNUNET_assert (GNUNET_YES == success);
504 GNUNET_assert (NULL == emsg);
505 GNUNET_assert (NULL == op);
506 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
511 identity_cb (void *cls,
512 struct GNUNET_IDENTITY_Ego *ego,
516 struct GNUNET_GNSRECORD_Data rd;
517 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
523 if (0 == strcmp (name, "phone-ego"))
525 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
526 GNUNET_asprintf (&gns_name,
528 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
529 phone = GNUNET_CONVERSATION_phone_create (cfg,
531 &phone_event_handler,
533 GNUNET_assert (NULL != phone);
534 memset (&rd, 0, sizeof (rd));
535 GNUNET_CONVERSATION_phone_get_record (phone,
537 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
538 rd.expiration_time = UINT64_MAX;
539 qe = GNUNET_NAMESTORE_records_store (ns,
540 GNUNET_IDENTITY_ego_get_private_key (ego),
541 "phone" /* GNS label */,
548 if (0 == strcmp (name, "caller-ego"))
550 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
551 GNUNET_asprintf (&gns_caller_id,
553 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
554 call1 = GNUNET_CONVERSATION_call_start (cfg,
561 call2 = GNUNET_CONVERSATION_call_start (cfg,
574 phone_ego_create_cont (void *cls,
578 GNUNET_assert (NULL == emsg);
584 const struct GNUNET_CONFIGURATION_Handle *c,
585 struct GNUNET_TESTING_Peer *peer)
588 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test,
590 id = GNUNET_IDENTITY_connect (cfg,
593 op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
594 ns = GNUNET_NAMESTORE_connect (cfg);
599 main (int argc, char *argv[])
601 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api_twocalls",
602 "test_conversation.conf",
608 /* end of test_conversation_api_twocalls.c */