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)
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 GNUNET_SCHEDULER_TaskIdentifier 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 GNUNET_SCHEDULER_TaskIdentifier 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,
128 const struct GNUNET_SCHEDULER_TaskContext *tc)
132 GNUNET_assert (NULL != phone_rdc);
133 GNUNET_snprintf (buf, sizeof (buf), "phone");
134 phone_rdc (phone_rdc_cls, strlen (buf) + 1, buf);
135 phone_task = GNUNET_SCHEDULER_add_delayed (FREQ,
141 call_send (void *cls,
142 const struct GNUNET_SCHEDULER_TaskContext *tc)
144 struct MicContext *mc = cls;
147 GNUNET_assert (NULL != mc->rdc);
148 GNUNET_snprintf (buf, sizeof (buf), "call");
149 mc->rdc (mc->rdc_cls, strlen (buf) + 1, buf);
150 mc->call_task = GNUNET_SCHEDULER_add_delayed (FREQ,
156 enable_speaker (void *cls)
158 const char *origin = CLS_STR (cls);
160 LOG_DEBUG ("Speaker %s enabled\n",
167 disable_speaker (void *cls)
169 const char *origin = CLS_STR (cls);
171 LOG_DEBUG ("Speaker %s disabled\n",
181 static unsigned int phone_i;
182 static unsigned int call_i;
184 if (0 == strncmp ("call", data, data_size))
186 else if (0 == strncmp ("phone", data, data_size))
190 LOG_DEBUG ("Received unexpected data %.*s\n",
192 (const char *) data);
195 if ( (20 < call_i) &&
199 /* time to hang up ... */
200 GNUNET_CONVERSATION_call_stop (call2);
205 call2_finished = GNUNET_YES;
207 if ( (20 < call_i) &&
211 /* time to hang up ... */
212 GNUNET_CONVERSATION_call_stop (call1);
216 call1_finished = GNUNET_YES;
222 destroy_speaker (void *cls)
224 const char *origin = CLS_STR (cls);
226 LOG_DEBUG ("Speaker %s destroyed\n", origin);
230 static struct GNUNET_SPEAKER_Handle call1_speaker = {
239 static struct GNUNET_SPEAKER_Handle call2_speaker = {
248 static struct GNUNET_SPEAKER_Handle phone_speaker = {
258 enable_mic (void *cls,
259 GNUNET_MICROPHONE_RecordedDataCallback rdc,
262 const char *origin = CLS_STR (cls);
263 struct MicContext *mc;
265 LOG_DEBUG ("Mic %s enabled\n",
270 phone_rdc_cls = rdc_cls;
271 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == phone_task);
272 phone_task = GNUNET_SCHEDULER_add_now (&phone_send, NULL);
275 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
277 mc->rdc_cls = rdc_cls;
278 GNUNET_break (GNUNET_SCHEDULER_NO_TASK == mc->call_task);
279 mc->call_task = GNUNET_SCHEDULER_add_now (&call_send, mc);
285 disable_mic (void *cls)
287 const char *origin = CLS_STR (cls);
288 struct MicContext *mc;
290 LOG_DEBUG ("Mic %s disabled\n",
295 phone_rdc_cls = NULL;
296 GNUNET_SCHEDULER_cancel (phone_task);
297 phone_task = GNUNET_SCHEDULER_NO_TASK;
300 mc = (CALLER1 == cls) ? &call1_mic_ctx : &call2_mic_ctx;
303 GNUNET_SCHEDULER_cancel (mc->call_task);
304 mc->call_task = GNUNET_SCHEDULER_NO_TASK;
309 destroy_mic (void *cls)
311 const char *origin = CLS_STR (cls);
313 LOG_DEBUG ("Mic %s destroyed\n",
318 static struct GNUNET_MICROPHONE_Handle call1_mic = {
326 static struct GNUNET_MICROPHONE_Handle call2_mic = {
334 static struct GNUNET_MICROPHONE_Handle phone_mic = {
343 * Signature of the main function of a task.
346 * @param tc context information (why was this task triggered now)
349 end_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
351 GNUNET_SCHEDULER_shutdown ();
354 GNUNET_IDENTITY_cancel (op);
359 GNUNET_CONVERSATION_call_stop (call1);
364 GNUNET_CONVERSATION_call_stop (call2);
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from PHONE service.\n");
370 GNUNET_CONVERSATION_phone_destroy (phone);
375 GNUNET_IDENTITY_disconnect (id);
380 GNUNET_NAMESTORE_cancel (qe);
385 GNUNET_NAMESTORE_disconnect (ns);
392 caller_event_handler (void *cls,
393 enum GNUNET_CONVERSATION_CallerEventCode code)
397 case GNUNET_CONVERSATION_EC_CALLER_SUSPEND:
398 case GNUNET_CONVERSATION_EC_CALLER_RESUME:
399 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected caller code: %d\n", code);
406 phone_event_handler (void *cls,
407 enum GNUNET_CONVERSATION_PhoneEventCode code,
408 struct GNUNET_CONVERSATION_Caller *caller,
409 const struct GNUNET_CRYPTO_EcdsaPublicKey *caller_id)
415 case GNUNET_CONVERSATION_EC_PHONE_RING:
416 if (NULL == active_caller1)
418 active_caller1 = caller;
420 GNUNET_CONVERSATION_caller_pick_up (caller,
421 &caller_event_handler,
428 GNUNET_CONVERSATION_caller_suspend (active_caller1);
429 active_caller2 = caller;
431 GNUNET_CONVERSATION_caller_pick_up (caller,
432 &caller_event_handler,
438 case GNUNET_CONVERSATION_EC_PHONE_HUNG_UP:
439 if (caller == active_caller2)
441 active_caller2 = NULL;
442 GNUNET_CONVERSATION_caller_resume (active_caller1,
446 else if (caller == active_caller1)
448 active_caller1 = NULL;
449 GNUNET_break (NULL == active_caller2);
450 GNUNET_SCHEDULER_shutdown ();
454 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected phone code: %d\n", code);
461 call_event_handler (void *cls,
462 enum GNUNET_CONVERSATION_CallEventCode code)
464 const char *cid = cls;
468 case GNUNET_CONVERSATION_EC_CALL_RINGING:
470 case GNUNET_CONVERSATION_EC_CALL_PICKED_UP:
471 LOG_DEBUG ("Call %s picked\n", cid);
473 case GNUNET_CONVERSATION_EC_CALL_GNS_FAIL:
474 LOG_DEBUG ("Call %s GNS lookup failed \n", cid);
475 case GNUNET_CONVERSATION_EC_CALL_HUNG_UP:
476 LOG_DEBUG ("Call %s hungup\n", cid);
477 if (0 == strcmp (cid, "call1"))
482 case GNUNET_CONVERSATION_EC_CALL_SUSPENDED:
483 LOG_DEBUG ("Call %s suspended\n", cid);
485 case GNUNET_CONVERSATION_EC_CALL_RESUMED:
486 LOG_DEBUG ("Call %s resumed\n", cid);
488 case GNUNET_CONVERSATION_EC_CALL_ERROR:
496 caller_ego_create_cont (void *cls,
500 GNUNET_assert (NULL == emsg);
505 namestore_put_cont (void *cls,
510 GNUNET_assert (GNUNET_YES == success);
511 GNUNET_assert (NULL == emsg);
512 GNUNET_assert (NULL == op);
513 op = GNUNET_IDENTITY_create (id, "caller-ego", &caller_ego_create_cont, NULL);
518 identity_cb (void *cls,
519 struct GNUNET_IDENTITY_Ego *ego,
523 struct GNUNET_GNSRECORD_Data rd;
524 struct GNUNET_CRYPTO_EcdsaPublicKey pub;
530 if (0 == strcmp (name, "phone-ego"))
532 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
533 GNUNET_asprintf (&gns_name,
535 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
536 phone = GNUNET_CONVERSATION_phone_create (cfg,
538 &phone_event_handler,
540 GNUNET_assert (NULL != phone);
541 memset (&rd, 0, sizeof (rd));
542 GNUNET_CONVERSATION_phone_get_record (phone,
544 GNUNET_assert (rd.record_type == GNUNET_GNSRECORD_TYPE_PHONE);
545 rd.expiration_time = UINT64_MAX;
546 qe = GNUNET_NAMESTORE_records_store (ns,
547 GNUNET_IDENTITY_ego_get_private_key (ego),
548 "phone" /* GNS label */,
555 if (0 == strcmp (name, "caller-ego"))
557 GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
558 GNUNET_asprintf (&gns_caller_id,
560 GNUNET_GNSRECORD_pkey_to_zkey (&pub));
561 call1 = GNUNET_CONVERSATION_call_start (cfg,
568 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 */