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_MINUTES, 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);
209 destroy_speaker (void *cls)
211 const char *origin = CLS_STR (cls);
213 fprintf (stderr, "Speaker %s destroyed\n", origin);
217 static struct GNUNET_SPEAKER_Handle call1_speaker = {
226 static struct GNUNET_SPEAKER_Handle call2_speaker = {
235 static struct GNUNET_SPEAKER_Handle phone_speaker = {
245 enable_mic (void *cls,
246 GNUNET_MICROPHONE_RecordedDataCallback rdc,
249 const char *origin = CLS_STR (cls);
250 struct MicContext *mc;
258 phone_rdc_cls = rdc_cls;
259 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == phone_task);
260 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
263 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
264 mc->sym = (CALLER1 == cls) ? '1': '2';
266 mc->rdc_cls = rdc_cls;
267 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == mc->call_task);
268 mc->call_task = GNUNET_SCHEDULER_add_now (&call_send, mc);
274 disable_mic (void *cls)
276 const char *origin = CLS_STR (cls);
277 struct MicContext *mc;
285 phone_rdc_cls = NULL;
286 GNUNET_SCHEDULER_cancel (phone_task);
287 phone_task = GNUNET_SCHEDULER_NO_TASK;
290 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
293 GNUNET_SCHEDULER_cancel (mc->call_task);
294 mc->call_task = GNUNET_SCHEDULER_NO_TASK;
299 destroy_mic (void *cls)
301 const char *origin = CLS_STR (cls);
304 "Mic %s destroyed\n",
309 static struct GNUNET_MICROPHONE_Handle call1_mic = {
317 static struct GNUNET_MICROPHONE_Handle call2_mic = {
325 static struct GNUNET_MICROPHONE_Handle phone_mic = {
334 * Signature of the main function of a task.
337 * @param tc context information (why was this task triggered now)
340 end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
342 GNUNET_SCHEDULER_shutdown ();
345 GNUNET_IDENTITY_cancel (op);
350 GNUNET_CONVERSATION_call_stop (call1);
355 GNUNET_CONVERSATION_call_stop (call2);
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
361 GNUNET_CONVERSATION_phone_destroy (phone);
366 GNUNET_IDENTITY_disconnect (id);
371 GNUNET_NAMESTORE_cancel (qe);
376 GNUNET_NAMESTORE_disconnect (ns);
383 caller_event_handler (void *cls,
384 enum GNUNET_CONVERSATION_CallerEventCode code)
388 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
389 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
390 fprintf (stderr, "Unexpected caller code: %d\n", code);
397 phone_event_handler (void *cls,
398 enum GNUNET_CONVERSATION_PhoneEventCode code,
399 struct GNUNET_CONVERSATION_Caller *caller,
400 const char *caller_id)
404 GNUNET_break (0 == strcmp (caller_id,
408 case GNUNET_CONVERSATION_EC_PHONE_RING:
409 if (NULL == active_caller1)
411 active_caller1 = caller;
413 GNUNET_CONVERSATION_caller_pick_up (caller,
414 &caller_event_handler,
421 GNUNET_CONVERSATION_caller_suspend (active_caller1);
422 active_caller2 = caller;
424 GNUNET_CONVERSATION_caller_pick_up (caller,
425 &caller_event_handler,
431 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
432 if (caller == active_caller2)
434 active_caller2 = NULL;
435 GNUNET_CONVERSATION_caller_resume (active_caller1,
439 else if (caller == active_caller1)
441 active_caller1 = NULL;
442 GNUNET_break (NULL == active_caller2);
444 GNUNET_SCHEDULER_shutdown ();
448 fprintf (stderr, "Unexpected phone code: %d\n", code);
455 call_event_handler (void *cls,
456 enum GNUNET_CONVERSATION_CallEventCode code)
458 const char *cid = cls;
462 case GNUNET_CONVERSATION_EC_CALL_RINGING:
464 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
465 fprintf (stderr, "\t Call %s picked\n", cid);
467 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
468 fprintf (stderr, "\t Call %s GNS lookup failed \n", cid);
469 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
470 fprintf (stderr, "\t Call %s hungup\n", cid);
471 if (0 == strcmp (cid, "call1"))
476 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
477 fprintf (stderr, "\t Call %s suspended\n", cid);
479 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
480 fprintf (stderr, "\t Call %s resumed\n", cid);
487 caller_ego_create_cont (void *cls,
491 GNUNET_assert (NULL == emsg);
496 namestore_put_cont (void *cls,
501 GNUNET_assert (GNUNET_YES == success);
502 GNUNET_assert (NULL == emsg);
503 GNUNET_assert (NULL == op);
504 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
509 identity_cb (void *cls,
510 struct GNUNET_IDENTITY_Ego *ego,
514 struct GNUNET_GNSRECORD_Data rd;
515 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
521 if (0 == strcmp (name, "phone-ego"))
523 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
524 GNUNET_asprintf (&gns_name,
526 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
527 phone = GNUNET_CONVERSATION_phone_create (cfg,
529 &phone_event_handler,
531 GNUNET_assert (NULL != phone);
532 memset (&rd, 0, sizeof (rd));
533 GNUNET_CONVERSATION_phone_get_record (phone,
535 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
536 rd.expiration_time = UINT64_MAX;
537 qe = GNUNET_NAMESTORE_records_store (ns,
538 GNUNET_IDENTITY_ego_get_private_key (ego),
539 "phone" /* GNS label */,
546 if (0 == strcmp (name, "caller-ego"))
548 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
549 GNUNET_asprintf (&gns_caller_id,
551 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
552 call1 = GNUNET_CONVERSATION_call_start (cfg,
559 call2 = GNUNET_CONVERSATION_call_start (cfg,
572 phone_ego_create_cont (void *cls,
576 GNUNET_assert (NULL == emsg);
582 const struct GNUNET_CONFIGURATION_Handle *c,
583 struct GNUNET_TESTING_Peer *peer)
586 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_test,
588 id = GNUNET_IDENTITY_connect (cfg,
591 op = GNUNET_IDENTITY_create (id, "phone-ego", &phone_ego_create_cont, NULL);
592 ns = GNUNET_NAMESTORE_connect (cfg);
597 main (int argc, char *argv[])
599 if (0 != GNUNET_TESTING_peer_run ("test_conversation_api_twocalls",
600 "test_conversation.conf",
606 /* end of test_conversation_api_twocalls.c */