From 9919a428c642af8ecbb13b1bb62beb1488406622 Mon Sep 17 00:00:00 2001 From: Srivardhan Hebbar Date: Wed, 22 Apr 2015 14:55:33 +0200 Subject: [PATCH] ecore_con: example program for ecore_con_eet. Summary: Signed-off-by: Srivardhan Hebbar Reviewers: cedric Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D2323 Signed-off-by: Cedric BAIL --- src/examples/ecore/.gitignore | 2 + .../ecore/ecore_con_eet_client_example.c | 97 +++++++++++++++ .../ecore/ecore_con_eet_descriptor_example.c | 42 +++++++ .../ecore/ecore_con_eet_descriptor_example.h | 20 +++ .../ecore/ecore_con_eet_server_example.c | 115 ++++++++++++++++++ 5 files changed, 276 insertions(+) create mode 100644 src/examples/ecore/ecore_con_eet_client_example.c create mode 100644 src/examples/ecore/ecore_con_eet_descriptor_example.c create mode 100644 src/examples/ecore/ecore_con_eet_descriptor_example.h create mode 100644 src/examples/ecore/ecore_con_eet_server_example.c diff --git a/src/examples/ecore/.gitignore b/src/examples/ecore/.gitignore index 700be3252a..acc3750839 100644 --- a/src/examples/ecore/.gitignore +++ b/src/examples/ecore/.gitignore @@ -6,6 +6,8 @@ /ecore_compose_get_example /ecore_con_client_example /ecore_con_client_simple_example +/ecore_con_eet_client_example +/ecore_con_eet_server_example /ecore_con_lookup_example /ecore_con_server_example /ecore_con_server_http_example diff --git a/src/examples/ecore/ecore_con_eet_client_example.c b/src/examples/ecore/ecore_con_eet_client_example.c new file mode 100644 index 0000000000..710df2e5d8 --- /dev/null +++ b/src/examples/ecore/ecore_con_eet_client_example.c @@ -0,0 +1,97 @@ +/* + * This example shows how to register a eet descriptor to a ecore con client + * and send message to ecore con server. The eet descriptor is defined in + * ecore_con_eet_descriptor_example.c. + */ + +//Compile with: +// gcc -o ecore_con_eet_client_example ecore_con_eet_client_example.c ecore_con_eet_descriptor_example.c `pkg-config --cflags --libs eet ecore ecore-con eina` + +#include +#include +#include +#include +#include "ecore_con_eet_descriptor_example.h" + +Eina_Bool +_eet_svr_discnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply, Ecore_Con_Client *conn EINA_UNUSED) +{ + ecore_main_loop_quit(); + return EINA_FALSE; +} + +Eina_Bool +_eet_svr_cnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply, Ecore_Con_Client *conn EINA_UNUSED) +{ + /* Only id and message parameter are sent to server. not_sending, parameter + * is not sent, as it is not added to the eet descriptor. */ + ECE_Example toSend = {1, "Message from Client.", + "This is not sent to server"}; + + ecore_con_eet_send(reply, EXAMPLE_STREAM, &toSend); + return EINA_TRUE; +} + +static void +_eet_raw_data_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply EINA_UNUSED, const char *protocol_name EINA_UNUSED, const char *section, void *value, size_t length EINA_UNUSED) +{ + /* Only a perticular parameter's value is sent by Server. */ + fprintf(stdout, "Section: %s\n", section); + fprintf(stdout, "Value: %s\n", (char *) value); +} + +int main(int argc, const char *argv[]) +{ + Ecore_Con_Eet *ec_eet; + Ecore_Con_Server *svr; + + if (argc < 3) + { + printf("Syntax: \n./ecore_con_eet_server_example \n"); + exit(0); + } + + eina_init(); + eet_init(); + ecore_con_init(); + ece_example_descriptor_init(); // Initialize a eet descriptor. + + svr = ecore_con_server_connect(ECORE_CON_REMOTE_TCP, argv[1], + atoi(argv[2]), NULL); + if (!svr) + { + printf("could not connect to the server.\n"); + exit(1); + } + + ec_eet = ecore_con_eet_client_new(svr); // Associate it with the connection. + + // Register the descriptor to send messages. + ece_example_descriptor_register_descs(ec_eet); + + // Register call backs. + ecore_con_eet_raw_data_callback_add(ec_eet, EXAMPLE_STREAM, + _eet_raw_data_cb, NULL); + ecore_con_eet_server_connect_callback_add(ec_eet, _eet_svr_cnct_cb, NULL); + ecore_con_eet_server_disconnect_callback_add(ec_eet, _eet_svr_discnct_cb, + NULL); + + ecore_main_loop_begin(); + + // Delete all callbacks before closing connection. + ecore_con_eet_server_disconnect_callback_del(ec_eet, _eet_svr_discnct_cb, + NULL); + ecore_con_eet_server_connect_callback_del(ec_eet, _eet_svr_discnct_cb, + NULL); + ecore_con_eet_raw_data_callback_del(ec_eet, EXAMPLE_STREAM); + + // Free the descriptor. + ece_example_descriptor_shutdown(); + + ecore_con_server_del(svr); + ecore_con_shutdown(); + eet_shutdown(); + eina_shutdown(); + + return 0; +} diff --git a/src/examples/ecore/ecore_con_eet_descriptor_example.c b/src/examples/ecore/ecore_con_eet_descriptor_example.c new file mode 100644 index 0000000000..cdf6c8895a --- /dev/null +++ b/src/examples/ecore/ecore_con_eet_descriptor_example.c @@ -0,0 +1,42 @@ +/* + * This file has the description of a eet decriptor which would be used + * in ecore_con_eet_server_example.c and ecore_con_eet_client_example.c. + * The structure for the descriptor is defined in ecore_con_eet_descriptor_example.h. + */ + +#include "ecore_con_eet_descriptor_example.h" + +static Eet_Data_Descriptor *_ece_example_descriptor; + +void +ece_example_descriptor_init(void) +{ + Eet_Data_Descriptor_Class eddc; + + EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, ECE_Example); + _ece_example_descriptor = eet_data_descriptor_stream_new(&eddc); + +#define ECE_EXAMPLE_ADD_BASIC(member, eet_type) \ + EET_DATA_DESCRIPTOR_ADD_BASIC \ + (_ece_example_descriptor, ECE_Example, # member, member, eet_type) + + ECE_EXAMPLE_ADD_BASIC(id, EET_T_INT); + ECE_EXAMPLE_ADD_BASIC(message, EET_T_STRING); + /* not_sending is not being added to the descriptor to show that, + * only those parameters which have been added are sent in the + * communication. + */ +#undef ECE_EXAMPLE_ADD_BASIC +} + +void +ece_example_descriptor_shutdown(void) +{ + eet_data_descriptor_free(_ece_example_descriptor); +} + +void +ece_example_descriptor_register_descs(Ecore_Con_Eet *eet_svr) +{ + ecore_con_eet_register(eet_svr, EXAMPLE_STREAM, _ece_example_descriptor); +} diff --git a/src/examples/ecore/ecore_con_eet_descriptor_example.h b/src/examples/ecore/ecore_con_eet_descriptor_example.h new file mode 100644 index 0000000000..9d71a0c5a3 --- /dev/null +++ b/src/examples/ecore/ecore_con_eet_descriptor_example.h @@ -0,0 +1,20 @@ +#ifndef _ECORE_CON_EET_DESCTRIPOR_EXAMPLE_H +#define _ECORE_CON_EET_DESCTRIPOR_EXAMPLE_H + +#include +#include + +#define EXAMPLE_STREAM "example_stream" + +typedef struct +{ + int id; + char *message; + char *not_sending; +}ECE_Example; + +void ece_example_descriptor_init(void); +void ece_example_descriptor_shutdown(void); +void ece_example_descriptor_register_descs(Ecore_Con_Eet *eet_svr); + +#endif diff --git a/src/examples/ecore/ecore_con_eet_server_example.c b/src/examples/ecore/ecore_con_eet_server_example.c new file mode 100644 index 0000000000..01ae4d9f76 --- /dev/null +++ b/src/examples/ecore/ecore_con_eet_server_example.c @@ -0,0 +1,115 @@ +/* + * An example Eet descriptor is defined in ecore_con_eet_descriptor.c. + * This example creates an Ecore Con Eet descriptor and associates it + * with the Ecore Con Server. The required callbacks are registered. + * This example demonstrates how to send Eet descriptor messages between + * client and server. + * The server runs until all clients associated with it disconnects. + * You can run a single server and then connect multiple clients to this + * to check the message passing. + */ + +//Compile with: +// gcc -o ecore_con_eet_server_example ecore_con_eet_server_example.c ecore_con_eet_descriptor_example.c `pkg-config --cflags --libs eet ecore ecore-con eina` + +#include +#include +#include +#include +#include "ecore_con_eet_descriptor_example.h" + +static int _client_count = 0; + +Eina_Bool +_eet_client_discnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply EINA_UNUSED, Ecore_Con_Client *conn EINA_UNUSED) +{ + _client_count--; + if (!_client_count) + ecore_main_loop_quit(); + + return EINA_FALSE; +} + +Eina_Bool +_eet_client_cnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply, Ecore_Con_Client *conn EINA_UNUSED) +{ + _client_count++; + return EINA_TRUE; +} + +static void +_eet_data_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply, const char *protocol_name, void *value) +{ + char toSend[] = "Received your message.";// The message to send to client. + ECE_Example *received = value;// The message received from client. + + fprintf(stdout, "id: %d\n message: %s\n not_sending: %s\n", + received->id, received->message, received->not_sending); + /* The not_sending would be empty as not_sending is not added in the + * descriptor. So that value is not sent. + */ + + /* Using eet raw send, you can send value of a particular field and + * not the whole structure. + */ + ecore_con_eet_raw_send(reply, protocol_name, "message", toSend, + strlen(toSend)); +} + +int main(int argc, char *argv[]) +{ + Ecore_Con_Eet *ec_eet; + Ecore_Con_Server *svr; + + if (argc < 3) + { + printf("Syntax: \n./ecore_con_eet_server_example \n"); + exit(0); + } + + eina_init(); + eet_init(); + ecore_con_init(); + ece_example_descriptor_init();// Initializing the example eet descriptor. + + svr = ecore_con_server_add(ECORE_CON_REMOTE_TCP, argv[1], + atoi(argv[2]), NULL); + if (!svr) + { + printf("Failed to bind\n"); + exit(1); + } + + ec_eet = ecore_con_eet_server_new(svr); + + // Register the initialized descriptor to the server. + ece_example_descriptor_register_descs(ec_eet); + + // Registering call backs. + ecore_con_eet_data_callback_add(ec_eet, EXAMPLE_STREAM, _eet_data_cb, NULL); + ecore_con_eet_client_connect_callback_add(ec_eet, _eet_client_cnct_cb, + NULL); + ecore_con_eet_client_disconnect_callback_add(ec_eet, _eet_client_discnct_cb, + NULL); + + ecore_main_loop_begin(); + + printf("Server was up for %0.3f seconds\n", + ecore_con_server_uptime_get(svr)); + + // Delete the callbacks before closing connection. + ecore_con_eet_data_callback_del(ec_eet, EXAMPLE_STREAM); + ecore_con_eet_client_connect_callback_del(ec_eet, _eet_client_cnct_cb, + NULL); + ecore_con_eet_client_disconnect_callback_del(ec_eet, _eet_client_discnct_cb, + NULL); + // Delete the eet descriptor. + ece_example_descriptor_shutdown(); + + ecore_con_server_del(svr); + ecore_con_shutdown(); + eet_shutdown(); + eina_shutdown(); + + return 0; +}