Tizen 2.1 base
[framework/uifw/ecore.git] / src / examples / ecore_con_server_http_example.c
1 #include <stdio.h>
2 #include <sys/time.h>
3 #include <Ecore.h>
4 #include <Ecore_Con.h>
5
6 #ifdef HAVE_CONFIG_H
7 # include "config.h"
8 #else
9 # define __UNUSED__
10 #endif
11
12 static const char response_template[] =
13   "HTTP/1.0 200 OK\r\n"
14   "Server: Ecore_Con custom server\r\n"
15   "Content-Length: %zd\r\n"
16   "Content-Type: text/html; charset=UTF-8\r\n"
17   "Set-Cookie: MYCOOKIE=1; path=/; expires=%s\r\n"
18   "Set-Cookie: SESSIONCOOKIE=1; path=/\r\n"
19   "\r\n"
20   "%s";
21
22 struct _Client
23 {
24    int sdata;
25 };
26
27 Eina_Bool
28 _add(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Add *ev)
29 {
30    struct _Client *client = malloc(sizeof(*client));
31    client->sdata = 0;
32    static char buf[4096];
33    char welcome[] = "Welcome to Ecore_Con server!";
34    time_t t;
35
36    printf("Client with ip %s, port %d, connected = %d!\n",
37           ecore_con_client_ip_get(ev->client),
38           ecore_con_client_port_get(ev->client),
39           ecore_con_client_connected_get(ev->client));
40
41    ecore_con_client_data_set(ev->client, client);
42
43    t = time(NULL);
44    t += 60 * 60 * 24;
45    snprintf(buf, sizeof(buf), response_template, sizeof(welcome) - 1, ctime(&t), welcome);
46
47    ecore_con_client_send(ev->client, buf, strlen(buf));
48    ecore_con_client_flush(ev->client);
49
50    return ECORE_CALLBACK_RENEW;
51 }
52
53 Eina_Bool
54 _del(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Del *ev)
55 {
56    struct _Client *client;
57
58    if (!ev->client)
59      return ECORE_CALLBACK_RENEW;
60
61    client = ecore_con_client_data_get(ev->client);
62
63    printf("Lost client with ip %s!\n", ecore_con_client_ip_get(ev->client));
64    printf("Total data received from this client: %d\n", client->sdata);
65    printf("Client was connected for %0.3f seconds.\n",
66           ecore_con_client_uptime_get(ev->client));
67
68    if (client)
69      free(client);
70
71    ecore_con_client_del(ev->client);
72
73    return ECORE_CALLBACK_RENEW;
74 }
75
76 Eina_Bool
77 _data(void *data __UNUSED__, int type __UNUSED__, Ecore_Con_Event_Client_Data *ev)
78 {
79    char fmt[128];
80    struct _Client *client = ecore_con_client_data_get(ev->client);
81
82    snprintf(fmt, sizeof(fmt),
83             "\nReceived %i bytes from client %s port %d:\n"
84             ">>>>>\n"
85             "%%.%is\n"
86             ">>>>>\n\n",
87             ev->size, ecore_con_client_ip_get(ev->client),
88             ecore_con_client_port_get(ev->client), ev->size);
89
90    printf(fmt, ev->data);
91
92    client->sdata += ev->size;
93
94    return ECORE_CALLBACK_RENEW;
95 }
96
97 int
98 main(void)
99 {
100    Ecore_Con_Server *svr;
101    Ecore_Con_Client *cl;
102    const Eina_List *clients, *l;
103
104    eina_init();
105    ecore_init();
106    ecore_con_init();
107
108    if (!(svr = ecore_con_server_add(ECORE_CON_REMOTE_TCP, "127.0.0.1", 8080, NULL)))
109      exit(1);
110
111    ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD, (Ecore_Event_Handler_Cb)_add, NULL);
112    ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL, (Ecore_Event_Handler_Cb)_del, NULL);
113    ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA, (Ecore_Event_Handler_Cb)_data, NULL);
114    ecore_con_server_client_limit_set(svr, 3, 0);
115
116    ecore_main_loop_begin();
117
118    clients = ecore_con_server_clients_get(svr);
119    printf("Clients still connected to this server when exiting: %d\n",
120           eina_list_count(clients));
121    EINA_LIST_FOREACH(clients, l, cl)
122      {
123         printf("%s\n", ecore_con_client_ip_get(cl));
124         free(ecore_con_client_data_get(cl));
125      }
126
127    printf("Server was up for %0.3f seconds\n",
128           ecore_con_server_uptime_get(svr));
129
130    ecore_con_shutdown();
131    ecore_shutdown();
132    eina_shutdown();
133
134    return 0;
135 }
136