#define LOG(kind,...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__)
+struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
+{
+ /**
+ * @brief Handle to the configuration
+ */
+ struct GNUNET_CONFIGURATION_Handle *cfg;
+
+ /**
+ * @brief File name of configuration file
+ */
+ char *cfg_filename;
+
+ /**
+ * @brief Handle to the transport service
+ */
+ struct GNUNET_SERVICE_Handle *tsh;
+
+ /**
+ * @brief Task that will be run on shutdown to stop and clean transport
+ * service
+ */
+ struct GNUNET_SCHEDULER_Task *ts_shutdown_task;
+
+ /**
+ * @brief Handle to the client
+ */
+ struct GNUNET_SERVICE_Client *client;
+
+ /**
+ * @brief Process of the communicator
+ */
+ struct GNUNET_OS_Process *c_proc;
+
+ /**
+ * @brief Task that will be run on shutdown to stop and clean communicator
+ */
+ struct GNUNET_SCHEDULER_Task *c_shutdown_task;
+
+ /* Callbacks + Closures */
+ /**
+ * @brief Callback called when a new communicator connects
+ */
+ GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available;
+
+ /**
+ * @brief Closure to the callback
+ */
+ void *communicator_available_cls;
+};
+
+
/**
* @brief Check whether incoming msg indicating available communicator is
* correct
check_communicator_available (void *cls,
const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
{
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "check_communicator_available()\n");
- return GNUNET_YES;
+ uint16_t size;
+
+ size = ntohs (msg->header.size) - sizeof (*msg);
+ if (0 == size)
+ return GNUNET_OK; /* receive-only communicator */
+ GNUNET_MQ_check_zero_termination (msg);
+ return GNUNET_OK;
}
handle_communicator_available (void *cls,
const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
{
- GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available = cls;
- LOG (GNUNET_ERROR_TYPE_DEBUG,
- "handle_communicator_available()\n");
- if (NULL != communicator_available)
+ struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
+ uint16_t size;
+
+ size = ntohs (msg->header.size) - sizeof (*msg);
+ if (0 == size)
+ return; /* receive-only communicator */
+ if (NULL != tc_h->communicator_available)
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"calling communicator_available()\n");
- communicator_available (NULL, msg);
+ tc_h->communicator_available (tc_h->communicator_available_cls, msg);
}
- //GNUNET_SERVICE_client_continue (client);
+ GNUNET_SERVICE_client_continue (tc_h->client);
}
}
+/**
+ * @brief Callback called when new Client (Communicator) connects
+ *
+ * @param cls Closure - TransporCommmunicator Handle
+ * @param client Client
+ * @param mq Messagequeue
+ *
+ * @return TransportCommunicator Handle
+ */
+static void *
+connect_cb (void *cls,
+ struct GNUNET_SERVICE_Client *client,
+ struct GNUNET_MQ_Handle *mq)
+{
+ struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Client connected.\n");
+ tc_h->client = client;
+ return tc_h;
+}
+
+
+/**
+ * @brief Callback called when Client disconnects
+ *
+ * @param cls Closure - TransportCommunicator Handle
+ * @param client Client
+ * @param internal_cls TransporCommmunicator Handle
+ */
+static void
+disconnect_cb (void *cls,
+ struct GNUNET_SERVICE_Client *client,
+ void *internal_cls)
+{
+ struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Client disconnected.\n");
+ tc_h->client = NULL;
+}
+
+
/**
* @brief Start the communicator part of the transport service
*
* @param cfg Configuration
*/
static void
-transport_communicator_start (GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available,
- struct GNUNET_CONFIGURATION_Handle *cfg)
+transport_communicator_start (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
{
struct GNUNET_MQ_MessageHandler mh[] = {
GNUNET_MQ_hd_var_size (communicator_available,
GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR,
struct GNUNET_TRANSPORT_CommunicatorAvailableMessage,
- &communicator_available),
+ &tc_h),
//GNUNET_MQ_hd_var_size (communicator_backchannel,
// GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL,
// struct GNUNET_TRANSPORT_CommunicatorBackchannel,
struct GNUNET_SERVICE_Handle *h;
h = GNUNET_SERVICE_start ("transport",
- cfg,
- NULL,
- NULL,
- NULL,
+ tc_h->cfg,
+ &connect_cb,
+ &disconnect_cb,
+ tc_h,
mh);
if (NULL == h)
LOG (GNUNET_ERROR_TYPE_ERROR,
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Started service\n");
- GNUNET_SCHEDULER_add_shutdown (&shutdown_service, h);
+ /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_service, h);
}
}
+/**
+ * @brief Task run at shutdown to kill communicator and clean up
+ *
+ * @param cls Closure - Process of communicator
+ */
+static void
+shutdown_communicator (void *cls)
+{
+ struct GNUNET_OS_Process *proc = cls;
+
+ if (GNUNET_OK != GNUNET_OS_process_kill (proc,
+ SIGTERM))
+ {
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "Error shutting down communicator with SIGERM, trying SIGKILL\n");
+ if (GNUNET_OK != GNUNET_OS_process_kill (proc,
+ SIGKILL))
+ {
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ "Error shutting down communicator with SIGERM and SIGKILL\n");
+ }
+ }
+ GNUNET_OS_process_destroy (proc);
+}
+
+
/**
* @brief Start the communicator
*
* @param cfgname Name of the communicator
*/
static void
-communicator_start (const char *cfgname)
+communicator_start (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h)
{
char *binary;
- struct GNUNET_CONFIGURATION_Handle *cfg;
- struct GNUNET_OS_Process *proc;
LOG (GNUNET_ERROR_TYPE_DEBUG,
"communicator_start\n");
binary = GNUNET_OS_get_libexec_binary_path ("gnunet-communicator-unix");
- cfg = GNUNET_CONFIGURATION_create ();
- proc =
+ tc_h->c_proc =
GNUNET_OS_start_process (GNUNET_YES,
GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
NULL, NULL, NULL,
binary,
"./gnunet-communicator-unix",
"-c",
- cfgname,
+ tc_h->cfg_filename,
NULL);
- if (NULL == proc)
+ if (NULL == tc_h->c_proc)
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Failed to start communicator!");
return;
}
- GNUNET_assert (GNUNET_OK ==
- GNUNET_CONFIGURATION_load (cfg,
- cfgname));
LOG (GNUNET_ERROR_TYPE_DEBUG,
"started communicator\n");
GNUNET_free (binary);
+ /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_communicator,
+ tc_h->c_proc);
}
//GNUNET_TRANSPORT_TESTING_Callback4 cb4,
void *cb_cls)
{
- struct GNUNET_CONFIGURATION_Handle *cfg;
+ struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h;
- cfg = GNUNET_CONFIGURATION_create ();
+ tc_h = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle);
+ tc_h->cfg_filename = GNUNET_strdup (cfg_filename);
+ tc_h->cfg = GNUNET_CONFIGURATION_create ();
if ( (GNUNET_SYSERR ==
- GNUNET_CONFIGURATION_load (cfg,
+ GNUNET_CONFIGURATION_load (tc_h->cfg,
cfg_filename)) )
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
cfg_filename);
return NULL;
}
+ tc_h->communicator_available = communicator_available;
+ tc_h->communicator_available_cls = cb_cls;
+
/* Start communicator part of service */
- transport_communicator_start (communicator_available, cfg);
+ transport_communicator_start (tc_h);
/* Schedule start communicator */
- communicator_start ("test_communicator_1.conf");
+ communicator_start (tc_h);
+ return tc_h;
}
//void