- add underlay api implementation
[oweals/gnunet.git] / src / conversation / conversation_api.c
index 9dfc6e9136d2fc7300b99a7481322b378bae06c8..24cd52eb275ff8b2ff40df4dc890beb9cc4147a7 100644 (file)
@@ -117,7 +117,7 @@ struct GNUNET_CONVERSATION_Caller
   struct GNUNET_NAMESTORE_QueueEntry *qe;
 
   /**
-   * Identity of the person calling us (valid while in state #PS_RINGING).
+   * Identity of the person calling us.
    */
   struct GNUNET_CRYPTO_EcdsaPublicKey caller_id;
 
@@ -261,7 +261,7 @@ handle_caller_name (void *cls,
   if (NULL == label)
     name = GNUNET_strdup (GNUNET_GNSRECORD_pkey_to_zkey (&caller->caller_id));
   else
-    GNUNET_asprintf (&name, "%.gnu", label);
+    GNUNET_asprintf (&name, "%s.gnu", label);
   caller->caller_id_str = name;
   caller->state = CS_RINGING;
   phone->event_handler (phone->event_handler_cls,
@@ -355,11 +355,20 @@ handle_phone_hangup (void *cls,
     if (hang->cid == caller->cid)
       break;
   if (NULL == caller)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Received HANG_UP message for unknown caller ID %u\n",
+                (unsigned int) hang->cid);
     return;
+  }
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Received HANG_UP message, terminating call with `%s'\n",
+              caller->caller_id_str);
   switch (caller->state)
   {
   case CS_RESOLVE:
+    /* application doesn't even know about call yet */
     GNUNET_NAMESTORE_cancel (caller->qe);
     caller->qe = NULL;
     break;
@@ -544,14 +553,40 @@ phone_error_handler (void *cls,
 {
   struct GNUNET_CONVERSATION_Phone *phone = cls;
 
-  GNUNET_break (0);
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-              _("Internal MQ error %d\n"),
-              error);
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              _("Connection to conversation service lost, trying to reconnect\n"));
   reconnect_phone (phone);
 }
 
 
+/**
+ * Clean up all callers of the given phone.
+ *
+ * @param phone phone to clean up callers for
+ */
+static void
+clean_up_callers (struct GNUNET_CONVERSATION_Phone *phone)
+{
+  struct GNUNET_CONVERSATION_Caller *caller;
+
+  while (NULL != (caller = phone->caller_head))
+  {
+    /* make sure mic/speaker are disabled *before* callback */
+    if (CS_ACTIVE == caller->state)
+    {
+      caller->speaker->disable_speaker (caller->speaker->cls);
+      caller->mic->disable_microphone (caller->mic->cls);
+      caller->state = CS_CALLER_SUSPENDED;
+    }
+    phone->event_handler (phone->event_handler_cls,
+                          GNUNET_CONVERSATION_EC_PHONE_HUNG_UP,
+                          caller,
+                          caller->caller_id_str);
+    GNUNET_CONVERSATION_caller_hang_up (caller);
+  }
+}
+
+
 /**
  * The phone got disconnected, reconnect to the service.
  *
@@ -581,16 +616,8 @@ reconnect_phone (struct GNUNET_CONVERSATION_Phone *phone)
   };
   struct GNUNET_MQ_Envelope *e;
   struct ClientPhoneRegisterMessage *reg;
-  struct GNUNET_CONVERSATION_Caller *caller;
 
-  while (NULL != (caller = phone->caller_head))
-  {
-    phone->event_handler (phone->event_handler_cls,
-                          GNUNET_CONVERSATION_EC_PHONE_HUNG_UP,
-                          caller,
-                          caller->caller_id_str);
-    GNUNET_CONVERSATION_caller_hang_up (caller);
-  }
+  clean_up_callers (phone);
   if (NULL != phone->mq)
   {
     GNUNET_MQ_destroy (phone->mq);
@@ -624,6 +651,7 @@ reconnect_phone (struct GNUNET_CONVERSATION_Phone *phone)
  * @param ego ego to use for name resolution (when determining caller ID)
  * @param event_handler how to notify the owner of the phone about events
  * @param event_handler_cls closure for @a event_handler
+ * @return NULL on error (no valid line configured)
  */
 struct GNUNET_CONVERSATION_Phone *
 GNUNET_CONVERSATION_phone_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
@@ -640,6 +668,8 @@ GNUNET_CONVERSATION_phone_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                              "LINE",
                                              &line))
     return NULL;
+  if (line >= (1 << 31))
+    return NULL;
   phone = GNUNET_new (struct GNUNET_CONVERSATION_Phone);
   if (GNUNET_OK !=
       GNUNET_CRYPTO_get_peer_identity (cfg,
@@ -756,7 +786,8 @@ GNUNET_CONVERSATION_caller_hang_up (struct GNUNET_CONVERSATION_Caller *caller)
                                caller);
   GNUNET_free_non_null (caller->caller_id_str);
   GNUNET_free (caller);
-  e = GNUNET_MQ_msg (hang, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
+  e = GNUNET_MQ_msg (hang,
+                     GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP);
   GNUNET_MQ_send (phone->mq, e);
 }
 
@@ -769,16 +800,7 @@ GNUNET_CONVERSATION_caller_hang_up (struct GNUNET_CONVERSATION_Caller *caller)
 void
 GNUNET_CONVERSATION_phone_destroy (struct GNUNET_CONVERSATION_Phone *phone)
 {
-  struct GNUNET_CONVERSATION_Caller *caller;
-
-  while (NULL != (caller = phone->caller_head))
-  {
-    phone->event_handler (phone->event_handler_cls,
-                          GNUNET_CONVERSATION_EC_PHONE_HUNG_UP,
-                          caller,
-                          caller->caller_id_str);
-    GNUNET_CONVERSATION_caller_hang_up (caller);
-  }
+  clean_up_callers (phone);
   if (NULL != phone->ns)
   {
     GNUNET_NAMESTORE_disconnect (phone->ns);
@@ -803,19 +825,38 @@ GNUNET_CONVERSATION_phone_destroy (struct GNUNET_CONVERSATION_Phone *phone)
  * and the microphone.  The call can later be resumed with
  * #GNUNET_CONVERSATION_caller_resume.
  *
- * @param phone phone to pause
+ * @param caller call to suspend
  */
 void
 GNUNET_CONVERSATION_caller_suspend (struct GNUNET_CONVERSATION_Caller *caller)
 {
-  GNUNET_break (0);
+  struct GNUNET_CONVERSATION_Phone *phone = caller->phone;
+  struct GNUNET_MQ_Envelope *e;
+  struct ClientPhoneSuspendMessage *suspend;
+
+  GNUNET_assert ( (CS_ACTIVE == caller->state) ||
+                  (CS_CALLER_SUSPENDED == caller->state) );
+  if (CS_ACTIVE == caller->state)
+  {
+    caller->speaker->disable_speaker (caller->speaker->cls);
+    caller->mic->disable_microphone (caller->mic->cls);
+  }
+  caller->speaker = NULL;
+  caller->mic = NULL;
+  e = GNUNET_MQ_msg (suspend, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND);
+  suspend->cid = caller->cid;
+  GNUNET_MQ_send (phone->mq, e);
+  if (CS_ACTIVE == caller->state)
+    caller->state = CS_CALLEE_SUSPENDED;
+  else
+    caller->state = CS_BOTH_SUSPENDED;
 }
 
 
 /**
  * Resume suspended conversation of a phone.
  *
- * @param phone phone to resume
+ * @param caller call to resume
  * @param speaker speaker to use
  * @param mic microphone to use
  */
@@ -824,7 +865,29 @@ GNUNET_CONVERSATION_caller_resume (struct GNUNET_CONVERSATION_Caller *caller,
                                    struct GNUNET_SPEAKER_Handle *speaker,
                                    struct GNUNET_MICROPHONE_Handle *mic)
 {
-  GNUNET_break (0);
+  struct GNUNET_CONVERSATION_Phone *phone = caller->phone;
+  struct GNUNET_MQ_Envelope *e;
+  struct ClientPhoneResumeMessage *resume;
+
+  GNUNET_assert ( (CS_CALLEE_SUSPENDED == caller->state) ||
+                  (CS_BOTH_SUSPENDED == caller->state) );
+  caller->speaker = speaker;
+  caller->mic = mic;
+  e = GNUNET_MQ_msg (resume, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME);
+  resume->cid = caller->cid;
+  GNUNET_MQ_send (phone->mq, e);
+  if (CS_CALLEE_SUSPENDED == caller->state)
+  {
+    caller->state = CS_ACTIVE;
+    caller->speaker->enable_speaker (caller->speaker->cls);
+    caller->mic->enable_microphone (caller->mic->cls,
+                                    &transmit_phone_audio,
+                                    caller);
+  }
+  else
+  {
+    caller->state = CS_CALLER_SUSPENDED;
+  }
 }
 
 /* end of conversation_api.c */