/************************ MAIN FUNCTIONS ****************************/
/******************************************************************************/
+/**
+ * Task run during shutdown.
+ *
+ * @param cls unused
+ * @param tc unused
+ */
+static void
+shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (core_handle != NULL)
+ {
+ GNUNET_CORE_disconnect (core_handle);
+ core_handle = NULL;
+ }
+ if (dht_handle != NULL)
+ {
+ GNUNET_DHT_disconnect (dht_handle);
+ dht_handle = NULL;
+ }
+}
+
/**
* Process mesh requests.
*
if (dht_handle == NULL) {
GNUNET_break(0);
}
+
+ /* Scheduled the task to clean up when shutdown is called */
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
+ &shutdown_task, NULL);
+
}
/**
*/
GNUNET_MESH_TunnelEndHandler *cleaner;
+
+ struct GNUNET_CLIENT_TransmitHandle *th;
+
/**
* Closure for all the handlers given by the client
*/
* @param buf where the callee should write the message
* @return number of bytes written to buf
*/
-size_t
+static size_t
send_connect_packet (void *cls, size_t size, void *buf) {
- struct GNUNET_MESH_Handle *h;
+ struct GNUNET_MESH_Handle *h = cls;
struct GNUNET_MESH_ClientConnect *msg;
uint16_t *types;
int ntypes;
GNUNET_MESH_ApplicationType *apps;
int napps;
- if(0 == size || buf == NULL) {
+ h->th = NULL;
+ if (0 == size || buf == NULL) {
GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Send: buffer size 0 or buffer invalid\n");
+ // FIXME: disconnect, reconnect, retry!
return 0;
}
- if(sizeof(struct GNUNET_MessageHeader) > size) {
+ if (sizeof(struct GNUNET_MessageHeader) > size) {
GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Send: buffer size too small\n");
+ // FIXME: disconnect, reconnect, retry!
return 0;
}
msg = (struct GNUNET_MESH_ClientConnect *) buf;
- h = (struct GNUNET_MESH_Handle *) cls;
msg->header.type = GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT;
for(ntypes = 0, types = NULL; h->message_handlers[ntypes].type; ntypes++) {
void *cls,
GNUNET_MESH_TunnelEndHandler cleaner,
const struct GNUNET_MESH_MessageHandler *handlers,
- const GNUNET_MESH_ApplicationType *stypes) {
+ const GNUNET_MESH_ApplicationType *stypes)
+{
struct GNUNET_MESH_Handle *h;
size_t size;
size += h->n_handlers * sizeof(uint16_t);
size += h->n_applications * sizeof(GNUNET_MESH_ApplicationType);
- GNUNET_CLIENT_notify_transmit_ready(h->mesh,
- size,
- GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
- GNUNET_NO,
- &send_connect_packet,
- (void *)h);
+ h->th = GNUNET_CLIENT_notify_transmit_ready(h->mesh,
+ size,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_YES,
+ &send_connect_packet,
+ (void *)h);
return h;
}
*
* @param handle connection to mesh to disconnect
*/
-void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle) {
-
- GNUNET_free(handle);
- return;
+void
+GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
+{
+ if (NULL != handle->th)
+ GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
+ if (NULL != handle->mesh)
+ GNUNET_CLIENT_disconnect (handle->mesh, GNUNET_NO);
+ GNUNET_free(handle);
}
{NULL, 0, 0}
};
+static struct GNUNET_OS_Process *arm_pid;
+
+static struct GNUNET_MESH_Handle *mesh;
+
+static struct GNUNET_DHT_Handle *dht;
+
+static void
+ do_shutdown (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (NULL != mesh)
+ GNUNET_MESH_disconnect (mesh);
+ if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
+ GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
+ GNUNET_OS_process_close (arm_pid);
+}
+
static void
run (void *cls,
char *const *args,
const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) {
- struct GNUNET_OS_Process *arm_pid;
- struct GNUNET_MESH_Handle *mesh;
- struct GNUNET_DHT_Handle *dht;
GNUNET_MESH_ApplicationType app;
- char buffer[2048];
+ // char buffer[2048];
arm_pid = GNUNET_OS_start_process (NULL, NULL,
}
/* do real test work here */
- if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
- GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
- GNUNET_OS_process_close (arm_pid);
-
- return;
+ GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+ &do_shutdown,
+ NULL);
}
+
+
int main (int argc, char *argv[]) {
int ret;
char *const argv2[] = {"test-mesh-api",