Previous: , Up: Writing a Client Library   [Contents]


4.3.2.3 Receiving Replies from the Service

Clients can receive messages from the service using the handlers specified in the handlers array we specified when connecting to the service. Entries in the the array are usually created using one of two macros, depending on whether the message is fixed size or variable size. Variable size messages are managed using two callbacks, one to check that the message is well-formed, the other to actually process the message. Fixed size messages are fully checked by the MQ-logic, and thus only need to provide the handler to process the message. Note that the prefixes check\_ and handle\_ are mandatory.

static void
handle_fix (void *cls, const struct MyMessage *msg)
{
  // process 'msg'
}

static int
check_var (void *cls, const struct MyVarMessage *msg)
{
  // check 'msg' is well-formed
  return GNUNET_OK;
}

static void
handle_var (void *cls, const struct MyVarMessage *msg)
{
  // process 'msg'
}

struct GNUNET_MQ_MessageHandler handlers[] = {
  GNUNET_MQ_hd_fixed_size (fix,
                          GNUNET_MESSAGE_TYPE_MY_FIX,
                          struct MyMessage,
                          NULL),
  GNUNET_MQ_hd_fixed_size (var,
                          GNUNET_MESSAGE_TYPE_MY_VAR,
                          struct MyVarMessage,
                          NULL),

  GNUNET_MQ_handler_end ()
};

Exercise: Expand your helper function to receive a response message (for example, containing just the struct GNUnet MessageHeader without any payload). Upon receiving the service’s response, you should call a callback provided to your helper function’s API.

Exercise: Figure out where you can pass values to the closures (cls).