#include #define PLAIN_PROFILE "http://fact.aspl.es/profiles/plain_profile" /* listener context */ VortexCtx * ctx = NULL; /* Flag to stop app */ axl_bool FlagClosed; /* Thread counters */ int CountThreadCreate; int CountThreadDestroy; void frame_received (VortexChannel * channel, VortexConnection * connection, VortexFrame * frame, axlPointer user_data) { printf ("A frame received on channl: %d\n", vortex_channel_get_number (channel)); printf ("Data received: '%s'\n", (char*) vortex_frame_get_payload (frame)); /* reply the peer client with the same content */ vortex_channel_send_rpyv (channel, vortex_frame_get_msgno (frame), "Received Ok: %s", vortex_frame_get_payload (frame)); printf ("VORTEX_LISTENER: end task (pid: %d)\n", getpid ()); return; } /* Declare original thread create function */ extern axl_bool vortex_thread_create_internal (VortexThread * thread_def, VortexThreadFunc func, axlPointer user_data, va_list args); /* Replacement thread create */ axl_bool AltThreadCreate(VortexThread * thread_def, VortexThreadFunc func, axlPointer user_data, va_list args) { CountThreadCreate++; return vortex_thread_create_internal (thread_def, func, user_data, args); } /* Declare original thread destroy function */ extern axl_bool vortex_thread_destroy_internal (VortexThread * thread_def, axl_bool free_data); /* Replacement Thread destroy */ axl_bool AltThreadDestroy (VortexThread * thread_def, axl_bool free_data) { CountThreadDestroy++; return vortex_thread_destroy_internal(thread_def, free_data); } /* Connection Close handler */ void on_close (VortexConnection * connection) { printf("Connection is closing\n"); FlagClosed = axl_true; } int on_accepted (VortexConnection * connection, axlPointer data) { printf ("New connection accepted from: %s:%s\n", vortex_connection_get_host (connection), vortex_connection_get_port (connection)); /* Set connection close handler */ vortex_connection_set_on_close(connection, on_close); /* return axl_true to accept the connection to be created */ return axl_true; } int main (int argc, char ** argv) { /* Initialise */ FlagClosed = axl_false; CountThreadCreate = 0; CountThreadDestroy = 0; /* create the context */ ctx = vortex_ctx_new (); /* init vortex library */ if (! vortex_init_ctx (ctx)) { /* unable to init context */ vortex_ctx_free (ctx); return -1; } /* end if */ /* register a profile */ vortex_profiles_register (ctx, PLAIN_PROFILE, NULL, NULL, NULL, NULL, frame_received, NULL); /* create a vortex server */ vortex_listener_new (ctx, "0.0.0.0", "44000", NULL, NULL); /* Use custom thread creation */ vortex_thread_set_create(AltThreadCreate); vortex_thread_set_destroy(AltThreadDestroy); /* configure connection notification */ vortex_listener_set_on_connection_accepted (ctx, on_accepted, NULL); /* wait for listeners (until vortex_exit is called) */ // vortex_listener_wait (ctx); while (axl_false == FlagClosed) { sleep(1); } printf("Connection was closed. Exiting...\n"); printf("Threads created: %d\n", CountThreadCreate); printf("Threads destroyed: %d\n", CountThreadDestroy); /* end vortex function */ vortex_exit_ctx (ctx, axl_true); return 0; }