aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPablo Neira Ayuso <pablo@gnumonks.org>2011-10-05 13:07:01 +0200
committerPablo Neira Ayuso <pablo@gnumonks.org>2011-10-05 13:43:21 +0200
commit602df1ec4cf103d8722260d3a4ba577c26fdf27a (patch)
tree9d23115456679c864f09b7b6a9a50ce835ac308a
parent7c20d4ef1e27974910c79979649fcba2a95ad1f8 (diff)
examples: add stream server/client example
This patch adds a couple of examples that allow chatting between the client and the server. For simplicity, the example only support one client.
-rw-r--r--examples/Makefile.am12
-rw-r--r--examples/stream-client.c124
-rw-r--r--examples/stream-server.c147
3 files changed, 282 insertions, 1 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
index c1759e2..39e7207 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -3,7 +3,9 @@ AM_CFLAGS=-Wall -g $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOABIS_CFLA
AM_LDFLAGS = $(COVERAGE_LDFLAGS)
noinst_PROGRAMS = lapd-over-datagram-user \
- lapd-over-datagram-network
+ lapd-over-datagram-network \
+ stream-client \
+ stream-server
lapd_over_datagram_user_SOURCES = lapd-over-datagram-user.c
lapd_over_datagram_user_LDADD = $(top_builddir)/src/libosmonetif.la \
@@ -14,3 +16,11 @@ lapd_over_datagram_network_SOURCES = lapd-over-datagram-network.c
lapd_over_datagram_network_LDADD = $(top_builddir)/src/libosmonetif.la \
$(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) \
$(LIBOSMOABIS_LIBS)
+
+stream_client_SOURCES = stream-client.c
+stream_client_LDADD = $(top_builddir)/src/libosmonetif.la \
+ $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS)
+
+stream_server_SOURCES = stream-server.c
+stream_server_LDADD = $(top_builddir)/src/libosmonetif.la \
+ $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS)
diff --git a/examples/stream-client.c b/examples/stream-client.c
new file mode 100644
index 0000000..441e681
--- /dev/null
+++ b/examples/stream-client.c
@@ -0,0 +1,124 @@
+/* stream client example. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <osmocom/core/select.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/application.h>
+
+#include <osmocom/netif/stream.h>
+
+#define DSTREAMTEST 0
+
+struct log_info_cat stream_client_test_cat[] = {
+ [DSTREAMTEST] = {
+ .name = "DSTREAMTEST",
+ .description = "STREAMCLIENT-mode test",
+ .color = "\033[1;35m",
+ .enabled = 1, .loglevel = LOGL_DEBUG,
+ },
+};
+
+const struct log_info stream_client_test_log_info = {
+ .filter_fn = NULL,
+ .cat = stream_client_test_cat,
+ .num_cat = ARRAY_SIZE(stream_client_test_cat),
+};
+
+static struct stream_client_conn *conn;
+
+void sighandler(int foo)
+{
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "closing stream.\n");
+ exit(EXIT_SUCCESS);
+}
+
+static int connect_cb(struct stream_client_conn *conn)
+{
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "connected\n");
+ return 0;
+}
+
+static int read_cb(struct stream_client_conn *conn, struct msgb *msg)
+{
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "received message from stream\n");
+ return 0;
+}
+
+static void *tall_test;
+
+static int kbd_cb(struct osmo_fd *fd, unsigned int what)
+{
+ char buf[1024];
+ struct msgb *msg;
+ uint8_t *ptr;
+ int ret;
+
+ ret = read(STDIN_FILENO, buf, sizeof(buf));
+
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "read %d byte from keyboard\n", ret);
+
+ msg = msgb_alloc(1024, "STREAMCLIENT/test");
+ if (msg == NULL) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "stream_client: cannot allocate message\n");
+ return 0;
+ }
+ ptr = msgb_put(msg, strlen(buf));
+ memcpy(ptr, buf, ret);
+
+ stream_client_conn_send(conn, msg);
+
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "message of %d bytes sent\n", msg->len);
+
+ return 0;
+}
+
+int main(void)
+{
+ struct osmo_fd *kbd_ofd;
+
+ tall_test = talloc_named_const(NULL, 1, "stream_client_test");
+
+ osmo_init_logging(&stream_client_test_log_info);
+ log_set_log_level(osmo_stderr_target, 1);
+
+ /*
+ * initialize stream client.
+ */
+
+ conn = stream_client_conn_create(tall_test);
+ if (conn == NULL) {
+ fprintf(stderr, "cannot create client\n");
+ exit(EXIT_FAILURE);
+ }
+ stream_client_conn_set_addr(conn, "127.0.0.1");
+ stream_client_conn_set_port(conn, 10000);
+ stream_client_conn_set_connect_cb(conn, connect_cb);
+ stream_client_conn_set_read_cb(conn, read_cb);
+
+ if (stream_client_conn_open(conn) < 0) {
+ fprintf(stderr, "cannot open client\n");
+ exit(EXIT_FAILURE);
+ }
+
+ kbd_ofd = talloc_zero(tall_test, struct osmo_fd);
+ if (!kbd_ofd) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "OOM\n");
+ exit(EXIT_FAILURE);
+ }
+ kbd_ofd->fd = STDIN_FILENO;
+ kbd_ofd->when = BSC_FD_READ;
+ kbd_ofd->data = conn;
+ kbd_ofd->cb = kbd_cb;
+ osmo_fd_register(kbd_ofd);
+
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "Entering main loop\n");
+
+ while(1) {
+ osmo_select_main(0);
+ }
+}
diff --git a/examples/stream-server.c b/examples/stream-server.c
new file mode 100644
index 0000000..2a49052
--- /dev/null
+++ b/examples/stream-server.c
@@ -0,0 +1,147 @@
+/* stream server example */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <osmocom/core/select.h>
+#include <osmocom/core/talloc.h>
+#include <osmocom/core/msgb.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/application.h>
+
+#include <osmocom/netif/stream.h>
+
+static void *tall_test;
+
+#define DSTREAMTEST 0
+
+struct log_info_cat stream_server_test_cat[] = {
+ [DSTREAMTEST] = {
+ .name = "DSTREAMTEST",
+ .description = "STREAMSERVER-mode test",
+ .color = "\033[1;35m",
+ .enabled = 1, .loglevel = LOGL_DEBUG,
+ },
+};
+
+const struct log_info stream_server_test_log_info = {
+ .filter_fn = NULL,
+ .cat = stream_server_test_cat,
+ .num_cat = ARRAY_SIZE(stream_server_test_cat),
+};
+
+static struct stream_server_link *server;
+static struct stream_server_conn *conn;
+
+
+void sighandler(int foo)
+{
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "closing STREAMSERVER.\n");
+ exit(EXIT_SUCCESS);
+}
+
+int read_cb(struct stream_server_conn *conn, struct msgb *msg)
+{
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "received message from stream\n");
+ return 0;
+}
+
+static int close_cb(struct stream_server_conn *dummy)
+{
+ conn = NULL;
+ return 0;
+}
+
+static int accept_cb(struct stream_server_link *server, int fd)
+{
+ if (conn != NULL) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "Sorry, this example only "
+ "support one client simultaneously\n");
+ return -1;
+ }
+
+ conn = stream_server_conn_create(tall_test, server, fd, read_cb,
+ close_cb, NULL);
+ if (conn == NULL) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "error in stream_server_receive\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int kbd_cb(struct osmo_fd *fd, unsigned int what)
+{
+ char buf[1024];
+ struct msgb *msg;
+ uint8_t *ptr;
+ int ret;
+
+ ret = read(STDIN_FILENO, buf, sizeof(buf));
+
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "read %d byte from keyboard\n", ret);
+
+ if (conn == NULL) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "no client, skipping\n");
+ return 0;
+ }
+
+ msg = msgb_alloc(1024, "stream_server_test");
+ if (msg == NULL) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "stream_server: cannot allocate message\n");
+ return 0;
+ }
+ ptr = msgb_put(msg, strlen(buf));
+ memcpy(ptr, buf, strlen(buf));
+ stream_server_conn_send(conn, msg);
+
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "message of %d bytes sent\n", msg->len);
+
+ return 0;
+}
+
+int main(void)
+{
+ struct osmo_fd *kbd_ofd;
+
+ tall_test = talloc_named_const(NULL, 1, "stream_server_test");
+
+ osmo_init_logging(&stream_server_test_log_info);
+ log_set_log_level(osmo_stderr_target, 1);
+
+ /*
+ * initialize stream server.
+ */
+
+ server = stream_server_link_create(tall_test);
+ if (server == NULL) {
+ fprintf(stderr, "cannot create client\n");
+ exit(EXIT_FAILURE);
+ }
+ stream_server_link_set_addr(server, "127.0.0.1");
+ stream_server_link_set_port(server, 10000);
+ stream_server_link_set_accept_cb(server, accept_cb);
+
+ if (stream_server_link_open(server) < 0) {
+ fprintf(stderr, "cannot open client\n");
+ exit(EXIT_FAILURE);
+ }
+
+ kbd_ofd = talloc_zero(tall_test, struct osmo_fd);
+ if (!kbd_ofd) {
+ LOGP(DSTREAMTEST, LOGL_ERROR, "OOM\n");
+ exit(EXIT_FAILURE);
+ }
+ kbd_ofd->fd = STDIN_FILENO;
+ kbd_ofd->when = BSC_FD_READ;
+ kbd_ofd->data = server;
+ kbd_ofd->cb = kbd_cb;
+ osmo_fd_register(kbd_ofd);
+
+ LOGP(DSTREAMTEST, LOGL_NOTICE, "Entering main loop\n");
+
+ while(1) {
+ osmo_select_main(0);
+ }
+}