summaryrefslogtreecommitdiff
path: root/src/examples
diff options
context:
space:
mode:
authorSrivardhan Hebbar <sri.hebbar@samsung.com>2015-04-22 14:55:33 +0200
committerCedric BAIL <cedric@osg.samsung.com>2015-04-22 15:10:20 +0200
commit9919a428c642af8ecbb13b1bb62beb1488406622 (patch)
tree9b82273a1b9ba504d1c4d3840b8c89cd930f05ea /src/examples
parentd86ed2a46c5f6a7a76fceff7d33ed934f87bb714 (diff)
ecore_con: example program for ecore_con_eet.
Summary: Signed-off-by: Srivardhan Hebbar <sri.hebbar@samsung.com> Reviewers: cedric Subscribers: cedric Differential Revision: https://phab.enlightenment.org/D2323 Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
Diffstat (limited to 'src/examples')
-rw-r--r--src/examples/ecore/.gitignore2
-rw-r--r--src/examples/ecore/ecore_con_eet_client_example.c97
-rw-r--r--src/examples/ecore/ecore_con_eet_descriptor_example.c42
-rw-r--r--src/examples/ecore/ecore_con_eet_descriptor_example.h20
-rw-r--r--src/examples/ecore/ecore_con_eet_server_example.c115
5 files changed, 276 insertions, 0 deletions
diff --git a/src/examples/ecore/.gitignore b/src/examples/ecore/.gitignore
index 700be32..acc3750 100644
--- a/src/examples/ecore/.gitignore
+++ b/src/examples/ecore/.gitignore
@@ -6,6 +6,8 @@
6/ecore_compose_get_example 6/ecore_compose_get_example
7/ecore_con_client_example 7/ecore_con_client_example
8/ecore_con_client_simple_example 8/ecore_con_client_simple_example
9/ecore_con_eet_client_example
10/ecore_con_eet_server_example
9/ecore_con_lookup_example 11/ecore_con_lookup_example
10/ecore_con_server_example 12/ecore_con_server_example
11/ecore_con_server_http_example 13/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 0000000..710df2e
--- /dev/null
+++ b/src/examples/ecore/ecore_con_eet_client_example.c
@@ -0,0 +1,97 @@
1/*
2 * This example shows how to register a eet descriptor to a ecore con client
3 * and send message to ecore con server. The eet descriptor is defined in
4 * ecore_con_eet_descriptor_example.c.
5 */
6
7//Compile with:
8// 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`
9
10#include <stdio.h>
11#include <Ecore.h>
12#include <Ecore_Con.h>
13#include <Eina.h>
14#include "ecore_con_eet_descriptor_example.h"
15
16Eina_Bool
17_eet_svr_discnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply, Ecore_Con_Client *conn EINA_UNUSED)
18{
19 ecore_main_loop_quit();
20 return EINA_FALSE;
21}
22
23Eina_Bool
24_eet_svr_cnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply, Ecore_Con_Client *conn EINA_UNUSED)
25{
26 /* Only id and message parameter are sent to server. not_sending, parameter
27 * is not sent, as it is not added to the eet descriptor. */
28 ECE_Example toSend = {1, "Message from Client.",
29 "This is not sent to server"};
30
31 ecore_con_eet_send(reply, EXAMPLE_STREAM, &toSend);
32 return EINA_TRUE;
33}
34
35static void
36_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)
37{
38 /* Only a perticular parameter's value is sent by Server. */
39 fprintf(stdout, "Section: %s\n", section);
40 fprintf(stdout, "Value: %s\n", (char *) value);
41}
42
43int main(int argc, const char *argv[])
44{
45 Ecore_Con_Eet *ec_eet;
46 Ecore_Con_Server *svr;
47
48 if (argc < 3)
49 {
50 printf("Syntax: \n./ecore_con_eet_server_example <IP> <PORT>\n");
51 exit(0);
52 }
53
54 eina_init();
55 eet_init();
56 ecore_con_init();
57 ece_example_descriptor_init(); // Initialize a eet descriptor.
58
59 svr = ecore_con_server_connect(ECORE_CON_REMOTE_TCP, argv[1],
60 atoi(argv[2]), NULL);
61 if (!svr)
62 {
63 printf("could not connect to the server.\n");
64 exit(1);
65 }
66
67 ec_eet = ecore_con_eet_client_new(svr); // Associate it with the connection.
68
69 // Register the descriptor to send messages.
70 ece_example_descriptor_register_descs(ec_eet);
71
72 // Register call backs.
73 ecore_con_eet_raw_data_callback_add(ec_eet, EXAMPLE_STREAM,
74 _eet_raw_data_cb, NULL);
75 ecore_con_eet_server_connect_callback_add(ec_eet, _eet_svr_cnct_cb, NULL);
76 ecore_con_eet_server_disconnect_callback_add(ec_eet, _eet_svr_discnct_cb,
77 NULL);
78
79 ecore_main_loop_begin();
80
81 // Delete all callbacks before closing connection.
82 ecore_con_eet_server_disconnect_callback_del(ec_eet, _eet_svr_discnct_cb,
83 NULL);
84 ecore_con_eet_server_connect_callback_del(ec_eet, _eet_svr_discnct_cb,
85 NULL);
86 ecore_con_eet_raw_data_callback_del(ec_eet, EXAMPLE_STREAM);
87
88 // Free the descriptor.
89 ece_example_descriptor_shutdown();
90
91 ecore_con_server_del(svr);
92 ecore_con_shutdown();
93 eet_shutdown();
94 eina_shutdown();
95
96 return 0;
97}
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 0000000..cdf6c88
--- /dev/null
+++ b/src/examples/ecore/ecore_con_eet_descriptor_example.c
@@ -0,0 +1,42 @@
1/*
2 * This file has the description of a eet decriptor which would be used
3 * in ecore_con_eet_server_example.c and ecore_con_eet_client_example.c.
4 * The structure for the descriptor is defined in ecore_con_eet_descriptor_example.h.
5 */
6
7#include "ecore_con_eet_descriptor_example.h"
8
9static Eet_Data_Descriptor *_ece_example_descriptor;
10
11void
12ece_example_descriptor_init(void)
13{
14 Eet_Data_Descriptor_Class eddc;
15
16 EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, ECE_Example);
17 _ece_example_descriptor = eet_data_descriptor_stream_new(&eddc);
18
19#define ECE_EXAMPLE_ADD_BASIC(member, eet_type) \
20 EET_DATA_DESCRIPTOR_ADD_BASIC \
21 (_ece_example_descriptor, ECE_Example, # member, member, eet_type)
22
23 ECE_EXAMPLE_ADD_BASIC(id, EET_T_INT);
24 ECE_EXAMPLE_ADD_BASIC(message, EET_T_STRING);
25 /* not_sending is not being added to the descriptor to show that,
26 * only those parameters which have been added are sent in the
27 * communication.
28 */
29#undef ECE_EXAMPLE_ADD_BASIC
30}
31
32void
33ece_example_descriptor_shutdown(void)
34{
35 eet_data_descriptor_free(_ece_example_descriptor);
36}
37
38void
39ece_example_descriptor_register_descs(Ecore_Con_Eet *eet_svr)
40{
41 ecore_con_eet_register(eet_svr, EXAMPLE_STREAM, _ece_example_descriptor);
42}
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 0000000..9d71a0c
--- /dev/null
+++ b/src/examples/ecore/ecore_con_eet_descriptor_example.h
@@ -0,0 +1,20 @@
1#ifndef _ECORE_CON_EET_DESCTRIPOR_EXAMPLE_H
2#define _ECORE_CON_EET_DESCTRIPOR_EXAMPLE_H
3
4#include <Eet.h>
5#include <Ecore_Con_Eet.h>
6
7#define EXAMPLE_STREAM "example_stream"
8
9typedef struct
10{
11 int id;
12 char *message;
13 char *not_sending;
14}ECE_Example;
15
16void ece_example_descriptor_init(void);
17void ece_example_descriptor_shutdown(void);
18void ece_example_descriptor_register_descs(Ecore_Con_Eet *eet_svr);
19
20#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 0000000..01ae4d9
--- /dev/null
+++ b/src/examples/ecore/ecore_con_eet_server_example.c
@@ -0,0 +1,115 @@
1/*
2 * An example Eet descriptor is defined in ecore_con_eet_descriptor.c.
3 * This example creates an Ecore Con Eet descriptor and associates it
4 * with the Ecore Con Server. The required callbacks are registered.
5 * This example demonstrates how to send Eet descriptor messages between
6 * client and server.
7 * The server runs until all clients associated with it disconnects.
8 * You can run a single server and then connect multiple clients to this
9 * to check the message passing.
10 */
11
12//Compile with:
13// 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`
14
15#include <stdio.h>
16#include <Ecore.h>
17#include <Ecore_Con.h>
18#include <Eina.h>
19#include "ecore_con_eet_descriptor_example.h"
20
21static int _client_count = 0;
22
23Eina_Bool
24_eet_client_discnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply EINA_UNUSED, Ecore_Con_Client *conn EINA_UNUSED)
25{
26 _client_count--;
27 if (!_client_count)
28 ecore_main_loop_quit();
29
30 return EINA_FALSE;
31}
32
33Eina_Bool
34_eet_client_cnct_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply, Ecore_Con_Client *conn EINA_UNUSED)
35{
36 _client_count++;
37 return EINA_TRUE;
38}
39
40static void
41_eet_data_cb(void *data EINA_UNUSED, Ecore_Con_Reply *reply, const char *protocol_name, void *value)
42{
43 char toSend[] = "Received your message.";// The message to send to client.
44 ECE_Example *received = value;// The message received from client.
45
46 fprintf(stdout, "id: %d\n message: %s\n not_sending: %s\n",
47 received->id, received->message, received->not_sending);
48 /* The not_sending would be empty as not_sending is not added in the
49 * descriptor. So that value is not sent.
50 */
51
52 /* Using eet raw send, you can send value of a particular field and
53 * not the whole structure.
54 */
55 ecore_con_eet_raw_send(reply, protocol_name, "message", toSend,
56 strlen(toSend));
57}
58
59int main(int argc, char *argv[])
60{
61 Ecore_Con_Eet *ec_eet;
62 Ecore_Con_Server *svr;
63
64 if (argc < 3)
65 {
66 printf("Syntax: \n./ecore_con_eet_server_example <IP> <PORT>\n");
67 exit(0);
68 }
69
70 eina_init();
71 eet_init();
72 ecore_con_init();
73 ece_example_descriptor_init();// Initializing the example eet descriptor.
74
75 svr = ecore_con_server_add(ECORE_CON_REMOTE_TCP, argv[1],
76 atoi(argv[2]), NULL);
77 if (!svr)
78 {
79 printf("Failed to bind\n");
80 exit(1);
81 }
82
83 ec_eet = ecore_con_eet_server_new(svr);
84
85 // Register the initialized descriptor to the server.
86 ece_example_descriptor_register_descs(ec_eet);
87
88 // Registering call backs.
89 ecore_con_eet_data_callback_add(ec_eet, EXAMPLE_STREAM, _eet_data_cb, NULL);
90 ecore_con_eet_client_connect_callback_add(ec_eet, _eet_client_cnct_cb,
91 NULL);
92 ecore_con_eet_client_disconnect_callback_add(ec_eet, _eet_client_discnct_cb,
93 NULL);
94
95 ecore_main_loop_begin();
96
97 printf("Server was up for %0.3f seconds\n",
98 ecore_con_server_uptime_get(svr));
99
100 // Delete the callbacks before closing connection.
101 ecore_con_eet_data_callback_del(ec_eet, EXAMPLE_STREAM);
102 ecore_con_eet_client_connect_callback_del(ec_eet, _eet_client_cnct_cb,
103 NULL);
104 ecore_con_eet_client_disconnect_callback_del(ec_eet, _eet_client_discnct_cb,
105 NULL);
106 // Delete the eet descriptor.
107 ece_example_descriptor_shutdown();
108
109 ecore_con_server_del(svr);
110 ecore_con_shutdown();
111 eet_shutdown();
112 eina_shutdown();
113
114 return 0;
115}