+/**
+ * Connect to the peerinfo service.
+ *
+ * @param cfg configuration to use
+ * @return NULL on error (configuration related, actual connection
+ * establishment may happen asynchronously).
+ */
+struct GNUNET_PEERINFO_Handle *
+GNUNET_PEERINFO_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+ struct GNUNET_PEERINFO_Handle *ret;
+
+ ret = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_Handle));
+ ret->client = GNUNET_CLIENT_connect ("peerinfo", cfg);
+ ret->cfg = cfg;
+ return ret;
+}
+
+
+/**
+ * Disconnect from the peerinfo service. Note that all iterators must
+ * have completed or have been cancelled by the time this function is
+ * called (otherwise, calling this function is a serious error).
+ * Furthermore, if 'GNUNET_PEERINFO_add_peer' operations are still
+ * pending, they will be cancelled silently on disconnect.
+ *
+ * @param h handle to disconnect
+ */
+void
+GNUNET_PEERINFO_disconnect (struct GNUNET_PEERINFO_Handle *h)
+{
+ struct TransmissionQueueEntry *tqe;
+
+ while (NULL != (tqe = h->tq_head))
+ {
+ GNUNET_CONTAINER_DLL_remove (h->tq_head, h->tq_tail, tqe);
+ if (tqe->cont != NULL)
+ tqe->cont (tqe->cont_cls, GNUNET_SYSERR);
+ GNUNET_free (tqe);
+ }
+ if (h->th != NULL)
+ {
+ GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+ h->th = NULL;
+ }
+ if (NULL != h->client)
+ {
+ GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
+ h->client = NULL;
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != h->r_task)
+ {
+ GNUNET_SCHEDULER_cancel (h->r_task);
+ h->r_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ GNUNET_free (h);
+}
+
+
+/**
+ * Check if we have a request pending in the transmission queue and are
+ * able to transmit it right now. If so, schedule transmission.
+ *
+ * @param h handle to the service
+ */
+static void
+trigger_transmit (struct GNUNET_PEERINFO_Handle *h);
+
+
+/**
+ * Close the existing connection to PEERINFO and reconnect.
+ *
+ * @param h handle to the service
+ */
+static void
+reconnect (struct GNUNET_PEERINFO_Handle *h);
+
+/**
+ * Task scheduled to re-try connecting to the peerinfo service.
+ *
+ * @param cls the 'struct GNUNET_PEERINFO_Handle'
+ * @param tc scheduler context
+ */
+static void
+reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct GNUNET_PEERINFO_Handle *h = cls;
+
+ h->r_task = GNUNET_SCHEDULER_NO_TASK;
+ reconnect (h);
+}
+
+
+/**
+ * Close the existing connection to PEERINFO and reconnect.
+ *
+ * @param h handle to the service
+ */
+static void
+reconnect (struct GNUNET_PEERINFO_Handle *h)
+{
+ if (h->r_task != GNUNET_SCHEDULER_NO_TASK)
+ {
+ GNUNET_SCHEDULER_cancel (h->r_task);
+ h->r_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+ if (NULL != h->th)
+ {
+ GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+ h->th = NULL;
+ }
+ if (NULL != h->client)
+ {
+ GNUNET_CLIENT_disconnect (h->client, GNUNET_SYSERR);
+ h->client = NULL;
+ }
+ h->client = GNUNET_CLIENT_connect ("peerinfo", h->cfg);
+ if (NULL == h->client)
+ {
+ h->r_task =
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
+ h);
+ return;
+ }
+ trigger_transmit (h);
+}
+
+
+/**
+ * Transmit the request at the head of the transmission queue
+ * and trigger continuation (if any).
+ *
+ * @param cls the 'struct GNUNET_PEERINFO_Handle' (with the queue)
+ * @param size size of the buffer (0 on error)
+ * @param buf where to copy the message
+ * @return number of bytes copied to buf
+ */