From c52ba858468b1d820502cbecfe986b7d7206face Mon Sep 17 00:00:00 2001 From: Milan Kubik Date: Sun, 14 Dec 2014 11:29:27 +0100 Subject: [PATCH] XEP-0114: Added an example for component connection. The example is based on basic.c program. To run the example, a configured server is needed. --- Makefile.am | 7 ++- examples/component.c | 100 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 examples/component.c diff --git a/Makefile.am b/Makefile.am index b78ada1..13d5b80 100644 --- a/Makefile.am +++ b/Makefile.am @@ -41,7 +41,7 @@ pkgconfig_DATA = libstrophe.pc EXTRA_DIST = docs ## Examples -noinst_PROGRAMS = examples/active examples/roster examples/basic examples/bot +noinst_PROGRAMS = examples/active examples/roster examples/basic examples/bot examples/component examples_active_SOURCES = examples/active.c examples_active_CFLAGS = $(STROPHE_FLAGS) examples_active_LDADD = $(STROPHE_LIBS) @@ -53,7 +53,10 @@ examples_basic_CFLAGS = $(STROPHE_FLAGS) examples_basic_LDADD = $(STROPHE_LIBS) examples_bot_SOURCES = examples/bot.c examples_bot_CFLAGS = $(STROPHE_FLAGS) -examples_bot_LDADD = $(STROPHE_LIBS) +examples_bot_LDADD = $(STROPHE_LIBS) +examples_component_SOURCES = examples/component.c +examples_component_CFLAGS = $(STROPHE_FLAGS) +examples_component_LDADD = $(STROPHE_LIBS) ## Tests diff --git a/examples/component.c b/examples/component.c new file mode 100644 index 0000000..cd1326a --- /dev/null +++ b/examples/component.c @@ -0,0 +1,100 @@ +/* basic.c +** libstrophe XMPP client library -- basic usage example +** +** Copyright (C) 2005-2009 Collecta, Inc. +** +** This software is provided AS-IS with no warranty, either express +** or implied. +** +** This software is distributed under license and may not be copied, +** modified or distributed except as expressly authorized under the +** terms of the license contained in the file LICENSE.txt in this +** distribution. +*/ + +/* This example demonstrates simple connection to a server +** as an external component. See XEP-0114 for more details. +** This program requires correctly configured server to run. +*/ + +#include +#include + +#include + + +/* define a handler for connection events */ +void conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, + const int error, xmpp_stream_error_t * const stream_error, + void * const userdata) +{ + xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + + if (status == XMPP_CONN_CONNECT) { + fprintf(stderr, "DEBUG: connected\n"); + xmpp_disconnect(conn); + } + else { + fprintf(stderr, "DEBUG: disconnected\n"); + xmpp_stop(ctx); + } +} + +int main(int argc, char **argv) +{ + xmpp_ctx_t *ctx; + xmpp_conn_t *conn; + xmpp_log_t *log; + char *jid, *pass, *host, *port_err = NULL; + unsigned short port = 0; + + /* take a jid and password on the command line */ + if (argc < 4 || argc > 5) { + fprintf(stderr, "Usage: component []\n\n"); + return 1; + } + + jid = argv[1]; + pass = argv[2]; + host = argv[3]; + + if (argc == 5) { + short tmp_port = (short) strtol(argv[4], &port_err, 10); + if (tmp_port < 0 || *port_err != '\0') { + fprintf(stderr, "Invalid value of [%s].\n", argv[4]); + return 1; + } + port = (unsigned short) tmp_port; + } + + /* init library */ + xmpp_initialize(); + + /* create a context */ + log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */ + ctx = xmpp_ctx_new(NULL, log); + + /* create a connection */ + conn = xmpp_conn_new(ctx); + + /* setup authentication information */ + xmpp_conn_set_jid(conn, jid); + xmpp_conn_set_pass(conn, pass); + + /* initiate connection */ + xmpp_connect_component(conn, host, port, conn_handler, ctx); + + /* enter the event loop - + our connect handler will trigger an exit */ + xmpp_run(ctx); + + /* release our connection and context */ + xmpp_conn_release(conn); + xmpp_ctx_free(ctx); + + /* final shutdown of the library */ + xmpp_shutdown(); + + return 0; +} +