Remove dummy headers for Windows
[platform/upstream/libwebsockets.git] / test-server / test-client.c
1 /*
2  * libwebsockets-test-client - libwebsockets test implementation
3  *
4  * Copyright (C) 2011 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <getopt.h>
25 #include <string.h>
26 #include <signal.h>
27
28 #ifndef _WIN32
29 #include <unistd.h>
30 #endif
31
32 #ifdef CMAKE_BUILD
33 #include "lws_config.h"
34 #endif
35
36 #include "../lib/libwebsockets.h"
37
38 static unsigned int opts;
39 static int was_closed;
40 static int deny_deflate;
41 static int deny_mux;
42 static struct libwebsocket *wsi_mirror;
43 static int mirror_lifetime = 0;
44 static volatile int force_exit = 0;
45 static int longlived = 0;
46
47 /*
48  * This demo shows how to connect multiple websockets simultaneously to a
49  * websocket server (there is no restriction on their having to be the same
50  * server just it simplifies the demo).
51  *
52  *  dumb-increment-protocol:  we connect to the server and print the number
53  *                              we are given
54  *
55  *  lws-mirror-protocol: draws random circles, which are mirrored on to every
56  *                              client (see them being drawn in every browser
57  *                              session also using the test server)
58  */
59
60 enum demo_protocols {
61
62         PROTOCOL_DUMB_INCREMENT,
63         PROTOCOL_LWS_MIRROR,
64
65         /* always last */
66         DEMO_PROTOCOL_COUNT
67 };
68
69
70 /* dumb_increment protocol */
71
72 static int
73 callback_dumb_increment(struct libwebsocket_context *this,
74                         struct libwebsocket *wsi,
75                         enum libwebsocket_callback_reasons reason,
76                                                void *user, void *in, size_t len)
77 {
78         switch (reason) {
79
80         case LWS_CALLBACK_CLIENT_ESTABLISHED:
81                 fprintf(stderr, "callback_dumb_increment: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
82                 break;
83
84         case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
85                 fprintf(stderr, "LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
86                 was_closed = 1;
87                 break;
88
89         case LWS_CALLBACK_CLOSED:
90                 fprintf(stderr, "LWS_CALLBACK_CLOSED\n");
91                 was_closed = 1;
92                 break;
93
94         case LWS_CALLBACK_CLIENT_RECEIVE:
95                 ((char *)in)[len] = '\0';
96                 fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
97                 break;
98
99         /* because we are protocols[0] ... */
100
101         case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
102                 if ((strcmp(in, "deflate-stream") == 0) && deny_deflate) {
103                         fprintf(stderr, "denied deflate-stream extension\n");
104                         return 1;
105                 }
106                 if ((strcmp(in, "deflate-frame") == 0) && deny_deflate) {
107                         fprintf(stderr, "denied deflate-frame extension\n");
108                         return 1;
109                 }
110                 if ((strcmp(in, "x-google-mux") == 0) && deny_mux) {
111                         fprintf(stderr, "denied x-google-mux extension\n");
112                         return 1;
113                 }
114
115                 break;
116
117         default:
118                 break;
119         }
120
121         return 0;
122 }
123
124
125 /* lws-mirror_protocol */
126
127
128 static int
129 callback_lws_mirror(struct libwebsocket_context *context,
130                         struct libwebsocket *wsi,
131                         enum libwebsocket_callback_reasons reason,
132                                                void *user, void *in, size_t len)
133 {
134         unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
135                                                   LWS_SEND_BUFFER_POST_PADDING];
136         int l = 0;
137         int n;
138
139         switch (reason) {
140
141         case LWS_CALLBACK_CLIENT_ESTABLISHED:
142
143                 fprintf(stderr, "callback_lws_mirror: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
144
145                 mirror_lifetime = 10 + (random() & 1023);
146                 /* useful to test single connection stability */
147                 if (longlived)
148                         mirror_lifetime += 50000;
149
150                 fprintf(stderr, "opened mirror connection with "
151                                      "%d lifetime\n", mirror_lifetime);
152
153                 /*
154                  * mirror_lifetime is decremented each send, when it reaches
155                  * zero the connection is closed in the send callback.
156                  * When the close callback comes, wsi_mirror is set to NULL
157                  * so a new connection will be opened
158                  */
159
160                 /*
161                  * start the ball rolling,
162                  * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
163                  */
164
165                 libwebsocket_callback_on_writable(context, wsi);
166                 break;
167
168         case LWS_CALLBACK_CLOSED:
169                 fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED mirror_lifetime=%d\n", mirror_lifetime);
170                 wsi_mirror = NULL;
171                 break;
172
173         case LWS_CALLBACK_CLIENT_RECEIVE:
174 /*              fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
175                 break;
176
177         case LWS_CALLBACK_CLIENT_WRITEABLE:
178
179                 for (n = 0; n < 1; n++)
180                         l += sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING + l],
181                                         "c #%06X %d %d %d;",
182                                         (int)random() & 0xffffff,
183                                         (int)random() % 500,
184                                         (int)random() % 250,
185                                         (int)random() % 24);
186
187                 n = libwebsocket_write(wsi,
188                    &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT);
189
190                 if (n < 0)
191                         return -1;
192                 if (n < l) {
193                         lwsl_err("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
194                         return -1;
195                 }
196
197                 mirror_lifetime--;
198                 if (!mirror_lifetime) {
199                         fprintf(stderr, "closing mirror session\n");
200                         return -1;
201                 } else
202                         /* get notified as soon as we can write again */
203                         libwebsocket_callback_on_writable(context, wsi);
204                 break;
205
206         default:
207                 break;
208         }
209
210         return 0;
211 }
212
213
214 /* list of supported protocols and callbacks */
215
216 static struct libwebsocket_protocols protocols[] = {
217         {
218                 "dumb-increment-protocol",
219                 callback_dumb_increment,
220                 0,
221                 20,
222         },
223         {
224                 "lws-mirror-protocol",
225                 callback_lws_mirror,
226                 0,
227                 128,
228         },
229         { NULL, NULL, 0, 0 } /* end */
230 };
231
232 void sighandler(int sig)
233 {
234         force_exit = 1;
235 }
236
237 static struct option options[] = {
238         { "help",       no_argument,            NULL, 'h' },
239         { "debug",      required_argument,      NULL, 'd' },
240         { "port",       required_argument,      NULL, 'p' },
241         { "ssl",        no_argument,            NULL, 's' },
242         { "version",    required_argument,      NULL, 'v' },
243         { "undeflated", no_argument,            NULL, 'u' },
244         { "nomux",      no_argument,            NULL, 'n' },
245         { "longlived",  no_argument,            NULL, 'l' },
246         { NULL, 0, 0, 0 }
247 };
248
249
250 int main(int argc, char **argv)
251 {
252         int n = 0;
253         int ret = 0;
254         int port = 7681;
255         int use_ssl = 0;
256         struct libwebsocket_context *context;
257         const char *address;
258         struct libwebsocket *wsi_dumb;
259         int ietf_version = -1; /* latest */
260         struct lws_context_creation_info info;
261
262         memset(&info, 0, sizeof info);
263
264         fprintf(stderr, "libwebsockets test client\n"
265                         "(C) Copyright 2010-2013 Andy Green <andy@warmcat.com> "
266                                                     "licensed under LGPL2.1\n");
267
268         if (argc < 2)
269                 goto usage;
270
271         while (n >= 0) {
272                 n = getopt_long(argc, argv, "nuv:hsp:d:l", options, NULL);
273                 if (n < 0)
274                         continue;
275                 switch (n) {
276                 case 'd':
277                         lws_set_log_level(atoi(optarg), NULL);
278                         break;
279                 case 's':
280                         use_ssl = 2; /* 2 = allow selfsigned */
281                         break;
282                 case 'p':
283                         port = atoi(optarg);
284                         break;
285                 case 'l':
286                         longlived = 1;
287                         break;
288                 case 'v':
289                         ietf_version = atoi(optarg);
290                         break;
291                 case 'u':
292                         deny_deflate = 1;
293                         break;
294                 case 'n':
295                         deny_mux = 1;
296                         break;
297                 case 'h':
298                         goto usage;
299                 }
300         }
301
302         if (optind >= argc)
303                 goto usage;
304
305         signal(SIGINT, sighandler);
306
307         address = argv[optind];
308
309         /*
310          * create the websockets context.  This tracks open connections and
311          * knows how to route any traffic and which protocol version to use,
312          * and if each connection is client or server side.
313          *
314          * For this client-only demo, we tell it to not listen on any port.
315          */
316
317         info.port = CONTEXT_PORT_NO_LISTEN;
318         info.protocols = protocols;
319 #ifndef LWS_NO_EXTENSIONS
320         info.extensions = libwebsocket_get_internal_extensions();
321 #endif
322         info.gid = -1;
323         info.uid = -1;
324
325         context = libwebsocket_create_context(&info);
326         if (context == NULL) {
327                 fprintf(stderr, "Creating libwebsocket context failed\n");
328                 return 1;
329         }
330
331         /* create a client websocket using dumb increment protocol */
332
333         wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
334                         "/", argv[optind], argv[optind],
335                          protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version);
336
337         if (wsi_dumb == NULL) {
338                 fprintf(stderr, "libwebsocket connect failed\n");
339                 ret = 1;
340                 goto bail;
341         }
342
343         fprintf(stderr, "Waiting for connect...\n");
344
345         /*
346          * sit there servicing the websocket context to handle incoming
347          * packets, and drawing random circles on the mirror protocol websocket
348          * nothing happens until the client websocket connection is
349          * asynchronously established
350          */
351
352         n = 0;
353         while (n >= 0 && !was_closed && !force_exit) {
354                 n = libwebsocket_service(context, 10);
355
356                 if (n < 0)
357                         continue;
358
359                 if (wsi_mirror)
360                         continue;
361
362                 /* create a client websocket using mirror protocol */
363
364                 wsi_mirror = libwebsocket_client_connect(context,
365                         address, port, use_ssl,  "/",
366                         argv[optind], argv[optind],
367                         protocols[PROTOCOL_LWS_MIRROR].name, ietf_version);
368
369                 if (wsi_mirror == NULL) {
370                         fprintf(stderr, "libwebsocket "
371                                               "mirror connect failed\n");
372                         ret = 1;
373                         goto bail;
374                 }
375         }
376
377 bail:
378         fprintf(stderr, "Exiting\n");
379
380         libwebsocket_context_destroy(context);
381
382         return ret;
383
384 usage:
385         fprintf(stderr, "Usage: libwebsockets-test-client "
386                                 "<server address> [--port=<p>] "
387                                 "[--ssl] [-k] [-v <ver>] "
388                                 "[-d <log bitfield>] [-l]\n");
389         return 1;
390 }