From 06263c9eb38685ec6815622237d203f7e8de5cae Mon Sep 17 00:00:00 2001 From: Gustavo Sverzut Barbieri Date: Tue, 22 Nov 2016 17:27:16 -0200 Subject: [PATCH] add ecore_ipc_{client,server}_example. Ecore_Ipc lacked examples so we can explore its API (and check if it work :-P) --- src/examples/ecore/.gitignore | 2 + src/examples/ecore/Makefile.am | 20 +- src/examples/ecore/ecore_ipc_client_example.c | 224 ++++++++++++++++++ src/examples/ecore/ecore_ipc_server_example.c | 214 +++++++++++++++++ 4 files changed, 458 insertions(+), 2 deletions(-) create mode 100644 src/examples/ecore/ecore_ipc_client_example.c create mode 100644 src/examples/ecore/ecore_ipc_server_example.c diff --git a/src/examples/ecore/.gitignore b/src/examples/ecore/.gitignore index a00f15f14e..e8e8aa9bdb 100644 --- a/src/examples/ecore/.gitignore +++ b/src/examples/ecore/.gitignore @@ -63,3 +63,5 @@ /*.pem /efl_net_session_example /efl_net_control_example +/ecore_ipc_server_example +/ecore_ipc_client_example diff --git a/src/examples/ecore/Makefile.am b/src/examples/ecore/Makefile.am index 2fa4350638..bd5796a5ed 100644 --- a/src/examples/ecore/Makefile.am +++ b/src/examples/ecore/Makefile.am @@ -14,6 +14,7 @@ AM_CPPFLAGS = \ -I$(top_srcdir)/src/lib/ecore_imf \ -I$(top_srcdir)/src/lib/ecore_imf_evas \ -I$(top_srcdir)/src/lib/ecore_con \ +-I$(top_srcdir)/src/lib/ecore_ipc \ -I$(top_srcdir)/src/lib/ecore_evas \ -I$(top_srcdir)/src/lib/ecore_audio \ -I$(top_builddir)/src/lib/eina \ @@ -29,6 +30,7 @@ AM_CPPFLAGS = \ -I$(top_builddir)/src/lib/ecore_imf \ -I$(top_builddir)/src/lib/ecore_imf_evas \ -I$(top_builddir)/src/lib/ecore_con \ +-I$(top_builddir)/src/lib/ecore_ipc \ -I$(top_builddir)/src/lib/ecore_evas \ -I$(top_builddir)/src/lib/ecore_audio \ -DPACKAGE_EXAMPLES_DIR=\".\" \ @@ -90,7 +92,9 @@ efl_net_dialer_udp_example \ efl_net_socket_ssl_dialer_example \ efl_net_socket_ssl_server_example \ efl_net_session_example \ -efl_net_control_example +efl_net_control_example \ +ecore_ipc_server_example \ +ecore_ipc_client_example ECORE_COMMON_LDADD = \ @@ -129,6 +133,10 @@ ECORE_CON_COMMON_LDADD = \ $(top_builddir)/src/lib/ecore_con/libecore_con.la \ $(ECORE_COMMON_LDADD) +ECORE_IPC_COMMON_LDADD = \ +$(top_builddir)/src/lib/ecore_ipc/libecore_ipc.la \ +$(ECORE_CON_COMMON_LDADD) + ecore_animator_example_SOURCES = ecore_animator_example.c ecore_animator_example_LDADD = $(ECORE_EVAS_COMMON_LDADD) @@ -342,6 +350,12 @@ efl_net_session_example_LDADD = $(ECORE_CON_COMMON_LDADD) efl_net_control_example_SOURCES = efl_net_control_example.c efl_net_control_example_LDADD = $(ECORE_CON_COMMON_LDADD) +ecore_ipc_server_example_SOURCES = ecore_ipc_server_example.c +ecore_ipc_server_example_LDADD = $(ECORE_IPC_COMMON_LDADD) + +ecore_ipc_client_example_SOURCES = ecore_ipc_client_example.c +ecore_ipc_client_example_LDADD = $(ECORE_IPC_COMMON_LDADD) + SRCS = \ ecore_animator_example.c \ ecore_buffer_example.c \ @@ -401,7 +415,9 @@ efl_net_dialer_udp_example.c \ efl_net_socket_ssl_dialer_example.c \ efl_net_socket_ssl_server_example.c \ efl_net_session_example.c \ -efl_net_control_example.c +efl_net_control_example.c \ +ecore_ipc_server_example.c \ +ecore_ipc_client_example.c %.pem: echo -e "US\nOR\nPortland\nXPTO Ltd\n\nlocalhost\nroot@localhost\n" | openssl req -new -x509 -days 30 -nodes -out $@ -keyout $@ diff --git a/src/examples/ecore/ecore_ipc_client_example.c b/src/examples/ecore/ecore_ipc_client_example.c new file mode 100644 index 0000000000..a6379a1407 --- /dev/null +++ b/src/examples/ecore/ecore_ipc_client_example.c @@ -0,0 +1,224 @@ +#include +#include +#include + +static int retval = EXIT_SUCCESS; +static Ecore_Ipc_Server *server = NULL; +static Eina_Bool do_flush = EINA_FALSE; + +static Eina_Bool +_server_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) +{ + Ecore_Ipc_Event_Server_Add *ev = event; + + printf("INFO: server added %p: %s\n", ev->server, ecore_ipc_server_ip_get(ev->server)); + + puts("INFO: start typing some lines of text to send to server..."); + + server = ev->server; + + return ECORE_CALLBACK_RENEW; +} + +static Eina_Bool +_server_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) +{ + Ecore_Ipc_Event_Server_Del *ev = event; + + printf("INFO: server deleted %p: %s\n", ev->server, ecore_ipc_server_ip_get(ev->server)); + + server = NULL; + ecore_main_loop_quit(); + return ECORE_CALLBACK_RENEW; +} + +static Eina_Bool +_server_data(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) +{ + Ecore_Ipc_Event_Server_Data *ev = event; + + printf("INFO: server data %p: %s\n" + "INFO: - major: %d, minor: %d, ref: %d (to: %d)\n" + "INFO: - response?: %d\n" + "INFO: - size: %d\n" + "-- BEGIN DATA --\n", + ev->server, ecore_ipc_server_ip_get(ev->server), + ev->major, ev->minor, ev->ref, ev->ref_to, + ev->response, + ev->size); + + fwrite(ev->data, ev->size, 1, stdout); + puts("-- END DATA --"); + + return ECORE_CALLBACK_RENEW; +} + +static Eina_Bool +_on_stdin(void *data EINA_UNUSED, Ecore_Fd_Handler *fdh EINA_UNUSED) +{ + char *line = NULL; + size_t len = 0; + ssize_t r = getline(&line, &len, stdin); + + if (r < 0) + { + fprintf(stderr, "ERROR: could not read from stdin: %s\n", strerror(errno)); + return ECORE_CALLBACK_CANCEL; + } + + if (!server) + fputs("WARNING: not connected to server, ignored input.\n", stderr); + else + { + ecore_ipc_server_send(server, 1, 2, 0, 0, 0, line, r); + printf("INFO: sent %zd bytes to server.\n", r); + if (do_flush) ecore_ipc_server_flush(server); + } + + free(line); + + return ECORE_CALLBACK_RENEW; +} + +static const char *types_strs[] = { + "user", + "system", + "remote", + NULL +}; + +static const Ecore_Getopt options = { + "ecore_ipc_client_example", /* program name */ + NULL, /* usage line */ + "1", /* version */ + "(C) 2016 Enlightenment Project", /* copyright */ + "BSD 2-Clause", /* license */ + /* long description, may be multiline and contain \n */ + "Example of ecore_ipc_server_connect() usage.\n", + EINA_FALSE, + { + ECORE_GETOPT_CHOICE('t', "type", "Server type to use, defaults to 'user'", types_strs), + ECORE_GETOPT_STORE_TRUE('S', "ssl", "Use SSL"), + ECORE_GETOPT_STORE_TRUE('P', "no-proxy", "Do not use SOCKS proxy for remote connections."), + + ECORE_GETOPT_STORE_INT('s', "max-size", "Maximum size (in bytes) for messages."), + + ECORE_GETOPT_STORE_TRUE('f', "flush", "Force a flush after every send call."), + + ECORE_GETOPT_VERSION('V', "version"), + ECORE_GETOPT_COPYRIGHT('C', "copyright"), + ECORE_GETOPT_LICENSE('L', "license"), + ECORE_GETOPT_HELP('h', "help"), + + ECORE_GETOPT_STORE_METAVAR_STR(0, NULL, "The server name.", "name"), + ECORE_GETOPT_STORE_METAVAR_INT(0, NULL, "The server port.", "port"), + + ECORE_GETOPT_SENTINEL + } +}; + +int +main(int argc, char **argv) +{ + Ecore_Ipc_Type type; + char *name = NULL; + char *type_choice = NULL; + int port = -1; + int max_size = -1; + Eina_Bool use_ssl = EINA_FALSE; + Eina_Bool no_proxy = EINA_FALSE; + Eina_Bool quit_option = EINA_FALSE; + Ecore_Getopt_Value values[] = { + ECORE_GETOPT_VALUE_STR(type_choice), + ECORE_GETOPT_VALUE_BOOL(use_ssl), + ECORE_GETOPT_VALUE_BOOL(no_proxy), + + ECORE_GETOPT_VALUE_INT(max_size), + + ECORE_GETOPT_VALUE_BOOL(do_flush), + + /* standard block to provide version, copyright, license and help */ + ECORE_GETOPT_VALUE_BOOL(quit_option), /* -V/--version quits */ + ECORE_GETOPT_VALUE_BOOL(quit_option), /* -C/--copyright quits */ + ECORE_GETOPT_VALUE_BOOL(quit_option), /* -L/--license quits */ + ECORE_GETOPT_VALUE_BOOL(quit_option), /* -h/--help quits */ + + /* positional argument */ + ECORE_GETOPT_VALUE_STR(name), + ECORE_GETOPT_VALUE_INT(port), + + ECORE_GETOPT_VALUE_NONE /* sentinel */ + }; + int args; + Ecore_Ipc_Server *server; + Ecore_Event_Handler *handlers[3]; + + ecore_init(); + ecore_ipc_init(); + + args = ecore_getopt_parse(&options, values, argc, argv); + if (args < 0) + { + fputs("ERROR: Could not parse command line options.\n", stderr); + retval = EXIT_FAILURE; + goto end; + } + + if (quit_option) goto end; + + args = ecore_getopt_parse_positional(&options, values, argc, argv, args); + if (args < 0) + { + fputs("ERROR: Could not parse positional arguments.\n", stderr); + retval = EXIT_FAILURE; + goto end; + } + + if (!type_choice) type_choice = "user"; + + if (strcmp(type_choice, "user") == 0) + type = ECORE_IPC_LOCAL_USER; + else if (strcmp(type_choice, "system") == 0) + type = ECORE_IPC_LOCAL_SYSTEM; + else if (strcmp(type_choice, "remote") == 0) + type = ECORE_IPC_REMOTE_SYSTEM; + else + { + fprintf(stderr, "ERROR: unsupported --type/-t '%s'\n", type_choice); + retval = EXIT_FAILURE; + goto end; + } + + handlers[0] = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_ADD, _server_add, NULL); + handlers[1] = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DEL, _server_del, NULL); + handlers[2] = ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DATA, _server_data, NULL); + + ecore_main_fd_handler_add(STDIN_FILENO, ECORE_FD_READ, _on_stdin, NULL, NULL, NULL); + + if (use_ssl) type |= ECORE_IPC_USE_SSL; + if (no_proxy) type |= ECORE_IPC_NO_PROXY; + + server = ecore_ipc_server_connect(type, name, port, NULL); + if (!server) + { + fprintf(stderr, "ERROR: Could not connect to server type=%#x, name=%s, port=%d\n", type, name, port); + goto end; + } + + ecore_ipc_server_data_size_max_set(server, max_size); + + ecore_main_loop_begin(); + + ecore_ipc_server_del(server); + server = NULL; + + ecore_event_handler_del(handlers[0]); + ecore_event_handler_del(handlers[1]); + ecore_event_handler_del(handlers[2]); + + end: + ecore_ipc_shutdown(); + ecore_shutdown(); + + return retval; +} diff --git a/src/examples/ecore/ecore_ipc_server_example.c b/src/examples/ecore/ecore_ipc_server_example.c new file mode 100644 index 0000000000..124e555517 --- /dev/null +++ b/src/examples/ecore/ecore_ipc_server_example.c @@ -0,0 +1,214 @@ +#include +#include +#include + +static int retval = EXIT_SUCCESS; +static int max_size = -1; +static Eina_Bool echo = EINA_FALSE; +static Eina_Bool do_flush = EINA_FALSE; + +static Eina_Bool +_client_add(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) +{ + Ecore_Ipc_Event_Client_Add *ev = event; + + ecore_ipc_client_data_size_max_set(ev->client, max_size); + + printf("INFO: client added %p: %s\n", ev->client, ecore_ipc_client_ip_get(ev->client)); + + if (!echo) + { + ecore_ipc_client_send(ev->client, 1, 2, 3, 0, EINA_TRUE, + "Hello World!", strlen("Hello World!")); + if (do_flush) ecore_ipc_client_flush(ev->client); + } + + return ECORE_CALLBACK_RENEW; +} + +static Eina_Bool +_client_del(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) +{ + Ecore_Ipc_Event_Client_Del *ev = event; + + printf("INFO: client deleted %p: %s\n", ev->client, ecore_ipc_client_ip_get(ev->client)); + return ECORE_CALLBACK_RENEW; +} + +static Eina_Bool +_client_data(void *data EINA_UNUSED, int type EINA_UNUSED, void *event) +{ + Ecore_Ipc_Event_Client_Data *ev = event; + + printf("INFO: client data %p: %s\n" + "INFO: - major: %d, minor: %d, ref: %d (to: %d)\n" + "INFO: - response?: %d\n" + "INFO: - size: %d\n" + "-- BEGIN DATA --\n", + ev->client, ecore_ipc_client_ip_get(ev->client), + ev->major, ev->minor, ev->ref, ev->ref_to, + ev->response, + ev->size); + + fwrite(ev->data, ev->size, 1, stdout); + puts("-- END DATA --"); + + if (echo) + { + ecore_ipc_client_send(ev->client, ev->major, ev->minor, + ev->ref + 1, + ev->ref, 0, ev->data, ev->size); + if (do_flush) ecore_ipc_client_flush(ev->client); + } + + return ECORE_CALLBACK_RENEW; +} + +static const char *types_strs[] = { + "user", + "system", + "remote", + NULL +}; + +static const Ecore_Getopt options = { + "ecore_ipc_server_example", /* program name */ + NULL, /* usage line */ + "1", /* version */ + "(C) 2016 Enlightenment Project", /* copyright */ + "BSD 2-Clause", /* license */ + /* long description, may be multiline and contain \n */ + "Example of ecore_ipc_server_add() usage.\n", + EINA_FALSE, + { + ECORE_GETOPT_CHOICE('t', "type", "Server type to use, defaults to 'user'", types_strs), + ECORE_GETOPT_STORE_TRUE('S', "ssl", "Use SSL"), + + ECORE_GETOPT_STORE_INT('s', "max-size", "Maximum size (in bytes) for messages."), + + ECORE_GETOPT_STORE_INT('l', "clients-limit", + "If set will limit number of clients to accept"), + ECORE_GETOPT_STORE_TRUE('r', "clients-reject-excess", + "Immediately reject excess clients (over limit)"), + + ECORE_GETOPT_STORE_TRUE('e', "echo", + "Behave as 'echo' server, send back to client all the data receive"), + ECORE_GETOPT_STORE_TRUE('f', "flush", "Force a flush after every send call."), + + ECORE_GETOPT_VERSION('V', "version"), + ECORE_GETOPT_COPYRIGHT('C', "copyright"), + ECORE_GETOPT_LICENSE('L', "license"), + ECORE_GETOPT_HELP('h', "help"), + + ECORE_GETOPT_STORE_METAVAR_STR(0, NULL, "The server name.", "name"), + ECORE_GETOPT_STORE_METAVAR_INT(0, NULL, "The server port.", "port"), + + ECORE_GETOPT_SENTINEL + } +}; + +int +main(int argc, char **argv) +{ + Ecore_Ipc_Type type; + char *name = NULL; + char *type_choice = NULL; + int clients_limit = -1; + int port = -1; + Eina_Bool use_ssl = EINA_FALSE; + Eina_Bool clients_reject_excess = EINA_FALSE; + Eina_Bool quit_option = EINA_FALSE; + Ecore_Getopt_Value values[] = { + ECORE_GETOPT_VALUE_STR(type_choice), + ECORE_GETOPT_VALUE_BOOL(use_ssl), + + ECORE_GETOPT_VALUE_INT(max_size), + + ECORE_GETOPT_VALUE_INT(clients_limit), + ECORE_GETOPT_VALUE_BOOL(clients_reject_excess), + + ECORE_GETOPT_VALUE_BOOL(echo), + ECORE_GETOPT_VALUE_BOOL(do_flush), + + /* standard block to provide version, copyright, license and help */ + ECORE_GETOPT_VALUE_BOOL(quit_option), /* -V/--version quits */ + ECORE_GETOPT_VALUE_BOOL(quit_option), /* -C/--copyright quits */ + ECORE_GETOPT_VALUE_BOOL(quit_option), /* -L/--license quits */ + ECORE_GETOPT_VALUE_BOOL(quit_option), /* -h/--help quits */ + + /* positional argument */ + ECORE_GETOPT_VALUE_STR(name), + ECORE_GETOPT_VALUE_INT(port), + + ECORE_GETOPT_VALUE_NONE /* sentinel */ + }; + int args; + Ecore_Ipc_Server *server; + Ecore_Event_Handler *handlers[3]; + + ecore_init(); + ecore_ipc_init(); + + args = ecore_getopt_parse(&options, values, argc, argv); + if (args < 0) + { + fputs("ERROR: Could not parse command line options.\n", stderr); + retval = EXIT_FAILURE; + goto end; + } + + if (quit_option) goto end; + + args = ecore_getopt_parse_positional(&options, values, argc, argv, args); + if (args < 0) + { + fputs("ERROR: Could not parse positional arguments.\n", stderr); + retval = EXIT_FAILURE; + goto end; + } + + if (!type_choice) type_choice = "user"; + + if (strcmp(type_choice, "user") == 0) + type = ECORE_IPC_LOCAL_USER; + else if (strcmp(type_choice, "system") == 0) + type = ECORE_IPC_LOCAL_SYSTEM; + else if (strcmp(type_choice, "remote") == 0) + type = ECORE_IPC_REMOTE_SYSTEM; + else + { + fprintf(stderr, "ERROR: unsupported --type/-t '%s'\n", type_choice); + retval = EXIT_FAILURE; + goto end; + } + + handlers[0] = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_ADD, _client_add, NULL); + handlers[1] = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DEL, _client_del, NULL); + handlers[2] = ecore_event_handler_add(ECORE_IPC_EVENT_CLIENT_DATA, _client_data, NULL); + + if (use_ssl) type |= ECORE_IPC_USE_SSL; + + server = ecore_ipc_server_add(type, name, port, NULL); + if (!server) + { + fprintf(stderr, "ERROR: Could not create server type=%#x, name=%s, port=%d\n", type, name, port); + goto end; + } + + ecore_ipc_server_client_limit_set(server, clients_limit, clients_reject_excess); + + ecore_main_loop_begin(); + + ecore_ipc_server_del(server); + server = NULL; + + ecore_event_handler_del(handlers[0]); + ecore_event_handler_del(handlers[1]); + ecore_event_handler_del(handlers[2]); + + end: + ecore_ipc_shutdown(); + ecore_shutdown(); + + return retval; +}