+ * 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 *h;
+
+ h = GNUNET_new (struct GNUNET_PEERINFO_Handle);
+ h->client = GNUNET_CLIENT_connect ("peerinfo", cfg);
+ h->cfg = cfg;
+ return h;
+}
+
+
+/**
+ * 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 GNUNET_PEERINFO_AddContext *ac;
+ struct GNUNET_PEERINFO_IteratorContext *ic;
+
+ while (NULL != (ic = h->ic_head))
+ {
+ GNUNET_break (GNUNET_YES == ic->request_transmitted);
+ ic->request_transmitted = GNUNET_NO;
+ GNUNET_PEERINFO_iterate_cancel (ic);
+ }
+ while (NULL != (ac = h->ac_head))
+ {
+ GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
+ if (NULL != ac->cont)
+ ac->cont (ac->cont_cls, _("aborted due to explicit disconnect request"));
+ GNUNET_free (ac);
+ }
+ if (NULL != h->th)
+ {
+ GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
+ h->th = NULL;
+ }
+ if (NULL != h->client)
+ {
+ GNUNET_CLIENT_disconnect (h->client);
+ 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 (GNUNET_SCHEDULER_NO_TASK != h->r_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);
+ h->client = NULL;
+ }
+ h->in_receive = GNUNET_NO;
+ 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
+ */
+static size_t
+do_transmit (void *cls, size_t size, void *buf)
+{
+ struct GNUNET_PEERINFO_Handle *h = cls;
+ struct GNUNET_PEERINFO_AddContext *ac = h->ac_head;
+ size_t ret;
+
+ h->th = NULL;
+ if (NULL == ac)
+ return 0; /* request was cancelled in the meantime */
+ if (NULL == buf)
+ {
+ /* peerinfo service died */
+ LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
+ "Failed to transmit message to `%s' service.\n", "PEERINFO");
+ GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
+ reconnect (h);
+ if (NULL != ac->cont)
+ ac->cont (ac->cont_cls, _("failed to transmit request (service down?)"));
+ GNUNET_free (ac);
+ return 0;
+ }
+ ret = ac->size;
+ if (size < ret)
+ {
+ /* change in head of queue (i.e. cancel + add), try again */
+ trigger_transmit (h);
+ return 0;
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Transmitting request of size %u to `%s' service.\n", ret, "PEERINFO");
+ memcpy (buf, &ac[1], ret);
+ GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
+ trigger_transmit (h);
+ if (NULL != ac->cont)
+ ac->cont (ac->cont_cls, NULL);
+ GNUNET_free (ac);
+ return ret;
+}
+
+
+/**
+ * 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)
+{
+ struct GNUNET_PEERINFO_AddContext *ac;
+
+ if (NULL == (ac = h->ac_head))
+ return; /* no requests queued */
+ if (NULL != h->th)
+ return; /* request already pending */
+ if (NULL == h->client)
+ {
+ /* disconnected, try to reconnect */
+ reconnect (h);
+ return;
+ }
+ h->th =
+ GNUNET_CLIENT_notify_transmit_ready (h->client, ac->size,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_YES,
+ &do_transmit, h);
+}
+
+
+/**
+ * Add a host to the persistent list. This method operates in
+ * semi-reliable mode: if the transmission is not completed by
+ * the time 'GNUNET_PEERINFO_disconnect' is called, it will be
+ * aborted. Furthermore, if a second HELLO is added for the
+ * same peer before the first one was transmitted, PEERINFO may
+ * merge the two HELLOs prior to transmission to the service.
+ *
+ * @param h handle to the peerinfo service
+ * @param hello the verified (!) HELLO message
+ * @param cont continuation to call when done, NULL is allowed
+ * @param cont_cls closure for 'cont'
+ * @return handle to cancel add operation; all pending
+ * 'add' operations will be cancelled automatically
+ * on disconnect, so it is not necessary to keep this
+ * handle (unless 'cont' is NULL and at some point
+ * calling 'cont' must be prevented)
+ */
+struct GNUNET_PEERINFO_AddContext *
+GNUNET_PEERINFO_add_peer (struct GNUNET_PEERINFO_Handle *h,
+ const struct GNUNET_HELLO_Message *hello,
+ GNUNET_PEERINFO_Continuation cont,
+ void *cont_cls)
+{
+ uint16_t hs = GNUNET_HELLO_size (hello);
+ struct GNUNET_PEERINFO_AddContext *ac;
+ struct GNUNET_PeerIdentity peer;
+
+ GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (hello, &peer));
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Adding peer `%s' to PEERINFO database (%u bytes of `%s')\n",
+ GNUNET_i2s (&peer), hs, "HELLO");
+ ac = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_AddContext) + hs);
+ ac->h = h;
+ ac->size = hs;
+ ac->cont = cont;
+ ac->cont_cls = cont_cls;
+ memcpy (&ac[1], hello, hs);
+ GNUNET_CONTAINER_DLL_insert_tail (h->ac_head, h->ac_tail, ac);
+ trigger_transmit (h);
+ return ac;
+}
+
+
+/**
+ * Cancel pending 'add' operation. Must only be called before
+ * either 'cont' or 'GNUNET_PEERINFO_disconnect' are invoked.
+ *
+ * @param ac handle for the add operation to cancel
+ */
+void
+GNUNET_PEERINFO_add_peer_cancel (struct GNUNET_PEERINFO_AddContext *ac)
+{
+ struct GNUNET_PEERINFO_Handle *h = ac->h;
+
+ GNUNET_CONTAINER_DLL_remove (h->ac_head, h->ac_tail, ac);
+ GNUNET_free (ac);
+}
+
+
+/**
+ * Type of a function to call when we receive a message from the
+ * service. Call the iterator with the result and (if applicable)
+ * continue to receive more messages or trigger processing the next
+ * event (if applicable).