introduce-client-support.patch

Signed-off-by: Andy Green <andy@warmcat.com>
diff --git a/test-server/Makefile.am b/test-server/Makefile.am
index 0332251..43d3c7c 100644
--- a/test-server/Makefile.am
+++ b/test-server/Makefile.am
@@ -1,7 +1,8 @@
-bin_PROGRAMS=libwebsockets-test-server
+bin_PROGRAMS=libwebsockets-test-server libwebsockets-test-client
 libwebsockets_test_server_SOURCES=test-server.c
 libwebsockets_test_server_LDADD=-L../lib -lwebsockets
-
+libwebsockets_test_client_SOURCES=test-client.c
+libwebsockets_test_client_LDADD=-L../lib -lwebsockets
 #
 # cook a random test cert and key
 # notice your real cert and key will want to be 0600 permissions
diff --git a/test-server/Makefile.in b/test-server/Makefile.in
index dbe97f6..03ad8a8 100644
--- a/test-server/Makefile.in
+++ b/test-server/Makefile.in
@@ -34,7 +34,8 @@
 POST_UNINSTALL = :
 build_triplet = @build@
 host_triplet = @host@
-bin_PROGRAMS = libwebsockets-test-server$(EXEEXT)
+bin_PROGRAMS = libwebsockets-test-server$(EXEEXT) \
+	libwebsockets-test-client$(EXEEXT)
 subdir = test-server
 DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@@ -47,6 +48,10 @@
 CONFIG_CLEAN_VPATH_FILES =
 am__installdirs = "$(DESTDIR)$(bindir)"
 PROGRAMS = $(bin_PROGRAMS)
+am_libwebsockets_test_client_OBJECTS = test-client.$(OBJEXT)
+libwebsockets_test_client_OBJECTS =  \
+	$(am_libwebsockets_test_client_OBJECTS)
+libwebsockets_test_client_DEPENDENCIES =
 am_libwebsockets_test_server_OBJECTS = test-server.$(OBJEXT)
 libwebsockets_test_server_OBJECTS =  \
 	$(am_libwebsockets_test_server_OBJECTS)
@@ -64,8 +69,10 @@
 LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \
 	--mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \
 	$(LDFLAGS) -o $@
-SOURCES = $(libwebsockets_test_server_SOURCES)
-DIST_SOURCES = $(libwebsockets_test_server_SOURCES)
+SOURCES = $(libwebsockets_test_client_SOURCES) \
+	$(libwebsockets_test_server_SOURCES)
+DIST_SOURCES = $(libwebsockets_test_client_SOURCES) \
+	$(libwebsockets_test_server_SOURCES)
 ETAGS = etags
 CTAGS = ctags
 DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@@ -184,6 +191,8 @@
 top_srcdir = @top_srcdir@
 libwebsockets_test_server_SOURCES = test-server.c
 libwebsockets_test_server_LDADD = -L../lib -lwebsockets
+libwebsockets_test_client_SOURCES = test-client.c
+libwebsockets_test_client_LDADD = -L../lib -lwebsockets
 all: all-am
 
 .SUFFIXES:
@@ -261,6 +270,9 @@
 	list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \
 	echo " rm -f" $$list; \
 	rm -f $$list
+libwebsockets-test-client$(EXEEXT): $(libwebsockets_test_client_OBJECTS) $(libwebsockets_test_client_DEPENDENCIES) 
+	@rm -f libwebsockets-test-client$(EXEEXT)
+	$(LINK) $(libwebsockets_test_client_OBJECTS) $(libwebsockets_test_client_LDADD) $(LIBS)
 libwebsockets-test-server$(EXEEXT): $(libwebsockets_test_server_OBJECTS) $(libwebsockets_test_server_DEPENDENCIES) 
 	@rm -f libwebsockets-test-server$(EXEEXT)
 	$(LINK) $(libwebsockets_test_server_OBJECTS) $(libwebsockets_test_server_LDADD) $(LIBS)
@@ -271,6 +283,7 @@
 distclean-compile:
 	-rm -f *.tab.c
 
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test-client.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test-server.Po@am__quote@
 
 .c.o:
@@ -501,7 +514,6 @@
 	mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \
 	tags uninstall uninstall-am uninstall-binPROGRAMS
 
-
 #
 # cook a random test cert and key
 # notice your real cert and key will want to be 0600 permissions
diff --git a/test-server/test-client.c b/test-server/test-client.c
new file mode 100644
index 0000000..6869d6d
--- /dev/null
+++ b/test-server/test-client.c
@@ -0,0 +1,390 @@
+/*
+ * libwebsockets-test-client - libwebsockets test implementation
+ *
+ * Copyright (C) 2011 Andy Green <andy@warmcat.com>
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation:
+ *  version 2.1 of the License.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *  MA  02110-1301  USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <string.h>
+
+
+#include "../lib/libwebsockets.h"
+#include <poll.h>
+
+/*
+ * This demo shows how to connect multiple websockets simultaneously to a
+ * websocket server (there is no restriction on their having to be the same
+ * server just it simplifies the demo).
+ *
+ *  dumb-increment-protocol:  we connect to the server and print the number
+ * 				we are given
+ *
+ *  lws-mirror-protocol: draws random circles, which are mirrored on to every
+ * 				client (see them being drawn in every browser
+ * 				session also using the test server)
+ */
+
+enum demo_protocols {
+
+	PROTOCOL_DUMB_INCREMENT,
+	PROTOCOL_LWS_MIRROR,
+
+	/* always last */
+	DEMO_PROTOCOL_COUNT
+};
+
+
+/* dumb_increment protocol */
+
+static int
+callback_dumb_increment(struct libwebsocket *wsi,
+			enum libwebsocket_callback_reasons reason,
+					       void *user, void *in, size_t len)
+{
+	switch (reason) {
+
+	case LWS_CALLBACK_CLIENT_RECEIVE:
+		fprintf(stderr, "rx %d '%s'\n", len, in);
+		break;
+
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+
+/* lws-mirror_protocol */
+
+/* "how to draw a circle" */
+
+struct coord {
+	int x;
+	int y;
+};
+
+static struct coord circle[] = {
+
+{ 0, 240 },
+{ 12, 239 },
+{ 25, 238 },
+{ 37, 237 },
+{ 49, 234 },
+{ 62, 231 },
+{ 74, 228 },
+{ 86, 224 },
+{ 97, 219 },
+{ 108, 213 },
+{ 120, 207 },
+{ 130, 201 },
+{ 141, 194 },
+{ 151, 186 },
+{ 160, 178 },
+{ 169, 169 },
+{ 178, 160 },
+{ 186, 151 },
+{ 194, 141 },
+{ 201, 130 },
+{ 207, 120 },
+{ 213, 108 },
+{ 219, 97 },
+{ 224, 86 },
+{ 228, 74 },
+{ 231, 62 },
+{ 234, 49 },
+{ 237, 37 },
+{ 238, 25 },
+{ 239, 12 },
+{ 240, 0 },
+{ 239, -12 },
+{ 238, -25 },
+{ 237, -37 },
+{ 234, -49 },
+{ 231, -62 },
+{ 228, -74 },
+{ 224, -86 },
+{ 219, -97 },
+{ 213, -108 },
+{ 207, -120 },
+{ 201, -130 },
+{ 194, -141 },
+{ 186, -151 },
+{ 178, -160 },
+{ 169, -169 },
+{ 160, -178 },
+{ 151, -186 },
+{ 141, -194 },
+{ 130, -201 },
+{ 120, -207 },
+{ 108, -213 },
+{ 97, -219 },
+{ 86, -224 },
+{ 74, -228 },
+{ 62, -231 },
+{ 49, -234 },
+{ 37, -237 },
+{ 25, -238 },
+{ 12, -239 },
+{ 0, -240 },
+{ -12, -239 },
+{ -25, -238 },
+{ -37, -237 },
+{ -49, -234 },
+{ -62, -231 },
+{ -74, -228 },
+{ -86, -224 },
+{ -97, -219 },
+{ -108, -213 },
+{ -119, -207 },
+{ -130, -201 },
+{ -141, -194 },
+{ -151, -186 },
+{ -160, -178 },
+{ -169, -169 },
+{ -178, -160 },
+{ -186, -151 },
+{ -194, -141 },
+{ -201, -130 },
+{ -207, -120 },
+{ -213, -108 },
+{ -219, -97 },
+{ -224, -86 },
+{ -228, -74 },
+{ -231, -62 },
+{ -234, -49 },
+{ -237, -37 },
+{ -238, -25 },
+{ -239, -12 },
+{ -240, 0 },
+{ -239, 12 },
+{ -238, 25 },
+{ -237, 37 },
+{ -234, 49 },
+{ -231, 62 },
+{ -228, 74 },
+{ -224, 86 },
+{ -219, 97 },
+{ -213, 108 },
+{ -207, 120 },
+{ -201, 130 },
+{ -194, 141 },
+{ -186, 151 },
+{ -178, 160 },
+{ -169, 169 },
+{ -160, 178 },
+{ -151, 186 },
+{ -141, 194 },
+{ -130, 201 },
+{ -119, 207 },
+{ -108, 213 },
+{ -97, 219 },
+{ -86, 224 },
+{ -74, 228 },
+{ -62, 231 },
+{ -49, 234 },
+{ -37, 237 },
+{ -25, 238 },
+{ -12, 239 },
+{ 0, 240 },
+	
+};
+
+static int
+callback_lws_mirror(struct libwebsocket *wsi,
+			enum libwebsocket_callback_reasons reason,
+					       void *user, void *in, size_t len)
+{
+	switch (reason) {
+
+	case LWS_CALLBACK_CLIENT_RECEIVE:
+//		fprintf(stderr, "rx %d '%s'\n", len, in);
+		break;
+
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+
+/* list of supported protocols and callbacks */
+
+static struct libwebsocket_protocols protocols[] = {
+
+	[PROTOCOL_DUMB_INCREMENT] = {
+		.name = "dumb-increment-protocol",
+		.callback = callback_dumb_increment,
+	},
+	[PROTOCOL_LWS_MIRROR] = {
+		.name = "lws-mirror-protocol",
+		.callback = callback_lws_mirror,
+	},
+	[DEMO_PROTOCOL_COUNT] = {  /* end of list */
+		.callback = NULL
+	}
+};
+
+static struct option options[] = {
+	{ "help", 	no_argument, NULL, 'h' },
+	{ "port", 	required_argument, NULL, 'p' },
+	{ "ssl", 	no_argument, NULL, 's' },
+	{ NULL, 0, 0, 0 }
+};
+
+
+int main(int argc, char **argv)
+{
+	int n = 0;
+	int port = 7681;
+	int use_ssl = 0;
+	struct libwebsocket_context *context;
+	const char * address = argv[1];
+	struct libwebsocket *wsi_dumb;
+	struct libwebsocket *wsi_mirror;
+	unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1024 +
+						  LWS_SEND_BUFFER_POST_PADDING];
+	int len;
+	int i = 0;
+	int xofs;
+	int yofs;
+	int oldx;
+	int oldy;
+	int scale;
+	int colour;
+
+	fprintf(stderr, "libwebsockets test client\n"
+			"(C) Copyright 2010 Andy Green <andy@warmcat.com> "
+						    "licensed under LGPL2.1\n");
+
+	if (argc < 2)
+		goto usage;
+
+	optind++;
+
+	while (n >= 0) {
+		n = getopt_long(argc, argv, "hsp:", options, NULL);
+		if (n < 0)
+			continue;
+		switch (n) {
+		case 's':
+			use_ssl = 1;
+			break;
+		case 'p':
+			port = atoi(optarg);
+			break;
+		case 'h':
+			goto usage;
+		}
+	}
+
+	/*
+	 * create the websockets context.  This tracks open connections and
+	 * knows how to route any traffic and which protocol version to use,
+	 * and if each connection is client or server side.
+	 *
+	 * For this client-only demo, we tell it to not listen on any port.
+	 */
+
+	context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN,
+						 protocols, NULL, NULL, -1, -1);
+	if (context == NULL) {
+		fprintf(stderr, "Creating libwebsocket context failed\n");
+		return 1;
+	}
+
+
+	/* create a client websocket using dumb increment protocol */
+
+	wsi_dumb = libwebsocket_client_connect(context, address, port, "/",
+				       "http://host", "origin",
+				       protocols[PROTOCOL_DUMB_INCREMENT].name);
+
+	if (wsi_dumb == NULL) {
+		fprintf(stderr, "libwebsocket dumb connect failed\n");
+		return -1;
+	}
+
+	/* create a client websocket using mirror protocol */
+
+	wsi_mirror = libwebsocket_client_connect(context, address, port, "/",
+				       "http://host", "origin",
+				       protocols[PROTOCOL_LWS_MIRROR].name);
+
+	if (wsi_mirror == NULL) {
+		fprintf(stderr, "libwebsocket dumb connect failed\n");
+		return -1;
+	}
+
+	fprintf(stderr, "Websocket connections opened\n");
+
+	/*
+	 * sit there servicing the websocket context to handle incoming
+	 * packets, and drawing random circles on the mirror protocol websocket
+	 */
+
+	n = 0;
+	while (n >= 0) {
+
+		usleep(10000);
+
+		if (i == sizeof circle / sizeof circle[0])
+			i = 0;
+
+		if (i == 0) {
+			xofs = random() % 500;
+			yofs = random() % 250;
+			scale = random() % 24;
+			if (!scale)
+				scale = 1;
+
+			oldx = xofs + (circle[i].x / scale);
+			oldy = yofs + (circle[i].y / scale);
+			colour = random() & 0xffffff;
+		}
+
+		len = sprintf(&buf[LWS_SEND_BUFFER_PRE_PADDING],
+			"d #%06X %d %d %d %d", colour, oldx, oldy,
+				xofs + (circle[i].x / scale),
+				yofs + (circle[i].y / scale));
+		oldx = xofs + (circle[i].x / scale);
+		oldy = yofs + (circle[i].y / scale);
+		i++;
+
+		libwebsocket_write(wsi_mirror,
+			&buf[LWS_SEND_BUFFER_PRE_PADDING], len, LWS_WRITE_TEXT);
+
+
+		n = libwebsocket_service(context, 0);
+	}
+
+	libwebsocket_client_close(wsi_dumb);
+	libwebsocket_client_close(wsi_mirror);
+
+	return 0;
+
+usage:
+	fprintf(stderr, "Usage: libwebsockets-test-client "
+					     "<server address> [--port=<p>] "
+					     "[--ssl]\n");
+	return 1;
+}
diff --git a/test-server/test-server.c b/test-server/test-server.c
index 36a1978..dc3cfd9 100644
--- a/test-server/test-server.c
+++ b/test-server/test-server.c
@@ -230,7 +230,7 @@
 						  LWS_SEND_BUFFER_POST_PADDING];
 	int port = 7681;
 	int use_ssl = 0;
-	struct libwebsocket_context *server;
+	struct libwebsocket_context *context;
 
 	fprintf(stderr, "libwebsockets test server\n"
 			"(C) Copyright 2010 Andy Green <andy@warmcat.com> "
@@ -257,9 +257,9 @@
 	if (!use_ssl)
 		cert_path = key_path = NULL;
 
-	server = libwebsocket_create_server(port, protocols, cert_path,
+	context = libwebsocket_create_context(port, protocols, cert_path,
 							      key_path, -1, -1);
-	if (server == NULL) {
+	if (context == NULL) {
 		fprintf(stderr, "libwebsocket init failed\n");
 		return -1;
 	}
@@ -304,7 +304,7 @@
 		 * immediately and quickly.
 		 */
 
-		libwebsocket_service(server, 0);
+		libwebsocket_service(context, 0);
 	}
 
 #else
@@ -320,7 +320,7 @@
 	 * don't have to take care about it.
 	 */
 
-	n = libwebsockets_fork_service_loop(server);
+	n = libwebsockets_fork_service_loop(context);
 	if (n < 0) {
 		fprintf(stderr, "Unable to fork service loop %d\n", n);
 		return 1;