Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets-test-server - libwebsockets test implementation |
| 3 | * |
| 4 | * Copyright (C) 2010 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 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 25 | #include <getopt.h> |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 26 | #include <string.h> |
| 27 | |
Andy Green | 7310e9c | 2010-11-01 09:12:17 +0000 | [diff] [blame] | 28 | #include "../lib/libwebsockets.h" |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 29 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 30 | /* |
| 31 | * This demo server shows how to use libwebsockets for one or more |
| 32 | * websocket protocols in the same server |
| 33 | * |
| 34 | * It defines the following websocket protocols: |
| 35 | * |
| 36 | * dumb-increment-protocol: once the socket is opened, an incrementing |
| 37 | * ascii string is sent down it every 50ms. |
| 38 | * If you send "reset\n" on the websocket, then |
| 39 | * the incrementing number is reset to 0. |
| 40 | * |
| 41 | */ |
| 42 | |
| 43 | |
Andy Green | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 44 | #define LOCAL_RESOURCE_PATH "/usr/share/libwebsockets-test-server" |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 45 | static int port = 7681; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 46 | static int use_ssl = 0; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 47 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 48 | /* this protocol server (always the first one) just knows how to do HTTP */ |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 49 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 50 | static int callback_http(struct libwebsocket * wsi, |
Andy Green | 251f6fa | 2010-11-03 11:13:06 +0000 | [diff] [blame] | 51 | enum libwebsocket_callback_reasons reason, void * user, |
| 52 | void *in, size_t len) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 53 | { |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 54 | switch (reason) { |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 55 | case LWS_CALLBACK_HTTP: |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 56 | fprintf(stderr, "serving HTTP URI %s\n", in); |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 57 | |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 58 | if (in && strcmp(in, "/favicon.ico") == 0) { |
Andy Green | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 59 | if (libwebsockets_serve_http_file(wsi, |
| 60 | LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon")) |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 61 | fprintf(stderr, "Failed to send favicon\n"); |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 62 | break; |
| 63 | } |
| 64 | |
| 65 | /* send the script... when it runs it'll start websockets */ |
| 66 | |
Andy Green | 3138e44 | 2010-11-01 09:20:48 +0000 | [diff] [blame] | 67 | if (libwebsockets_serve_http_file(wsi, |
| 68 | LOCAL_RESOURCE_PATH"/test.html", "text/html")) |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 69 | fprintf(stderr, "Failed to send HTTP file\n"); |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 70 | break; |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 71 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 72 | default: |
| 73 | break; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 74 | } |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 75 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 79 | /* dumb_increment protocol */ |
| 80 | |
| 81 | struct per_session_data__dumb_increment { |
| 82 | int number; |
| 83 | }; |
| 84 | |
| 85 | static int |
| 86 | callback_dumb_increment(struct libwebsocket * wsi, |
| 87 | enum libwebsocket_callback_reasons reason, |
| 88 | void * user, void *in, size_t len) |
| 89 | { |
| 90 | int n; |
| 91 | char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 + |
| 92 | LWS_SEND_BUFFER_POST_PADDING]; |
| 93 | unsigned char *p = (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING]; |
| 94 | struct per_session_data__dumb_increment * pss = user; |
| 95 | |
| 96 | switch (reason) { |
| 97 | |
| 98 | case LWS_CALLBACK_ESTABLISHED: |
| 99 | pss->number = 0; |
| 100 | break; |
| 101 | |
| 102 | case LWS_CALLBACK_SEND: |
| 103 | n = sprintf(p, "%d", pss->number++); |
| 104 | n = libwebsocket_write(wsi, p, n, LWS_WRITE_TEXT); |
| 105 | if (n < 0) { |
| 106 | fprintf(stderr, "ERROR writing to socket"); |
| 107 | return 1; |
| 108 | } |
| 109 | break; |
| 110 | |
| 111 | case LWS_CALLBACK_RECEIVE: |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 112 | fprintf(stderr, "rx %d\n", len); |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 113 | if (len < 6) |
| 114 | break; |
| 115 | if (strcmp(in, "reset\n") == 0) |
| 116 | pss->number = 0; |
| 117 | break; |
| 118 | |
| 119 | default: |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 127 | /* lws-mirror_protocol */ |
| 128 | |
| 129 | #define MAX_MESSAGE_QUEUE 64 |
| 130 | const int MAX_COMMUNE_MEMBERS = 20; |
| 131 | |
| 132 | struct per_session_data__lws_mirror { |
| 133 | struct libwebsocket * wsi; |
| 134 | int ringbuffer_tail; |
| 135 | }; |
| 136 | |
| 137 | struct a_message { |
| 138 | struct per_session_data * sender; |
| 139 | void * payload; |
| 140 | size_t len; |
| 141 | }; |
| 142 | |
| 143 | static struct a_message ringbuffer[MAX_MESSAGE_QUEUE]; |
| 144 | static int ringbuffer_head; |
| 145 | |
| 146 | |
| 147 | struct per_session_data * all_members; |
| 148 | |
| 149 | |
| 150 | static int |
| 151 | callback_lws_mirror(struct libwebsocket * wsi, |
| 152 | enum libwebsocket_callback_reasons reason, |
| 153 | void * user, void *in, size_t len) |
| 154 | { |
| 155 | int n; |
| 156 | char buf[LWS_SEND_BUFFER_PRE_PADDING + 512 + |
| 157 | LWS_SEND_BUFFER_POST_PADDING]; |
| 158 | unsigned char *p = (unsigned char *)&buf[LWS_SEND_BUFFER_PRE_PADDING]; |
| 159 | struct per_session_data__lws_mirror * pss = user; |
| 160 | |
| 161 | switch (reason) { |
| 162 | |
| 163 | case LWS_CALLBACK_ESTABLISHED: |
| 164 | pss->wsi = wsi; |
| 165 | pss->ringbuffer_tail = ringbuffer_head; |
| 166 | break; |
| 167 | |
| 168 | case LWS_CALLBACK_SEND: |
| 169 | /* send everything that's pending */ |
| 170 | while (pss->ringbuffer_tail != ringbuffer_head) { |
| 171 | |
| 172 | n = libwebsocket_write(wsi, |
| 173 | (unsigned char *)ringbuffer[pss->ringbuffer_tail].payload + |
| 174 | LWS_SEND_BUFFER_PRE_PADDING, |
| 175 | ringbuffer[pss->ringbuffer_tail].len, |
| 176 | LWS_WRITE_TEXT); |
| 177 | if (n < 0) { |
| 178 | fprintf(stderr, "ERROR writing to socket"); |
| 179 | exit(1); |
| 180 | } |
| 181 | |
| 182 | if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1)) |
| 183 | pss->ringbuffer_tail = 0; |
| 184 | else |
| 185 | pss->ringbuffer_tail++; |
| 186 | } |
| 187 | break; |
| 188 | |
| 189 | case LWS_CALLBACK_RECEIVE: |
| 190 | // fprintf(stderr, "Received %d bytes payload\n", (int)len); |
| 191 | ringbuffer[ringbuffer_head].payload = |
| 192 | malloc(LWS_SEND_BUFFER_PRE_PADDING + len + |
| 193 | LWS_SEND_BUFFER_POST_PADDING); |
| 194 | ringbuffer[ringbuffer_head].len = len; |
| 195 | ringbuffer[ringbuffer_head].sender = pss; |
| 196 | memcpy(ringbuffer[ringbuffer_head].payload + |
| 197 | LWS_SEND_BUFFER_PRE_PADDING, in, len); |
| 198 | if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1)) |
| 199 | ringbuffer_head = 0; |
| 200 | else |
| 201 | ringbuffer_head++; |
| 202 | break; |
| 203 | |
| 204 | default: |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 212 | /* list of supported protocols and callbacks */ |
| 213 | |
| 214 | static const struct libwebsocket_protocols protocols[] = { |
| 215 | { |
| 216 | .name = "http-only", |
| 217 | .callback = callback_http, |
| 218 | .per_session_data_size = 0, |
| 219 | }, |
| 220 | { |
| 221 | .name = "dumb-increment-protocol", |
| 222 | .callback = callback_dumb_increment, |
| 223 | .per_session_data_size = |
| 224 | sizeof(struct per_session_data__dumb_increment), |
| 225 | }, |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 226 | { |
| 227 | .name = "lws-mirror-protocol", |
| 228 | .callback = callback_lws_mirror, |
| 229 | .per_session_data_size = |
| 230 | sizeof(struct per_session_data__lws_mirror), |
| 231 | }, |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 232 | { /* end of list */ |
| 233 | .callback = NULL |
| 234 | } |
| 235 | }; |
| 236 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 237 | static struct option options[] = { |
| 238 | { "help", no_argument, NULL, 'h' }, |
| 239 | { "port", required_argument, NULL, 'p' }, |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 240 | { "ssl", no_argument, NULL, 's' }, |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 241 | { NULL, 0, 0, 0 } |
| 242 | }; |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 243 | |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 244 | int main(int argc, char **argv) |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 245 | { |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 246 | int n = 0; |
Andy Green | 7c212cc | 2010-11-08 20:20:42 +0000 | [diff] [blame] | 247 | const char * cert_path = |
| 248 | LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem"; |
| 249 | const char * key_path = |
| 250 | LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem"; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 251 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 252 | fprintf(stderr, "libwebsockets test server\n" |
Andy Green | 6452f1e | 2010-11-11 09:22:22 +0000 | [diff] [blame] | 253 | "(C) Copyright 2010 Andy Green <andy@warmcat.com> " |
| 254 | "licensed under LGPL2.1\n"); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 255 | |
| 256 | while (n >= 0) { |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 257 | n = getopt_long(argc, argv, "hp:", options, NULL); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 258 | if (n < 0) |
| 259 | continue; |
| 260 | switch (n) { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 261 | case 's': |
| 262 | use_ssl = 1; |
| 263 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 264 | case 'p': |
| 265 | port = atoi(optarg); |
| 266 | break; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 267 | case 'h': |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 268 | fprintf(stderr, "Usage: test-server " |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 269 | "[--port=<p>] [--ssl]\n"); |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 270 | exit(1); |
| 271 | } |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 272 | } |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 273 | |
| 274 | if (!use_ssl) |
| 275 | cert_path = key_path = NULL; |
Andy Green | ea71ed1 | 2010-10-31 07:40:33 +0000 | [diff] [blame] | 276 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 277 | if (libwebsocket_create_server(port, protocols, |
| 278 | cert_path, key_path, -1, -1) < 0) { |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 279 | fprintf(stderr, "libwebsocket init failed\n"); |
| 280 | return -1; |
| 281 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 282 | |
| 283 | return 0; |
| 284 | } |