Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 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 <unistd.h> |
| 25 | #include <getopt.h> |
| 26 | #include <string.h> |
| 27 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 28 | #include "../lib/libwebsockets.h" |
| 29 | #include <poll.h> |
| 30 | |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 31 | static unsigned int opts; |
| 32 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 33 | /* |
| 34 | * This demo shows how to connect multiple websockets simultaneously to a |
| 35 | * websocket server (there is no restriction on their having to be the same |
| 36 | * server just it simplifies the demo). |
| 37 | * |
| 38 | * dumb-increment-protocol: we connect to the server and print the number |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 39 | * we are given |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 40 | * |
| 41 | * lws-mirror-protocol: draws random circles, which are mirrored on to every |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 42 | * client (see them being drawn in every browser |
| 43 | * session also using the test server) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 44 | */ |
| 45 | |
| 46 | enum demo_protocols { |
| 47 | |
| 48 | PROTOCOL_DUMB_INCREMENT, |
| 49 | PROTOCOL_LWS_MIRROR, |
| 50 | |
| 51 | /* always last */ |
| 52 | DEMO_PROTOCOL_COUNT |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | /* dumb_increment protocol */ |
| 57 | |
| 58 | static int |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame^] | 59 | callback_dumb_increment(struct libwebsocket_context * this, |
| 60 | struct libwebsocket *wsi, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 61 | enum libwebsocket_callback_reasons reason, |
| 62 | void *user, void *in, size_t len) |
| 63 | { |
| 64 | switch (reason) { |
| 65 | |
| 66 | case LWS_CALLBACK_CLIENT_RECEIVE: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 67 | ((char *)in)[len] = '\0'; |
| 68 | fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 69 | break; |
| 70 | |
| 71 | default: |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /* lws-mirror_protocol */ |
| 80 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 81 | |
| 82 | static int |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame^] | 83 | callback_lws_mirror(struct libwebsocket_context * this, |
| 84 | struct libwebsocket *wsi, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 85 | enum libwebsocket_callback_reasons reason, |
| 86 | void *user, void *in, size_t len) |
| 87 | { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 88 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 + |
| 89 | LWS_SEND_BUFFER_POST_PADDING]; |
| 90 | int l; |
| 91 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 92 | switch (reason) { |
| 93 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 94 | case LWS_CALLBACK_CLIENT_ESTABLISHED: |
| 95 | |
| 96 | /* |
| 97 | * start the ball rolling, |
Andy Green | b6e6ebe | 2011-01-27 06:36:39 +0000 | [diff] [blame] | 98 | * LWS_CALLBACK_CLIENT_WRITEABLE will come next service |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 99 | */ |
| 100 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame^] | 101 | libwebsocket_callback_on_writable(this, wsi); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 102 | break; |
| 103 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 104 | case LWS_CALLBACK_CLIENT_RECEIVE: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 105 | /* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */ |
| 106 | break; |
| 107 | |
| 108 | case LWS_CALLBACK_CLIENT_WRITEABLE: |
| 109 | |
| 110 | l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING], |
| 111 | "c #%06X %d %d %d;", |
| 112 | (int)random() & 0xffffff, |
| 113 | (int)random() % 500, |
| 114 | (int)random() % 250, |
| 115 | (int)random() % 24); |
| 116 | |
| 117 | libwebsocket_write(wsi, |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 118 | &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 119 | |
| 120 | /* get notified as soon as we can write again */ |
| 121 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame^] | 122 | libwebsocket_callback_on_writable(this, wsi); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 123 | |
Andy Green | b6e6ebe | 2011-01-27 06:36:39 +0000 | [diff] [blame] | 124 | /* |
| 125 | * without at least this delay, we choke the browser |
| 126 | * and the connection stalls, despite we now take care about |
| 127 | * flow control |
| 128 | */ |
| 129 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 130 | usleep(200); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 131 | break; |
| 132 | |
| 133 | default: |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /* list of supported protocols and callbacks */ |
| 142 | |
| 143 | static struct libwebsocket_protocols protocols[] = { |
| 144 | |
| 145 | [PROTOCOL_DUMB_INCREMENT] = { |
| 146 | .name = "dumb-increment-protocol", |
| 147 | .callback = callback_dumb_increment, |
| 148 | }, |
| 149 | [PROTOCOL_LWS_MIRROR] = { |
| 150 | .name = "lws-mirror-protocol", |
| 151 | .callback = callback_lws_mirror, |
| 152 | }, |
| 153 | [DEMO_PROTOCOL_COUNT] = { /* end of list */ |
| 154 | .callback = NULL |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | static struct option options[] = { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 159 | { "help", no_argument, NULL, 'h' }, |
| 160 | { "port", required_argument, NULL, 'p' }, |
| 161 | { "ssl", no_argument, NULL, 's' }, |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 162 | { "killmask", no_argument, NULL, 'k' }, |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 163 | { "version", required_argument, NULL, 'v' }, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 164 | { NULL, 0, 0, 0 } |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | int main(int argc, char **argv) |
| 169 | { |
| 170 | int n = 0; |
| 171 | int port = 7681; |
| 172 | int use_ssl = 0; |
| 173 | struct libwebsocket_context *context; |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 174 | const char *address; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 175 | struct libwebsocket *wsi_dumb; |
| 176 | struct libwebsocket *wsi_mirror; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 177 | int ietf_version = -1; /* latest */ |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 178 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 179 | fprintf(stderr, "libwebsockets test client\n" |
| 180 | "(C) Copyright 2010 Andy Green <andy@warmcat.com> " |
| 181 | "licensed under LGPL2.1\n"); |
| 182 | |
| 183 | if (argc < 2) |
| 184 | goto usage; |
| 185 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 186 | while (n >= 0) { |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 187 | n = getopt_long(argc, argv, "v:khsp:", options, NULL); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 188 | if (n < 0) |
| 189 | continue; |
| 190 | switch (n) { |
| 191 | case 's': |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 192 | use_ssl = 2; /* 2 = allow selfsigned */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 193 | break; |
| 194 | case 'p': |
| 195 | port = atoi(optarg); |
| 196 | break; |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 197 | case 'k': |
| 198 | opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK; |
| 199 | break; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 200 | case 'v': |
| 201 | ietf_version = atoi(optarg); |
| 202 | break; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 203 | case 'h': |
| 204 | goto usage; |
| 205 | } |
| 206 | } |
| 207 | |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 208 | if (optind >= argc) |
| 209 | goto usage; |
| 210 | |
| 211 | address = argv[optind]; |
| 212 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 213 | /* |
| 214 | * create the websockets context. This tracks open connections and |
| 215 | * knows how to route any traffic and which protocol version to use, |
| 216 | * and if each connection is client or server side. |
| 217 | * |
| 218 | * For this client-only demo, we tell it to not listen on any port. |
| 219 | */ |
| 220 | |
| 221 | context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 222 | protocols, NULL, NULL, -1, -1, 0); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 223 | if (context == NULL) { |
| 224 | fprintf(stderr, "Creating libwebsocket context failed\n"); |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /* create a client websocket using dumb increment protocol */ |
| 230 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 231 | wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl, |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 232 | "/", argv[optind], argv[optind], |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 233 | protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 234 | |
| 235 | if (wsi_dumb == NULL) { |
| 236 | fprintf(stderr, "libwebsocket dumb connect failed\n"); |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | /* create a client websocket using mirror protocol */ |
| 241 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 242 | wsi_mirror = libwebsocket_client_connect(context, address, port, |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 243 | use_ssl, "/", argv[optind], argv[optind], |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 244 | protocols[PROTOCOL_LWS_MIRROR].name, ietf_version); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 245 | |
| 246 | if (wsi_mirror == NULL) { |
| 247 | fprintf(stderr, "libwebsocket dumb connect failed\n"); |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | fprintf(stderr, "Websocket connections opened\n"); |
| 252 | |
| 253 | /* |
| 254 | * sit there servicing the websocket context to handle incoming |
| 255 | * packets, and drawing random circles on the mirror protocol websocket |
| 256 | */ |
| 257 | |
| 258 | n = 0; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 259 | while (n >= 0) |
| 260 | n = libwebsocket_service(context, 1000); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 261 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 262 | libwebsocket_context_destroy(context); |
| 263 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 264 | return 0; |
| 265 | |
| 266 | usage: |
| 267 | fprintf(stderr, "Usage: libwebsockets-test-client " |
| 268 | "<server address> [--port=<p>] " |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 269 | "[--ssl] [-k] [-v <ver>]\n"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 270 | return 1; |
| 271 | } |