introduce-network-connect-filter-callback.patch
[profile/ivi/libwebsockets.git] / test-server / test-server-extpoll.c
1 /*
2  * libwebsockets-test-server-extpoll - libwebsockets external poll loop sample
3  *
4  * This acts the same as libwebsockets-test-server but works with the poll
5  * loop taken out of libwebsockets and into this app.  It's an example of how
6  * you can integrate libwebsockets polling into an app that already has its
7  * own poll loop.
8  *
9  * Copyright (C) 2010-2011 Andy Green <andy@warmcat.com>
10  *
11  *  This library is free software; you can redistribute it and/or
12  *  modify it under the terms of the GNU Lesser General Public
13  *  License as published by the Free Software Foundation:
14  *  version 2.1 of the License.
15  *
16  *  This library is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  Lesser General Public License for more details.
20  *
21  *  You should have received a copy of the GNU Lesser General Public
22  *  License along with this library; if not, write to the Free Software
23  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24  *  MA  02110-1301  USA
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <getopt.h>
31 #include <string.h>
32 #include <sys/time.h>
33 #include <poll.h>
34
35 #include "../lib/libwebsockets.h"
36
37
38 /*
39  * This demo server shows how to use libwebsockets for one or more
40  * websocket protocols in the same server
41  *
42  * It defines the following websocket protocols:
43  *
44  *  dumb-increment-protocol:  once the socket is opened, an incrementing
45  *                              ascii string is sent down it every 50ms.
46  *                              If you send "reset\n" on the websocket, then
47  *                              the incrementing number is reset to 0.
48  *
49  *  lws-mirror-protocol: copies any received packet to every connection also
50  *                              using this protocol, including the sender
51  */
52
53 #define MAX_POLL_ELEMENTS 100
54 struct pollfd pollfds[100];
55 int count_pollfds = 0;
56
57  
58
59 enum demo_protocols {
60         /* always first */
61         PROTOCOL_HTTP = 0,
62
63         PROTOCOL_DUMB_INCREMENT,
64         PROTOCOL_LWS_MIRROR,
65
66         /* always last */
67         DEMO_PROTOCOL_COUNT
68 };
69
70
71 #define LOCAL_RESOURCE_PATH DATADIR"/libwebsockets-test-server"
72
73 /* this protocol server (always the first one) just knows how to do HTTP */
74
75 static int callback_http(struct libwebsocket *wsi,
76                 enum libwebsocket_callback_reasons reason, void *user,
77                                                            void *in, size_t len)
78 {
79         int n;
80         char client_name[128];
81         char client_ip[128];
82
83         switch (reason) {
84         case LWS_CALLBACK_HTTP:
85                 fprintf(stderr, "serving HTTP URI %s\n", (char *)in);
86
87                 if (in && strcmp(in, "/favicon.ico") == 0) {
88                         if (libwebsockets_serve_http_file(wsi,
89                              LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon"))
90                                 fprintf(stderr, "Failed to send favicon\n");
91                         break;
92                 }
93
94                 /* send the script... when it runs it'll start websockets */
95
96                 if (libwebsockets_serve_http_file(wsi,
97                                   LOCAL_RESOURCE_PATH"/test.html", "text/html"))
98                         fprintf(stderr, "Failed to send HTTP file\n");
99                 break;
100
101         /*
102          * callbacks for managing the external poll() array appear in
103          * protocol 0 callback
104          */
105
106         case LWS_CALLBACK_ADD_POLL_FD:
107                 pollfds[count_pollfds].fd = (int)(long)user;
108                 pollfds[count_pollfds].events = (int)len;
109                 pollfds[count_pollfds++].revents = 0;
110                 break;
111
112         case LWS_CALLBACK_DEL_POLL_FD:
113                 for (n = 0; n < count_pollfds; n++)
114                         if (pollfds[n].fd == (int)(long)user)
115                                 while (n < count_pollfds) {
116                                         pollfds[n] = pollfds[n + 1];
117                                         n++;
118                                 }
119                 count_pollfds--;
120                 break;
121
122         case LWS_CALLBACK_SET_MODE_POLL_FD:
123                 for (n = 0; n < count_pollfds; n++)
124                         if (pollfds[n].fd == (int)(long)user)
125                                 pollfds[n].events |= (int)(long)len;
126                 break;
127
128         case LWS_CALLBACK_CLEAR_MODE_POLL_FD:
129                 for (n = 0; n < count_pollfds; n++)
130                         if (pollfds[n].fd == (int)(long)user)
131                                 pollfds[n].events &= ~(int)(long)len;
132                 break;
133
134         default:
135                 break;
136         }
137
138         return 0;
139 }
140
141 /* dumb_increment protocol */
142
143 /*
144  * one of these is auto-created for each connection and a pointer to the
145  * appropriate instance is passed to the callback in the user parameter
146  *
147  * for this example protocol we use it to individualize the count for each
148  * connection.
149  */
150
151 struct per_session_data__dumb_increment {
152         int number;
153 };
154
155 static int
156 callback_dumb_increment(struct libwebsocket *wsi,
157                         enum libwebsocket_callback_reasons reason,
158                                                void *user, void *in, size_t len)
159 {
160         int n;
161         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 +
162                                                   LWS_SEND_BUFFER_POST_PADDING];
163         unsigned char *p = &buf[LWS_SEND_BUFFER_PRE_PADDING];
164         struct per_session_data__dumb_increment *pss = user;
165
166         switch (reason) {
167
168         case LWS_CALLBACK_ESTABLISHED:
169                 pss->number = 0;
170                 break;
171
172         /*
173          * in this protocol, we just use the broadcast action as the chance to
174          * send our own connection-specific data and ignore the broadcast info
175          * that is available in the 'in' parameter
176          */
177
178         case LWS_CALLBACK_BROADCAST:
179                 n = sprintf((char *)p, "%d", pss->number++);
180                 n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT);
181                 if (n < 0) {
182                         fprintf(stderr, "ERROR writing to socket");
183                         return 1;
184                 }
185                 break;
186
187         case LWS_CALLBACK_RECEIVE:
188                 fprintf(stderr, "rx %d\n", (int)len);
189                 if (len < 6)
190                         break;
191                 if (strcmp(in, "reset\n") == 0)
192                         pss->number = 0;
193                 break;
194
195         default:
196                 break;
197         }
198
199         return 0;
200 }
201
202
203 /* lws-mirror_protocol */
204
205 #define MAX_MESSAGE_QUEUE 64
206
207 struct per_session_data__lws_mirror {
208         struct libwebsocket *wsi;
209         int ringbuffer_tail;
210 };
211
212 struct a_message {
213         void *payload;
214         size_t len;
215 };
216
217 static struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
218 static int ringbuffer_head;
219
220
221 static int
222 callback_lws_mirror(struct libwebsocket *wsi,
223                         enum libwebsocket_callback_reasons reason,
224                                                void *user, void *in, size_t len)
225 {
226         int n;
227         struct per_session_data__lws_mirror *pss = user;
228
229         switch (reason) {
230
231         case LWS_CALLBACK_ESTABLISHED:
232                 pss->ringbuffer_tail = ringbuffer_head;
233                 pss->wsi = wsi;
234                 libwebsocket_callback_on_writable(wsi);
235                 break;
236
237         case LWS_CALLBACK_CLIENT_WRITEABLE:
238
239                 if (pss->ringbuffer_tail != ringbuffer_head) {
240
241                         n = libwebsocket_write(wsi, (unsigned char *)
242                                    ringbuffer[pss->ringbuffer_tail].payload +
243                                    LWS_SEND_BUFFER_PRE_PADDING,
244                                    ringbuffer[pss->ringbuffer_tail].len,
245                                                                 LWS_WRITE_TEXT);
246
247                         if (n < 0) {
248                                 fprintf(stderr, "ERROR writing to socket");
249                                 exit(1);
250                         }
251
252                         if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
253                                 pss->ringbuffer_tail = 0;
254                         else
255                                 pss->ringbuffer_tail++;
256
257                         if (((ringbuffer_head - pss->ringbuffer_tail) %
258                                   MAX_MESSAGE_QUEUE) < (MAX_MESSAGE_QUEUE - 15))
259                                 libwebsocket_rx_flow_control(wsi, 1);
260
261                         libwebsocket_callback_on_writable(wsi);
262
263                 }
264                 break;
265
266         case LWS_CALLBACK_BROADCAST:
267                 n = libwebsocket_write(wsi, in, len, LWS_WRITE_TEXT);
268                 if (n < 0)
269                         fprintf(stderr, "mirror write failed\n");
270                 break;
271
272         case LWS_CALLBACK_RECEIVE:
273
274                 if (ringbuffer[ringbuffer_head].payload)
275                         free(ringbuffer[ringbuffer_head].payload);
276
277                 ringbuffer[ringbuffer_head].payload =
278                                 malloc(LWS_SEND_BUFFER_PRE_PADDING + len +
279                                                   LWS_SEND_BUFFER_POST_PADDING);
280                 ringbuffer[ringbuffer_head].len = len;
281                 memcpy((char *)ringbuffer[ringbuffer_head].payload +
282                                           LWS_SEND_BUFFER_PRE_PADDING, in, len);
283                 if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
284                         ringbuffer_head = 0;
285                 else
286                         ringbuffer_head++;
287
288                 if (((ringbuffer_head - pss->ringbuffer_tail) %
289                                   MAX_MESSAGE_QUEUE) > (MAX_MESSAGE_QUEUE - 10))
290                         libwebsocket_rx_flow_control(wsi, 0);
291
292                 libwebsocket_callback_on_writable_all_protocol(
293                                                libwebsockets_get_protocol(wsi));
294                 break;
295
296         default:
297                 break;
298         }
299
300         return 0;
301 }
302
303
304 /* list of supported protocols and callbacks */
305
306 static struct libwebsocket_protocols protocols[] = {
307         /* first protocol must always be HTTP handler */
308         [PROTOCOL_HTTP] = {
309                 .name = "http-only",
310                 .callback = callback_http,
311         },
312         [PROTOCOL_DUMB_INCREMENT] = {
313                 .name = "dumb-increment-protocol",
314                 .callback = callback_dumb_increment,
315                 .per_session_data_size =
316                                 sizeof(struct per_session_data__dumb_increment),
317         },
318         [PROTOCOL_LWS_MIRROR] = {
319                 .name = "lws-mirror-protocol",
320                 .callback = callback_lws_mirror,
321                 .per_session_data_size =
322                                 sizeof(struct per_session_data__lws_mirror),
323         },
324         [DEMO_PROTOCOL_COUNT] = {  /* end of list */
325                 .callback = NULL
326         }
327 };
328
329 static struct option options[] = {
330         { "help",       no_argument,            NULL, 'h' },
331         { "port",       required_argument,      NULL, 'p' },
332         { "ssl",        no_argument,            NULL, 's' },
333         { "killmask",   no_argument,            NULL, 'k' },
334         { NULL, 0, 0, 0 }
335 };
336
337 int main(int argc, char **argv)
338 {
339         int n = 0;
340         const char *cert_path =
341                             LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem";
342         const char *key_path =
343                         LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem";
344         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1024 +
345                                                   LWS_SEND_BUFFER_POST_PADDING];
346         int port = 7681;
347         int use_ssl = 0;
348         struct libwebsocket_context *context;
349         int opts = 0;
350         unsigned int oldus = 0;
351
352         fprintf(stderr, "libwebsockets test server with external poll()\n"
353                         "(C) Copyright 2010-2011 Andy Green <andy@warmcat.com> "
354                                                     "licensed under LGPL2.1\n");
355
356         while (n >= 0) {
357                 n = getopt_long(argc, argv, "khsp:", options, NULL);
358                 if (n < 0)
359                         continue;
360                 switch (n) {
361                 case 's':
362                         use_ssl = 1;
363                         break;
364                 case 'k':
365                         opts = LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK;
366                         break;
367                 case 'p':
368                         port = atoi(optarg);
369                         break;
370                 case 'h':
371                         fprintf(stderr, "Usage: test-server "
372                                              "[--port=<p>] [--ssl]\n");
373                         exit(1);
374                 }
375         }
376
377         if (!use_ssl)
378                 cert_path = key_path = NULL;
379
380         context = libwebsocket_create_context(port, protocols, cert_path,
381                                                 key_path, -1, -1, opts);
382         if (context == NULL) {
383                 fprintf(stderr, "libwebsocket init failed\n");
384                 return -1;
385         }
386
387         buf[LWS_SEND_BUFFER_PRE_PADDING] = 'x';
388
389         /*
390          * This is an example of an existing application's explicit poll()
391          * loop that libwebsockets can integrate with.
392          */
393
394         while (1) {
395                 struct timeval tv;
396
397                 /*
398                  * this represents an existing server's single poll action
399                  * which also includes libwebsocket sockets
400                  */
401
402                 n = poll(pollfds, count_pollfds, 25);
403                 if (n < 0)
404                         goto done;
405
406                 if (n)
407                         for (n = 0; n < count_pollfds; n++)
408                                 if (pollfds[n].revents)
409                                         /*
410                                         * returns immediately if the fd does not
411                                         * match anything under libwebsockets
412                                         * control
413                                         */
414                                         libwebsocket_service_fd(context,
415                                                                    &pollfds[n]);
416
417                 /* do our broadcast periodically */
418
419                 gettimeofday(&tv, NULL);
420
421                 /*
422                  * This broadcasts to all dumb-increment-protocol connections
423                  * at 20Hz.
424                  *
425                  * We're just sending a character 'x', in these examples the
426                  * callbacks send their own per-connection content.
427                  *
428                  * You have to send something with nonzero length to get the
429                  * callback actions delivered.
430                  *
431                  * We take care of pre-and-post padding allocation.
432                  */
433
434                 if (((unsigned int)tv.tv_usec - oldus) > 50000) {
435                         libwebsockets_broadcast(
436                                         &protocols[PROTOCOL_DUMB_INCREMENT],
437                                         &buf[LWS_SEND_BUFFER_PRE_PADDING], 1);
438                         oldus = tv.tv_usec;
439                 }
440         }
441
442 done:
443         libwebsocket_context_destroy(context);
444
445         return 0;
446 }