-rps: merge duplicate functions
[oweals/gnunet.git] / src / conversation / conversation_api.c
index aad272d9c72b6a99523fca8fae9fa66402981e54..f28e71a98a5e90da9cbe3395963baf98eda130bb 100644 (file)
@@ -1,6 +1,6 @@
 /*
   This file is part of GNUnet
-  (C) 2013 Christian Grothoff (and other contributing authors)
+  Copyright (C) 2013, 2014 GNUnet e.V.
 
   GNUnet is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
   You should have received a copy of the GNU General Public License
   along with GNUnet; see the file COPYING.  If not, write to the
-  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-  Boston, MA 02111-1307, USA.
+  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+  Boston, MA 02110-1301, USA.
  */
 
 /**
  */
 enum CallerState
 {
-  /**
-   * We still need to reverse lookup the caller ID.
-   */
-  CS_RESOLVE,
-
   /**
    * The phone is ringing (user knows about incoming call).
    */
@@ -111,21 +106,11 @@ struct GNUNET_CONVERSATION_Caller
    */
   struct GNUNET_MICROPHONE_Handle *mic;
 
-  /**
-   * Active NAMESTORE lookup (or NULL).
-   */
-  struct GNUNET_NAMESTORE_QueueEntry *qe;
-
   /**
    * Identity of the person calling us.
    */
   struct GNUNET_CRYPTO_EcdsaPublicKey caller_id;
 
-  /**
-   * Caller ID of the person calling us as a string.
-   */
-  char *caller_id_str;
-
   /**
    * Internal handle to identify the caller with the service.
    */
@@ -237,40 +222,6 @@ static void
 reconnect_phone (struct GNUNET_CONVERSATION_Phone *phone);
 
 
-/**
- * We have resolved the caller ID using our name service.
- *
- * @param cls the `struct GNUNET_CONVERSATION_Caller`
- * @param zone our zone used for resolution
- * @param label name of the caller
- * @param rd_count number of records we have in @a rd
- * @param rd records we have for the caller's label
- */
-static void
-handle_caller_name (void *cls,
-                    const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
-                    const char *label,
-                    unsigned int rd_count,
-                    const struct GNUNET_GNSRECORD_Data *rd)
-{
-  struct GNUNET_CONVERSATION_Caller *caller = cls;
-  struct GNUNET_CONVERSATION_Phone *phone = caller->phone;
-  char *name;
-
-  caller->qe = NULL;
-  if (NULL == label)
-    name = GNUNET_strdup (GNUNET_GNSRECORD_pkey_to_zkey (&caller->caller_id));
-  else
-    GNUNET_asprintf (&name, "%.gnu", label);
-  caller->caller_id_str = name;
-  caller->state = CS_RINGING;
-  phone->event_handler (phone->event_handler_cls,
-                        GNUNET_CONVERSATION_EC_PHONE_RING,
-                        caller,
-                        name);
-}
-
-
 /**
  * Process recorded audio data.
  *
@@ -323,14 +274,13 @@ handle_phone_ring (void *cls,
     GNUNET_CONTAINER_DLL_insert (phone->caller_head,
                                  phone->caller_tail,
                                  caller);
-    caller->state = CS_RESOLVE;
     caller->caller_id = ring->caller_id;
     caller->cid = ring->cid;
-    caller->qe = GNUNET_NAMESTORE_zone_to_name (phone->ns,
-                                                &phone->my_zone,
-                                                &ring->caller_id,
-                                                &handle_caller_name,
-                                                caller);
+    caller->state = CS_RINGING;
+    phone->event_handler (phone->event_handler_cls,
+                          GNUNET_CONVERSATION_EC_PHONE_RING,
+                          caller,
+                          &caller->caller_id);
     break;
   }
 }
@@ -339,7 +289,7 @@ handle_phone_ring (void *cls,
 /**
  * We received a `struct ClientPhoneHangupMessage`.
  *
- * @param cls the `struct GNUNET_CONVERSATION_Phone`
+ * @param cls the `struct GNUNET_CONVERSATION_Phone *`
  * @param msg the message
  */
 static void
@@ -355,19 +305,23 @@ 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",
+              GNUNET_GNSRECORD_pkey_to_zkey (&caller->caller_id));
   switch (caller->state)
   {
-  case CS_RESOLVE:
-    GNUNET_NAMESTORE_cancel (caller->qe);
-    caller->qe = NULL;
-    break;
   case CS_RINGING:
     phone->event_handler (phone->event_handler_cls,
                           GNUNET_CONVERSATION_EC_PHONE_HUNG_UP,
                           caller,
-                          caller->caller_id_str);
+                          &caller->caller_id);
     break;
   case CS_ACTIVE:
     caller->speaker->disable_speaker (caller->speaker->cls);
@@ -375,7 +329,7 @@ handle_phone_hangup (void *cls,
     phone->event_handler (phone->event_handler_cls,
                           GNUNET_CONVERSATION_EC_PHONE_HUNG_UP,
                           caller,
-                          caller->caller_id_str);
+                          &caller->caller_id);
     break;
   case CS_CALLEE_SUSPENDED:
   case CS_CALLER_SUSPENDED:
@@ -383,7 +337,7 @@ handle_phone_hangup (void *cls,
     phone->event_handler (phone->event_handler_cls,
                           GNUNET_CONVERSATION_EC_PHONE_HUNG_UP,
                           caller,
-                          caller->caller_id_str);
+                          &caller->caller_id);
     break;
   }
   GNUNET_CONTAINER_DLL_remove (phone->caller_head,
@@ -415,9 +369,6 @@ handle_phone_suspend (void *cls,
     return;
   switch (caller->state)
   {
-  case CS_RESOLVE:
-    GNUNET_break_op (0);
-    break;
   case CS_RINGING:
     GNUNET_break_op (0);
     break;
@@ -463,9 +414,6 @@ handle_phone_resume (void *cls,
     return;
   switch (caller->state)
   {
-  case CS_RESOLVE:
-    GNUNET_break_op (0);
-    break;
   case CS_RINGING:
     GNUNET_break_op (0);
     break;
@@ -513,9 +461,6 @@ handle_phone_audio_message (void *cls,
     return;
   switch (caller->state)
   {
-  case CS_RESOLVE:
-    GNUNET_break_op (0);
-    break;
   case CS_RINGING:
     GNUNET_break_op (0);
     break;
@@ -544,14 +489,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);
+    GNUNET_CONVERSATION_caller_hang_up (caller);
+  }
+}
+
+
 /**
  * The phone got disconnected, reconnect to the service.
  *
@@ -581,16 +552,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);
@@ -640,9 +603,20 @@ GNUNET_CONVERSATION_phone_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                              "CONVERSATION",
                                              "LINE",
                                              &line))
+  {
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
+                               "CONVERSATION",
+                               "LINE");
     return NULL;
+  }
   if (line >= (1 << 31))
+  {
+    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
+                               "CONVERSATION",
+                               "LINE",
+                               _("number too large"));
     return NULL;
+  }
   phone = GNUNET_new (struct GNUNET_CONVERSATION_Phone);
   if (GNUNET_OK !=
       GNUNET_CRYPTO_get_peer_identity (cfg,
@@ -743,10 +717,6 @@ GNUNET_CONVERSATION_caller_hang_up (struct GNUNET_CONVERSATION_Caller *caller)
 
   switch (caller->state)
   {
-  case CS_RESOLVE:
-    GNUNET_NAMESTORE_cancel (caller->qe);
-    caller->qe = NULL;
-    break;
   case CS_ACTIVE:
     caller->speaker->disable_speaker (caller->speaker->cls);
     caller->mic->disable_microphone (caller->mic->cls);
@@ -757,10 +727,11 @@ GNUNET_CONVERSATION_caller_hang_up (struct GNUNET_CONVERSATION_Caller *caller)
   GNUNET_CONTAINER_DLL_remove (phone->caller_head,
                                phone->caller_tail,
                                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);
+  hang->cid = caller->cid;
   GNUNET_MQ_send (phone->mq, e);
+  GNUNET_free (caller);
 }
 
 
@@ -772,16 +743,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);