use c99
[oweals/gnunet.git] / src / conversation / conversation_api.c
index bc6d0f460161c00767da33f766f6dd6644048f6c..730cf1c70d0a34f929f56ab801109db66591f745 100644 (file)
@@ -1,6 +1,6 @@
 /*
   This file is part of GNUnet
-  (C) 2013, 2014 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.
  */
 
 /**
@@ -160,11 +160,6 @@ struct GNUNET_CONVERSATION_Phone
    */
   const struct GNUNET_CONFIGURATION_Handle *cfg;
 
-  /**
-   * Handle to talk with CONVERSATION service.
-   */
-  struct GNUNET_CLIENT_Connection *client;
-
   /**
    * We keep all callers in a DLL.
    */
@@ -252,17 +247,15 @@ transmit_phone_audio (void *cls,
  * We received a `struct ClientPhoneRingMessage`
  *
  * @param cls the `struct GNUNET_CONVERSATION_Phone`
- * @param msg the message
+ * @param ring the message
  */
 static void
 handle_phone_ring (void *cls,
-                   const struct GNUNET_MessageHeader *msg)
+                   const struct ClientPhoneRingMessage *ring)
 {
   struct GNUNET_CONVERSATION_Phone *phone = cls;
-  const struct ClientPhoneRingMessage *ring;
   struct GNUNET_CONVERSATION_Caller *caller;
 
-  ring = (const struct ClientPhoneRingMessage *) msg;
   switch (phone->state)
   {
   case PS_REGISTER:
@@ -289,18 +282,16 @@ 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
 handle_phone_hangup (void *cls,
-                     const struct GNUNET_MessageHeader *msg)
+                     const struct ClientPhoneHangupMessage *hang)
 {
   struct GNUNET_CONVERSATION_Phone *phone = cls;
-  const struct ClientPhoneHangupMessage *hang;
   struct GNUNET_CONVERSATION_Caller *caller;
 
-  hang = (const struct ClientPhoneHangupMessage *) msg;
   for (caller = phone->caller_head; NULL != caller; caller = caller->next)
     if (hang->cid == caller->cid)
       break;
@@ -351,17 +342,15 @@ handle_phone_hangup (void *cls,
  * We received a `struct ClientPhoneSuspendMessage`.
  *
  * @param cls the `struct GNUNET_CONVERSATION_Phone`
- * @param msg the message
+ * @param suspend the message
  */
 static void
 handle_phone_suspend (void *cls,
-                     const struct GNUNET_MessageHeader *msg)
+                     const struct ClientPhoneSuspendMessage *suspend)
 {
   struct GNUNET_CONVERSATION_Phone *phone = cls;
   struct GNUNET_CONVERSATION_Caller *caller;
-  const struct ClientPhoneSuspendMessage *suspend;
 
-  suspend = (const struct ClientPhoneSuspendMessage *) msg;
   for (caller = phone->caller_head; NULL != caller; caller = caller->next)
     if (suspend->cid == caller->cid)
       break;
@@ -396,17 +385,15 @@ handle_phone_suspend (void *cls,
  * We received a `struct ClientPhoneResumeMessage`.
  *
  * @param cls the `struct GNUNET_CONVERSATION_Phone`
- * @param msg the message
+ * @param resume the message
  */
 static void
 handle_phone_resume (void *cls,
-                     const struct GNUNET_MessageHeader *msg)
+                    const struct ClientPhoneResumeMessage *resume)
 {
   struct GNUNET_CONVERSATION_Phone *phone = cls;
   struct GNUNET_CONVERSATION_Caller *caller;
-  const struct ClientPhoneResumeMessage *resume;
 
-  resume = (const struct ClientPhoneResumeMessage *) msg;
   for (caller = phone->caller_head; NULL != caller; caller = caller->next)
     if (resume->cid == caller->cid)
       break;
@@ -439,21 +426,35 @@ handle_phone_resume (void *cls,
 }
 
 
+/**
+ * We received a `struct ClientAudioMessage`, check it is well-formed.
+ *
+ * @param cls the `struct GNUNET_CONVERSATION_Phone`
+ * @param am the message
+ * @return #GNUNET_OK if @a am is well-formed
+ */
+static int
+check_phone_audio (void *cls,
+                  const struct ClientAudioMessage *am)
+{
+  /* any variable-size payload is OK */
+  return GNUNET_OK;
+}
+
+
 /**
  * We received a `struct ClientAudioMessage`
  *
  * @param cls the `struct GNUNET_CONVERSATION_Phone`
- * @param msg the message
+ * @param am the message
  */
 static void
-handle_phone_audio_message (void *cls,
-                            const struct GNUNET_MessageHeader *msg)
+handle_phone_audio (void *cls,
+                   const struct ClientAudioMessage *am)
 {
   struct GNUNET_CONVERSATION_Phone *phone = cls;
-  const struct ClientAudioMessage *am;
   struct GNUNET_CONVERSATION_Caller *caller;
 
-  am = (const struct ClientAudioMessage *) msg;
   for (caller = phone->caller_head; NULL != caller; caller = caller->next)
     if (am->cid == caller->cid)
       break;
@@ -466,7 +467,7 @@ handle_phone_audio_message (void *cls,
     break;
   case CS_ACTIVE:
     caller->speaker->play (caller->speaker->cls,
-                           ntohs (msg->size) - sizeof (struct ClientAudioMessage),
+                           ntohs (am->header.size) - sizeof (struct ClientAudioMessage),
                            &am[1]);
     break;
   case CS_CALLEE_SUSPENDED:
@@ -531,24 +532,28 @@ clean_up_callers (struct GNUNET_CONVERSATION_Phone *phone)
 static void
 reconnect_phone (struct GNUNET_CONVERSATION_Phone *phone)
 {
-  static struct GNUNET_MQ_MessageHandler handlers[] =
-  {
-    { &handle_phone_ring,
-      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RING,
-      sizeof (struct ClientPhoneRingMessage) },
-    { &handle_phone_hangup,
-      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
-      sizeof (struct ClientPhoneHangupMessage) },
-    { &handle_phone_suspend,
-      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND,
-      sizeof (struct ClientPhoneSuspendMessage) },
-    { &handle_phone_resume,
-      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME,
-      sizeof (struct ClientPhoneResumeMessage) },
-    { &handle_phone_audio_message,
-      GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
-      0 },
-    { NULL, 0, 0 }
+  GNUNET_MQ_hd_fixed_size (phone_ring,
+                          GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RING,
+                          struct ClientPhoneRingMessage);
+  GNUNET_MQ_hd_fixed_size (phone_hangup,
+                          GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_HANG_UP,
+                          struct ClientPhoneHangupMessage);
+  GNUNET_MQ_hd_fixed_size (phone_suspend,
+                          GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_SUSPEND,
+                          struct ClientPhoneSuspendMessage);
+  GNUNET_MQ_hd_fixed_size (phone_resume,
+                          GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_RESUME,
+                          struct ClientPhoneResumeMessage);
+  GNUNET_MQ_hd_var_size (phone_audio,
+                        GNUNET_MESSAGE_TYPE_CONVERSATION_CS_AUDIO,
+                        struct ClientAudioMessage);
+  struct GNUNET_MQ_MessageHandler handlers[] = {
+    make_phone_ring_handler (phone),
+    make_phone_hangup_handler (phone),
+    make_phone_suspend_handler (phone),
+    make_phone_resume_handler (phone),
+    make_phone_audio_handler (phone),
+    GNUNET_MQ_handler_end ()
   };
   struct GNUNET_MQ_Envelope *e;
   struct ClientPhoneRegisterMessage *reg;
@@ -559,19 +564,14 @@ reconnect_phone (struct GNUNET_CONVERSATION_Phone *phone)
     GNUNET_MQ_destroy (phone->mq);
     phone->mq = NULL;
   }
-  if (NULL != phone->client)
-  {
-    GNUNET_CLIENT_disconnect (phone->client);
-    phone->client = NULL;
-  }
   phone->state = PS_REGISTER;
-  phone->client = GNUNET_CLIENT_connect ("conversation", phone->cfg);
-  if (NULL == phone->client)
+  phone->mq = GNUNET_CLIENT_connecT (phone->cfg,
+                                     "conversation",
+                                     handlers,
+                                     &phone_error_handler,
+                                     phone);
+  if (NULL == phone->mq)
     return;
-  phone->mq = GNUNET_MQ_queue_for_connection_client (phone->client,
-                                                     handlers,
-                                                     &phone_error_handler,
-                                                     phone);
   e = GNUNET_MQ_msg (reg, GNUNET_MESSAGE_TYPE_CONVERSATION_CS_PHONE_REGISTER);
   reg->line = phone->my_record.line;
   GNUNET_MQ_send (phone->mq, e);
@@ -634,7 +634,7 @@ GNUNET_CONVERSATION_phone_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
   phone->my_record.line = htonl ((uint32_t) line);
   phone->my_record.version = htonl (0);
   reconnect_phone (phone);
-  if ( (NULL == phone->client) ||
+  if ( (NULL == phone->mq) ||
        (NULL == phone->ns) )
   {
     GNUNET_break (0);
@@ -754,11 +754,6 @@ GNUNET_CONVERSATION_phone_destroy (struct GNUNET_CONVERSATION_Phone *phone)
     GNUNET_MQ_destroy (phone->mq);
     phone->mq = NULL;
   }
-  if (NULL != phone->client)
-  {
-    GNUNET_CLIENT_disconnect (phone->client);
-    phone->client = NULL;
-  }
   GNUNET_free (phone);
 }