cleanup test app startup messages
[platform/upstream/libwebsockets.git] / test-server / test-client.c
1 /*
2  * libwebsockets-test-client - libwebsockets test implementation
3  *
4  * Copyright (C) 2011-2016 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 #ifdef _WIN32
29 #define random rand
30 #include "gettimeofday.h"
31 #else
32 #include <syslog.h>
33 #include <sys/time.h>
34 #include <unistd.h>
35 #endif
36
37 #include "../lib/libwebsockets.h"
38
39 static int deny_deflate, deny_mux, longlived, mirror_lifetime;
40 static struct lws *wsi_dumb, *wsi_mirror;
41 static volatile int force_exit;
42 static unsigned int opts;
43
44 /*
45  * This demo shows how to connect multiple websockets simultaneously to a
46  * websocket server (there is no restriction on their having to be the same
47  * server just it simplifies the demo).
48  *
49  *  dumb-increment-protocol:  we connect to the server and print the number
50  *                              we are given
51  *
52  *  lws-mirror-protocol: draws random circles, which are mirrored on to every
53  *                              client (see them being drawn in every browser
54  *                              session also using the test server)
55  */
56
57 enum demo_protocols {
58
59         PROTOCOL_DUMB_INCREMENT,
60         PROTOCOL_LWS_MIRROR,
61
62         /* always last */
63         DEMO_PROTOCOL_COUNT
64 };
65
66
67 /*
68  * dumb_increment protocol
69  *
70  * since this also happens to be protocols[0], some callbacks that are not
71  * bound to a specific protocol also turn up here.
72  */
73
74 static int
75 callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
76                         void *user, void *in, size_t len)
77 {
78         switch (reason) {
79
80         case LWS_CALLBACK_CLIENT_ESTABLISHED:
81                 lwsl_info("dumb: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
82                 break;
83
84         case LWS_CALLBACK_CLOSED:
85                 lwsl_notice("dumb: LWS_CALLBACK_CLOSED\n");
86                 wsi_dumb = NULL;
87                 break;
88
89         case LWS_CALLBACK_CLIENT_RECEIVE:
90                 ((char *)in)[len] = '\0';
91                 lwsl_info("rx %d '%s'\n", (int)len, (char *)in);
92                 break;
93
94         /* because we are protocols[0] ... */
95
96         case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
97                 if (wsi == wsi_dumb) {
98                         lwsl_err("dumb: LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
99                         wsi_dumb = NULL;
100                 }
101                 if (wsi == wsi_mirror) {
102                         lwsl_err("mirror: LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
103                         wsi_mirror = NULL;
104                 }
105                 break;
106
107         case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
108                 if ((strcmp(in, "deflate-stream") == 0) && deny_deflate) {
109                         lwsl_notice("denied deflate-stream extension\n");
110                         return 1;
111                 }
112                 if ((strcmp(in, "x-webkit-deflate-frame") == 0))
113                         return 1;
114                 if ((strcmp(in, "deflate-frame") == 0))
115                         return 1;
116                 break;
117
118         default:
119                 break;
120         }
121
122         return 0;
123 }
124
125
126 /* lws-mirror_protocol */
127
128
129 static int
130 callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
131                     void *user, void *in, size_t len)
132 {
133         unsigned char buf[LWS_PRE + 4096];
134         unsigned int rands[4];
135         int l = 0;
136         int n;
137
138         switch (reason) {
139         case LWS_CALLBACK_CLIENT_ESTABLISHED:
140
141                 lwsl_notice("mirror: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
142
143                 lws_get_random(lws_get_context(wsi), rands, sizeof(rands[0]));
144                 mirror_lifetime = 16384 + (rands[0] & 65535);
145                 /* useful to test single connection stability */
146                 if (longlived)
147                         mirror_lifetime += 500000;
148
149                 lwsl_info("opened mirror connection with "
150                           "%d lifetime\n", mirror_lifetime);
151
152                 /*
153                  * mirror_lifetime is decremented each send, when it reaches
154                  * zero the connection is closed in the send callback.
155                  * When the close callback comes, wsi_mirror is set to NULL
156                  * so a new connection will be opened
157                  *
158                  * start the ball rolling,
159                  * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
160                  */
161                 lws_callback_on_writable(wsi);
162                 break;
163
164         case LWS_CALLBACK_CLOSED:
165                 lwsl_notice("mirror: LWS_CALLBACK_CLOSED mirror_lifetime=%d\n", mirror_lifetime);
166                 wsi_mirror = NULL;
167                 break;
168
169         case LWS_CALLBACK_CLIENT_WRITEABLE:
170                 for (n = 0; n < 1; n++) {
171                         lws_get_random(lws_get_context(wsi), rands, sizeof(rands));
172                         l += sprintf((char *)&buf[LWS_PRE + l],
173                                         "c #%06X %u %u %u;",
174                                         rands[0] & 0xffffff,    /* colour */
175                                         rands[1] & 511,         /* x */
176                                         rands[2] & 255,         /* y */
177                                         (rands[3] & 31) + 1);   /* radius */
178                 }
179
180                 n = lws_write(wsi, &buf[LWS_PRE], l,
181                               opts | LWS_WRITE_TEXT);
182                 if (n < 0)
183                         return -1;
184                 if (n < l) {
185                         lwsl_err("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
186                         return -1;
187                 }
188
189                 mirror_lifetime--;
190                 if (!mirror_lifetime) {
191                         lwsl_info("closing mirror session\n");
192                         return -1;
193                 }
194                 /* get notified as soon as we can write again */
195                 lws_callback_on_writable(wsi);
196                 break;
197
198         default:
199                 break;
200         }
201
202         return 0;
203 }
204
205
206 /* list of supported protocols and callbacks */
207
208 static struct lws_protocols protocols[] = {
209         {
210                 "dumb-increment-protocol,fake-nonexistant-protocol",
211                 callback_dumb_increment,
212                 0,
213                 20,
214         },
215         {
216                 "fake-nonexistant-protocol,lws-mirror-protocol",
217                 callback_lws_mirror,
218                 0,
219                 128,
220         },
221         { NULL, NULL, 0, 0 } /* end */
222 };
223
224 static const struct lws_extension exts[] = {
225         {
226                 "permessage-deflate",
227                 lws_extension_callback_pm_deflate,
228                 "permessage-deflate; client_max_window_bits"
229         },
230         {
231                 "deflate-frame",
232                 lws_extension_callback_pm_deflate,
233                 "deflate_frame"
234         },
235         { NULL, NULL, NULL /* terminator */ }
236 };
237
238
239
240 void sighandler(int sig)
241 {
242         force_exit = 1;
243 }
244
245 static struct option options[] = {
246         { "help",       no_argument,            NULL, 'h' },
247         { "debug",      required_argument,      NULL, 'd' },
248         { "port",       required_argument,      NULL, 'p' },
249         { "ssl",        no_argument,            NULL, 's' },
250         { "version",    required_argument,      NULL, 'v' },
251         { "undeflated", no_argument,            NULL, 'u' },
252         { "nomux",      no_argument,            NULL, 'n' },
253         { "longlived",  no_argument,            NULL, 'l' },
254         { NULL, 0, 0, 0 }
255 };
256
257 static int ratelimit_connects(unsigned int *last, unsigned int secs)
258 {
259         struct timeval tv;
260
261         gettimeofday(&tv, NULL);
262         if (tv.tv_sec - (*last) < secs)
263                 return 0;
264
265         *last = tv.tv_sec;
266
267         return 1;
268 }
269
270 int main(int argc, char **argv)
271 {
272         int n = 0, ret = 0, port = 7681, use_ssl = 0, ietf_version = -1;
273         unsigned int rl_dumb = 0, rl_mirror = 0;
274         struct lws_context_creation_info info;
275         struct lws_client_connect_info i;
276         struct lws_context *context;
277         const char *prot;
278
279         memset(&info, 0, sizeof info);
280
281         lwsl_notice("libwebsockets test client - license LGPL2.1+SLE\n");
282         lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
283
284         if (argc < 2)
285                 goto usage;
286
287         while (n >= 0) {
288                 n = getopt_long(argc, argv, "nuv:hsp:d:l", options, NULL);
289                 if (n < 0)
290                         continue;
291                 switch (n) {
292                 case 'd':
293                         lws_set_log_level(atoi(optarg), NULL);
294                         break;
295                 case 's':
296                         use_ssl = 2; /* 2 = allow selfsigned */
297                         break;
298                 case 'p':
299                         port = atoi(optarg);
300                         break;
301                 case 'l':
302                         longlived = 1;
303                         break;
304                 case 'v':
305                         ietf_version = atoi(optarg);
306                         break;
307                 case 'u':
308                         deny_deflate = 1;
309                         break;
310                 case 'n':
311                         deny_mux = 1;
312                         break;
313                 case 'h':
314                         goto usage;
315                 }
316         }
317
318         if (optind >= argc)
319                 goto usage;
320
321         signal(SIGINT, sighandler);
322
323         memset(&i, 0, sizeof(i));
324
325         i.port = port;
326         if (lws_parse_uri(argv[optind], &prot, &i.address, &i.port, &i.path))
327                 goto usage;
328
329         if (!strcmp(prot, "http://") || !strcmp(prot, "ws://"))
330                 use_ssl = 0;
331         if (!strcmp(prot, "https://") || !strcmp(prot, "wss://"))
332                 use_ssl = 1;
333
334         /*
335          * create the websockets context.  This tracks open connections and
336          * knows how to route any traffic and which protocol version to use,
337          * and if each connection is client or server side.
338          *
339          * For this client-only demo, we tell it to not listen on any port.
340          */
341
342         info.port = CONTEXT_PORT_NO_LISTEN;
343         info.protocols = protocols;
344         info.gid = -1;
345         info.uid = -1;
346
347         context = lws_create_context(&info);
348         if (context == NULL) {
349                 fprintf(stderr, "Creating libwebsocket context failed\n");
350                 return 1;
351         }
352
353         i.context = context;
354         i.ssl_connection = use_ssl;
355         i.host = i.address;
356         i.origin = i.address;
357         i.ietf_version_or_minus_one = ietf_version;
358         i.client_exts = exts;
359         /*
360          * sit there servicing the websocket context to handle incoming
361          * packets, and drawing random circles on the mirror protocol websocket
362          *
363          * nothing happens until the client websocket connection is
364          * asynchronously established... calling lws_client_connect() only
365          * instantiates the connection logically, lws_service() progresses it
366          * asynchronously.
367          */
368
369         while (!force_exit) {
370
371                 if (!wsi_dumb && ratelimit_connects(&rl_dumb, 2u)) {
372                         lwsl_notice("dumb: connecting\n");
373                         i.protocol = protocols[PROTOCOL_DUMB_INCREMENT].name;
374                         wsi_dumb = lws_client_connect_via_info(&i);
375                 }
376
377                 if (!wsi_mirror && ratelimit_connects(&rl_mirror, 2u)) {
378                         lwsl_notice("mirror: connecting\n");
379                         i.protocol = protocols[PROTOCOL_LWS_MIRROR].name;
380                         wsi_mirror = lws_client_connect_via_info(&i);
381                 }
382
383                 lws_service(context, 500);
384         }
385
386         lwsl_err("Exiting\n");
387         lws_context_destroy(context);
388
389         return ret;
390
391 usage:
392         fprintf(stderr, "Usage: libwebsockets-test-client "
393                                 "<server address> [--port=<p>] "
394                                 "[--ssl] [-k] [-v <ver>] "
395                                 "[-d <log bitfield>] [-l]\n");
396         return 1;
397 }