* @param timeout after how long should we give up trying to connect to the core service?
* @param cls closure for the various callbacks that follow (including handlers in the handlers array)
* @param init callback to call on timeout or once we have successfully
- * connected to the core service
+ * connected to the core service; note that timeout is only meaningful if init is not NULL
* @param connects function to call on peer connect, can be NULL
* @param disconnects function to call on peer disconnect / timeout, can be NULL
* @param bfc function to call to fill up spare bandwidth, can be NULL
* GNUNET_MessageHeader and hence we do not need to give it the full message
* can be used to improve efficiency, ignored if outbound_notify is NULLL
* @param handlers callbacks for messages we care about, NULL-terminated
+ * @return handle to the core service (only useful for disconnect until 'init' is called);
+ * NULL on error (in this case, init is never called)
*/
-void
+struct GNUNET_CORE_Handle *
GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
const struct GNUNET_CONFIGURATION_Handle *cfg,
struct GNUNET_TIME_Relative timeout,
{
struct GNUNET_CORE_Handle *h;
- GNUNET_assert (init != NULL);
h = GNUNET_malloc (sizeof (struct GNUNET_CORE_Handle));
h->sched = sched;
h->cfg = cfg;
h->client = GNUNET_CLIENT_connect (sched, "core", cfg);
if (h->client == NULL)
{
- init (cls, NULL, NULL, NULL);
GNUNET_free (h);
- return;
+ return NULL;
}
h->startup_timeout = GNUNET_TIME_relative_to_absolute (timeout);
h->hcnt = 0;
sizeof (uint16_t) * h->hcnt, timeout,
GNUNET_YES,
&transmit_start, h);
+ return h;
}
*/
static uint64_t max_pending_requests = 32;
+
/**
* Write the current index information list to disk.
*/
struct IndexInfo *pos;
if (NULL != core)
- GNUNET_CORE_disconnect (core);
- GNUNET_DATASTORE_disconnect (dsh,
- GNUNET_NO);
- dsh = NULL;
+ {
+ GNUNET_CORE_disconnect (core);
+ core = NULL;
+ }
+ if (NULL != dsh)
+ {
+ GNUNET_DATASTORE_disconnect (dsh,
+ GNUNET_NO);
+ dsh = NULL;
+ }
GNUNET_CONTAINER_multihashmap_iterate (requests_by_query,
&destroy_pending_request_cb,
NULL);
};
-/**
- * Task that will try to initiate a connection with the
- * core service.
- *
- * @param cls unused
- * @param tc unused
- */
-static void
-core_connect_task (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-
-/**
- * Function called by the core after we've
- * connected.
- *
- * @param cls closure, unused
- * @param server handle to the core service
- * @param my_identity our peer identity (unused)
- * @param publicKey our public key (unused)
- */
-static void
-core_start_cb (void *cls,
- struct GNUNET_CORE_Handle * server,
- const struct GNUNET_PeerIdentity *
- my_identity,
- const struct
- GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
- publicKey)
-{
- if (server == NULL)
- {
- GNUNET_SCHEDULER_add_delayed (sched,
- GNUNET_TIME_UNIT_SECONDS,
- &core_connect_task,
- NULL);
- return;
- }
- core = server;
-}
-
-
-/**
- * Task that will try to initiate a connection with the
- * core service.
- *
- * @param cls unused
- * @param tc unused
- */
-static void
-core_connect_task (void *cls,
- const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
- GNUNET_CORE_connect (sched,
- cfg,
- GNUNET_TIME_UNIT_FOREVER_REL,
- NULL,
- &core_start_cb,
- &peer_connect_handler,
- &peer_disconnect_handler,
- NULL,
- NULL, GNUNET_NO,
- NULL, GNUNET_NO,
- p2p_handlers);
-}
-
-
/**
* Process fs requests.
*
read_index_list ();
dsh = GNUNET_DATASTORE_connect (cfg,
sched);
- if (NULL == dsh)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("Failed to connect to datastore service.\n"));
- return;
- }
+ core = GNUNET_CORE_connect (sched,
+ cfg,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ NULL,
+ NULL,
+ &peer_connect_handler,
+ &peer_disconnect_handler,
+ NULL,
+ NULL, GNUNET_NO,
+ NULL, GNUNET_NO,
+ p2p_handlers);
+
GNUNET_SERVER_disconnect_notify (server,
&handle_client_disconnect,
NULL);
GNUNET_SERVER_add_handlers (server, handlers);
- core_connect_task (NULL, NULL);
GNUNET_SCHEDULER_add_delayed (sched,
GNUNET_TIME_UNIT_FOREVER_REL,
&shutdown_task,
NULL);
+ if (NULL == dsh)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _("Failed to connect to `%s' service.\n"),
+ "datastore");
+ GNUNET_SCHEDULER_shutdown (sched);
+ return;
+ }
+ if (NULL == core)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _("Failed to connect to `%s' service.\n"),
+ "core");
+ GNUNET_SCHEDULER_shutdown (sched);
+ return;
+ }
}
*/
static struct GNUNET_STATISTICS_Handle *stats;
+/**
+ * Handle to the core service (NULL until we've connected to it).
+ */
+struct GNUNET_CORE_Handle *core;
+
/**
* gnunet-daemon-hostlist command line options.
*/
GNUNET_NO);
stats = NULL;
}
+ GNUNET_CORE_disconnect (core);
}
{
GNUNET_HOSTLIST_server_start (cfg, sched, stats);
}
- GNUNET_CORE_connect (sched, cfg,
- GNUNET_TIME_UNIT_FOREVER_REL,
- NULL,
- &core_init,
- ch, dh,
- NULL,
- NULL, GNUNET_NO,
- NULL, GNUNET_NO,
- handlers);
+ core = GNUNET_CORE_connect (sched, cfg,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ NULL,
+ &core_init,
+ ch, dh,
+ NULL,
+ NULL, GNUNET_NO,
+ NULL, GNUNET_NO,
+ handlers);
GNUNET_SCHEDULER_add_delayed (sched,
GNUNET_TIME_UNIT_FOREVER_REL,
&cleaning_task, NULL);
+ if (NULL == core)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _("Failed to connect to `%s' service.\n"),
+ "core");
+ GNUNET_SCHEDULER_shutdown (sched);
+ return;
+ }
}
* @param timeout after how long should we give up trying to connect to the core service?
* @param cls closure for the various callbacks that follow (including handlers in the handlers array)
* @param init callback to call on timeout or once we have successfully
- * connected to the core service
+ * connected to the core service; note that timeout is only meaningful if init is not NULL
* @param connects function to call on peer connect, can be NULL
* @param disconnects function to call on peer disconnect / timeout, can be NULL
* @param bfc function to call to fill up spare bandwidth, can be NULL
* GNUNET_MessageHeader and hence we do not need to give it the full message
* can be used to improve efficiency, ignored if outbound_notify is NULLL
* @param handlers callbacks for messages we care about, NULL-terminated
+ * @return handle to the core service (only useful for disconnect until 'init' is called),
+ * NULL on error (in this case, init is never called)
*/
-void
+struct GNUNET_CORE_Handle *
GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched,
const struct GNUNET_CONFIGURATION_Handle *cfg,
struct GNUNET_TIME_Relative timeout,
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
_("Failed to connect to core service, can not manage topology!\n"));
+ GNUNET_SCHEDULER_shutdown (sched);
return;
}
handle = server;
GNUNET_TRANSPORT_disconnect (transport);
transport = NULL;
- GNUNET_CORE_disconnect (handle);
- handle = NULL;
+ if (handle != NULL)
+ {
+ GNUNET_CORE_disconnect (handle);
+ handle = NULL;
+ }
while (NULL != (pl = friends))
{
friends = pl->next;
NULL,
NULL,
NULL);
- GNUNET_CORE_connect (sched,
- cfg,
- GNUNET_TIME_UNIT_FOREVER_REL,
- NULL,
- &core_init,
- &connect_notify,
- &disconnect_notify,
- &hello_advertising,
- NULL, GNUNET_NO,
- NULL, GNUNET_NO,
- handlers);
-
+ handle = GNUNET_CORE_connect (sched,
+ cfg,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ NULL,
+ &core_init,
+ &connect_notify,
+ &disconnect_notify,
+ &hello_advertising,
+ NULL, GNUNET_NO,
+ NULL, GNUNET_NO,
+ handlers);
GNUNET_SCHEDULER_add_delayed (sched,
GNUNET_TIME_UNIT_FOREVER_REL,
&cleaning_task, NULL);
+ if (NULL == transport)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _("Failed to connect to `%s' service.\n"),
+ "transport");
+ GNUNET_SCHEDULER_shutdown (sched);
+ return;
+ }
+ if (NULL == handle)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ _("Failed to connect to `%s' service.\n"),
+ "core");
+ GNUNET_SCHEDULER_shutdown (sched);
+ return;
+ }
}