diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2012-12-11 06:13:02 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2012-12-11 06:13:02 +0000 |
commit | 36adc76235fb0a38e7042bc751f988b71627e2a0 (patch) | |
tree | fc83b3f4e0afeb3932c8d200f5493f0a8d6c4619 /sca-cpp/trunk/components/chat | |
parent | 86b1de85536e93c59a25702a5a2d3e384202ffd2 (diff) |
Changes to get successful C++11 based build. Code cleanup, dependency upgrades, and const + inline optimizations in components, samples, and app hosting server.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1420007 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp/trunk/components/chat')
-rw-r--r-- | sca-cpp/trunk/components/chat/Makefile.am | 10 | ||||
-rw-r--r-- | sca-cpp/trunk/components/chat/chat-send.cpp | 4 | ||||
-rw-r--r-- | sca-cpp/trunk/components/chat/chat-sender.cpp | 91 | ||||
-rw-r--r-- | sca-cpp/trunk/components/chat/chat-sender2.cpp | 36 | ||||
-rw-r--r-- | sca-cpp/trunk/components/chat/chat-sendreceiver.cpp | 99 | ||||
-rw-r--r-- | sca-cpp/trunk/components/chat/client-test.cpp | 39 | ||||
-rw-r--r-- | sca-cpp/trunk/components/chat/xmpp-test.cpp | 23 | ||||
-rw-r--r-- | sca-cpp/trunk/components/chat/xmpp.hpp | 73 |
8 files changed, 136 insertions, 239 deletions
diff --git a/sca-cpp/trunk/components/chat/Makefile.am b/sca-cpp/trunk/components/chat/Makefile.am index 5c995ad452..1efbb5b298 100644 --- a/sca-cpp/trunk/components/chat/Makefile.am +++ b/sca-cpp/trunk/components/chat/Makefile.am @@ -58,7 +58,7 @@ xmpp_test_SOURCES = xmpp-test.cpp xmpp_test_LDFLAGS = -L${LIBSTROPHE_LIB} -R${LIBSTROPHE_LIB} -lstrophe -lssl -lresolv client_test_SOURCES = client-test.cpp -client_test_LDFLAGS = -lxml2 -lcurl -lmozjs -L${LIBSTROPHE_LIB} -R${LIBSTROPHE_LIB} -lstrophe -lssl -lresolv +client_test_LDFLAGS = -lxml2 -lcurl -ljansson -L${LIBSTROPHE_LIB} -R${LIBSTROPHE_LIB} -lstrophe -lssl -lresolv comp_PROGRAMS = chat-send @@ -67,12 +67,18 @@ dist_noinst_SCRIPTS = server-test if WANT_VYSPER +noinst_DATA += test/TestVysperServer.class + AM_JAVACFLAGS = -cp `${top_builddir}/components/chat/vysper-classpath ${VYSPER_PREFIX}`${JAVAROOT} dist_noinst_JAVA = test/*.java +.java.class: + ${JAVAC} ${AM_JAVACFLAGS} test/*.java + CLEANFILES = test/*.class dist_noinst_SCRIPTS += echo-test -TESTS = echo-test server-test +#TESTS = echo-test server-test + endif endif diff --git a/sca-cpp/trunk/components/chat/chat-send.cpp b/sca-cpp/trunk/components/chat/chat-send.cpp index bb3907acfd..b4c21a49ee 100644 --- a/sca-cpp/trunk/components/chat/chat-send.cpp +++ b/sca-cpp/trunk/components/chat/chat-send.cpp @@ -37,7 +37,7 @@ namespace tuscany { namespace chat { -bool sendmsg(const string& jid, const string& pass, const string& to, const string& msg) { +const bool sendmsg(const string& jid, const string& pass, const string& to, const string& msg) { XMPPClient xc(jid, pass); const failable<bool> c = connect(xc); assert(hasContent(c)); @@ -49,7 +49,7 @@ bool sendmsg(const string& jid, const string& pass, const string& to, const stri } } -int main(unused const int argc, const char** argv) { +int main(unused const int argc, const char** const argv) { tuscany::chat::sendmsg(argv[1], argv[2], argv[3], argv[4]); return 0; } diff --git a/sca-cpp/trunk/components/chat/chat-sender.cpp b/sca-cpp/trunk/components/chat/chat-sender.cpp index a4cabef8de..64d0271d43 100644 --- a/sca-cpp/trunk/components/chat/chat-sender.cpp +++ b/sca-cpp/trunk/components/chat/chat-sender.cpp @@ -51,45 +51,38 @@ const failable<value> post(const list<value>& params, XMPPClient& xc) { } /** - * Subscribe and listen to an XMPP session. + * Start the component. */ -class noop { -public: - noop() { - } - - const failable<bool> operator()(unused const value& jid, unused const value& val, unused XMPPClient& xc) const { - return true; - } -}; - -class subscribe { -public: - subscribe(XMPPClient& xc) : xc(xc) { - } - - const failable<bool> operator()() const { - gc_pool pool; +const failable<value> start(const list<value>& params) { + // Extract the the XMPP JID and password + const list<value> props = params; + const value jid = ((lvvlambda)car(props))(nilListValue); + const value pass = ((lvvlambda)cadr(props))(nilListValue); + + // Create an XMPP client session + XMPPClient xc(jid, pass, false); + const failable<bool> r = connect(xc); + if (!hasContent(r)) + return mkfailure<value>(r); + + // Subscribe and listen to XMPP session + const lambda<const failable<bool>()> subscribe = [xc]() -> const failable<bool> { + const gc_pool pool; debug("chat::subscribe::listen"); - const failable<bool> r = listen(noop(), const_cast<XMPPClient&>(xc)); + const lambda<const failable<bool>(const value&, const value&, XMPPClient&)> noop = [](unused const value& jid, unused const value& val, unused XMPPClient& xc) -> const failable<bool> { + return true; + }; + const failable<bool> r = listen(noop, const_cast<XMPPClient&>(xc)); debug("chat::subscribe::stopped"); return r; - } - -private: - const lambda<failable<bool>(const value&, const value&, XMPPClient&)> l; - XMPPClient xc; -}; + }; -/** - * Chatter component lambda function - */ -class chatSender { -public: - chatSender(XMPPClient& xc, worker& w) : xc(xc), w(w) { - } + // Listen and relay messages in a worker thread + worker w(3); + submit<failable<bool> >(w, subscribe); - const value operator()(const list<value>& params) const { + // Return the chat sender component lambda function + const lvvlambda chatSender = [xc, w](const list<value>& params) -> const value { const tuscany::value func(car(params)); if (func == "post") return post(cdr(params), const_cast<XMPPClient&>(xc)); @@ -104,35 +97,9 @@ public: cancel(const_cast<worker&>(w)); debug("chat::sender::stopped"); - return failable<value>(value(lambda<value(const list<value>&)>())); - } - -private: - const XMPPClient xc; - worker w; -}; - -/** - * Start the component. - */ -const failable<value> start(const list<value>& params) { - // Extract the the XMPP JID and password - const list<value> props = params; - const value jid = ((lambda<value(const list<value>&)>)car(props))(list<value>()); - const value pass = ((lambda<value(const list<value>&)>)cadr(props))(list<value>()); - - // Create an XMPP client session - XMPPClient xc(jid, pass, false); - const failable<bool> r = connect(xc); - if (!hasContent(r)) - return mkfailure<value>(r); - - // Listen and relay messages in a worker thread - worker w(3); - submit<failable<bool> >(w, lambda<failable<bool>()>(subscribe(xc))); - - // Return the chat sender component lambda function - return value(lambda<value(const list<value>&)>(chatSender(xc, w))); + return failable<value>(value(lvvlambda())); + }; + return value(chatSender); } } diff --git a/sca-cpp/trunk/components/chat/chat-sender2.cpp b/sca-cpp/trunk/components/chat/chat-sender2.cpp index 0e00728022..2101617899 100644 --- a/sca-cpp/trunk/components/chat/chat-sender2.cpp +++ b/sca-cpp/trunk/components/chat/chat-sender2.cpp @@ -41,7 +41,7 @@ namespace sender { /** * Post an item to an XMPP JID. */ -const failable<value> post(const lambda<value(const list<value>&)>& jid, const lambda<value(const list<value>&)>& pass, const lambda<value(const list<value>&)>& to, const lambda<value(const list<value>&)>& msg, const list<value>& params) { +const failable<value> post(const lvvlambda& jid, const lvvlambda& pass, const lvvlambda& to, const lvvlambda& msg, const list<value>& params) { const value vjid = jid(mklist<value>("get", params)); const value vpass = pass(mklist<value>("get", params)); @@ -65,14 +65,16 @@ const failable<value> post(const lambda<value(const list<value>&)>& jid, const l } /** - * Chat sender component lambda function + * Start the component. */ -class chatSender { -public: - chatSender(const lambda<value(const list<value>&)>& jid, const lambda<value(const list<value>&)>& pass, const lambda<value(const list<value>&)>& to, const lambda<value(const list<value>&)>& msg) : jid(jid), pass(pass), to(to), msg(msg) { - } +const failable<value> start(const list<value>& params) { - const value operator()(const list<value>& params) const { + // Return the chat sender component lambda function + const lvvlambda jid = car(params); + const lvvlambda pass = cadr(params); + const lvvlambda to = caddr(params); + const lvvlambda msg = cadddr(params); + const lvvlambda sender = [jid, pass, to, msg](const list<value>& params) -> const value { const tuscany::value func(car(params)); if (func == "get") return post(jid, pass, to, msg, cdr(params)); @@ -81,23 +83,9 @@ public: if (func != "stop") return mkfailure<value>(); debug("chat::sender::stop"); - return failable<value>(value(lambda<value(const list<value>&)>())); - } - -private: - const lambda<value(const list<value>&)> jid; - const lambda<value(const list<value>&)> pass; - const lambda<value(const list<value>&)> to; - const lambda<value(const list<value>&)> msg; -}; - -/** - * Start the component. - */ -const failable<value> start(const list<value>& params) { - - // Return the chat sender component lambda function - return value(lambda<value(const list<value>&)>(chatSender(car(params), cadr(params), caddr(params), cadddr(params)))); + return failable<value>(value(lvvlambda())); + }; + return value(sender); } } diff --git a/sca-cpp/trunk/components/chat/chat-sendreceiver.cpp b/sca-cpp/trunk/components/chat/chat-sendreceiver.cpp index bfbd32b9ae..94577b7551 100644 --- a/sca-cpp/trunk/components/chat/chat-sendreceiver.cpp +++ b/sca-cpp/trunk/components/chat/chat-sendreceiver.cpp @@ -51,56 +51,46 @@ const failable<value> post(const list<value>& params, XMPPClient& xc) { } /** - * A relay function that posts the XMPP messages it receives to a relay component reference. + * Start the component. */ -class relay { -public: - relay(const lambda<value(const list<value>&)>& rel) : rel(rel) { - } +const failable<value> start(const list<value>& params) { + // Extract the relay reference and the XMPP JID and password + const bool hasRelay = !isNil(cddr(params)); + const lvvlambda rel = hasRelay? (lvvlambda)car(params) : lvvlambda(); + const list<value> props = hasRelay? cdr(params) : params; + const value jid = ((lvvlambda)car(props))(nilListValue); + const value pass = ((lvvlambda)cadr(props))(nilListValue); + + // Create an XMPP client session + XMPPClient xc(jid, pass, false); + const failable<bool> r = connect(xc); + if (!hasContent(r)) + return mkfailure<value>(r); - const failable<bool> operator()(const value& jid, const value& val, unused XMPPClient& xc) const { + // Listen and relay messages in a worker thread + worker w(3); + const lambda<const failable<bool>(const value&, const value&, XMPPClient&)> rl = [rel](const value& jid, const value& val, unused XMPPClient& xc) -> const failable<bool> { + // A relay function that posts the XMPP messages it receives to a relay component reference. if (isNil(rel)) return true; debug(jid, "chat::relay::jid"); debug(val, "chat::relay::value"); const value res = rel(mklist<value>("post", mklist<value>(jid), val)); return true; - } - -private: - const lambda<value(const list<value>&)> rel; -}; - -/** - * Subscribe and listen to an XMPP session. - */ -class subscribe { -public: - subscribe(const lambda<failable<bool>(const value&, const value&, XMPPClient&)>& l, XMPPClient& xc) : l(l), xc(xc) { - } + }; - const failable<bool> operator()() const { - gc_pool pool; + // Subscribe and listen to the XMPP session. + const lambda<const failable<bool>()> subscribe = [rl, xc]() -> const failable<bool> { + const gc_pool pool; debug("chat::subscribe::listen"); - const failable<bool> r = listen(l, const_cast<XMPPClient&>(xc)); + const failable<bool> r = listen(rl, const_cast<XMPPClient&>(xc)); debug("chat::subscribe::stopped"); return r; - } - -private: - const lambda<failable<bool>(const value&, const value&, XMPPClient&)> l; - XMPPClient xc; -}; - -/** - * Chat sender/receiver component lambda function - */ -class chatSenderReceiver { -public: - chatSenderReceiver(XMPPClient& xc, worker& w) : xc(xc), w(w) { - } + }; + submit<failable<bool> >(w, subscribe); - const value operator()(const list<value>& params) const { + // Return the chat sender/receiver component lambda function + const lvvlambda senderReceiver = [xc, w](const list<value>& params) -> const value { const tuscany::value func(car(params)); if (func == "post") return post(cdr(params), const_cast<XMPPClient&>(xc)); @@ -115,38 +105,9 @@ public: cancel(const_cast<worker&>(w)); debug("chat::sendreceiver::stopped"); - return failable<value>(value(lambda<value(const list<value>&)>())); - } - -private: - const XMPPClient xc; - worker w; -}; - -/** - * Start the component. - */ -const failable<value> start(const list<value>& params) { - // Extract the relay reference and the XMPP JID and password - const bool hasRelay = !isNil(cddr(params)); - const value rel = hasRelay? car(params) : value(lambda<value(const list<value>&)>()); - const list<value> props = hasRelay? cdr(params) : params; - const value jid = ((lambda<value(const list<value>&)>)car(props))(list<value>()); - const value pass = ((lambda<value(const list<value>&)>)cadr(props))(list<value>()); - - // Create an XMPP client session - XMPPClient xc(jid, pass, false); - const failable<bool> r = connect(xc); - if (!hasContent(r)) - return mkfailure<value>(r); - - // Listen and relay messages in a worker thread - worker w(3); - const lambda<failable<bool>(const value&, const value&, XMPPClient&)> rl = relay(rel); - submit<failable<bool> >(w, lambda<failable<bool>()>(subscribe(rl, xc))); - - // Return the chat sender/receiver component lambda function - return value(lambda<value(const list<value>&)>(chatSenderReceiver(xc, w))); + return failable<value>(value(lvvlambda())); + }; + return value(senderReceiver); } } diff --git a/sca-cpp/trunk/components/chat/client-test.cpp b/sca-cpp/trunk/components/chat/client-test.cpp index 220382fa89..11c7bed35e 100644 --- a/sca-cpp/trunk/components/chat/client-test.cpp +++ b/sca-cpp/trunk/components/chat/client-test.cpp @@ -45,12 +45,12 @@ const value pass2("sca2"); const value jid3("sca3@localhost"); const value pass3("sca3"); -const list<value> item = list<value>() + "content" + (list<value>() + "item" - + (list<value>() + "name" + string("Apple")) - + (list<value>() + "price" + string("$2.99"))); -const list<value> entry = list<value>() + (list<value>() + "entry" - + (list<value>() + "title" + string("item")) - + (list<value>() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) +const list<value> item = nilListValue + "content" + (nilListValue + "item" + + (nilListValue + "name" + string("Apple")) + + (nilListValue + "price" + string("$2.99"))); +const list<value> entry = nilListValue + (nilListValue + "entry" + + (nilListValue + "title" + string("item")) + + (nilListValue + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + item); worker w(2); @@ -63,36 +63,29 @@ const failable<bool> listener(const value& from, const value& val, unused XMPPCl return false; } -struct subscribe { - XMPPClient& xc; - subscribe(XMPPClient& xc) : xc(xc) { - } - const failable<bool> operator()() const { - const lambda<failable<bool>(const value&, const value&, XMPPClient&)> l(listener); - listen(l, xc); - return true; - } -}; - -bool testListen() { +const bool testListen() { received = false; XMPPClient& xc = *(new (gc_new<XMPPClient>()) XMPPClient(jid3, pass3)); const failable<bool> c = connect(xc); assert(hasContent(c)); - const lambda<failable<bool>()> subs = subscribe(xc); + const lambda<const failable<bool>()> subs = [&xc]() -> const failable<bool> { + const lambda<const failable<bool>(const value&, const value&, XMPPClient&)> l(listener); + listen(l, xc); + return true; + }; submit(w, subs); return true; } -bool testPost() { - gc_scoped_pool pool; - http::CURLSession ch("", "", "", "", 0); +const bool testPost() { + const gc_scoped_pool pool; + const http::CURLSession ch("", "", "", "", 0); const failable<value> id = http::post(entry, "http://localhost:8090/print-sender/sca2@localhost", ch); assert(hasContent(id)); return true; } -bool testReceived() { +const bool testReceived() { shutdown(w); assert(received == true); return true; diff --git a/sca-cpp/trunk/components/chat/xmpp-test.cpp b/sca-cpp/trunk/components/chat/xmpp-test.cpp index 6b7fa3439f..853029a836 100644 --- a/sca-cpp/trunk/components/chat/xmpp-test.cpp +++ b/sca-cpp/trunk/components/chat/xmpp-test.cpp @@ -52,28 +52,21 @@ const failable<bool> listener(const value& from, const value& val, unused XMPPCl return false; } -struct subscribe { - XMPPClient& xc; - subscribe(XMPPClient& xc) : xc(xc) { - } - const failable<bool> operator()() const { - const lambda<failable<bool>(const value&, const value&, XMPPClient&)> l(listener); - listen(l, xc); - return true; - } -}; - -bool testListen() { +const bool testListen() { received = false; XMPPClient& xc = *(new (gc_new<XMPPClient>()) XMPPClient(jid2, pass2)); const failable<bool> c = connect(xc); assert(hasContent(c)); - const lambda<failable<bool>()> subs = subscribe(xc); + const lambda<const failable<bool>()> subs = [&xc]() -> const failable<bool> { + const lambda<const failable<bool>(const value&, const value&, XMPPClient&)> l(listener); + listen(l, xc); + return true; + }; submit(w, subs); return true; } -bool testPost() { +const bool testPost() { XMPPClient xc(jid1, pass1); const failable<bool> c = connect(xc); assert(hasContent(c)); @@ -82,7 +75,7 @@ bool testPost() { return true; } -bool testReceived() { +const bool testReceived() { shutdown(w); assert(received == true); return true; diff --git a/sca-cpp/trunk/components/chat/xmpp.hpp b/sca-cpp/trunk/components/chat/xmpp.hpp index aa006029fa..88174613a0 100644 --- a/sca-cpp/trunk/components/chat/xmpp.hpp +++ b/sca-cpp/trunk/components/chat/xmpp.hpp @@ -66,8 +66,8 @@ private: */ class XMPPClient { public: - XMPPClient(const string& jid, const string& pass, bool owner = true) : owner(owner), ctx(xmpp_ctx_new(NULL, xmppRuntime.log)), conn(xmpp_conn_new(ctx)), connecting(false), connected(false), disconnecting(false) { - xmpp_conn_set_jid(conn, c_str(jid + "/" + mkuuid())); + XMPPClient(const string& jid, const string& pass, const bool owner = true) : owner(owner), ctx(xmpp_ctx_new(NULL, xmppRuntime.log)), conn(xmpp_conn_new(ctx)), connecting(false), connected(false), disconnecting(false) { + xmpp_conn_set_jid(conn, c_str(jid + "/" + (string)mkuuid())); xmpp_conn_set_pass(conn, c_str(pass)); debug(jid, "chat::xmppclient::jid"); } @@ -76,19 +76,7 @@ public: debug("chat::xmppclient::copy"); } - const XMPPClient& operator=(const XMPPClient& xc) { - debug("chat::xmppclient::operator="); - if(this == &xc) - return *this; - owner = false; - ctx = xc.ctx; - conn = xc.conn; - listener = xc.listener; - connecting = xc.connecting; - connected = xc.connected; - disconnecting = xc.disconnecting; - return *this; - } + XMPPClient& operator=(const XMPPClient& xc) = delete; ~XMPPClient() { debug("chat::~xmppclient"); @@ -110,12 +98,12 @@ private: friend const failable<size_t> send(xmpp_stanza_t* const stanza, XMPPClient& xc); friend const failable<bool> post(const value& to, const value& val, XMPPClient& xc); friend const failable<bool> disconnect(XMPPClient& xc); - friend const failable<bool> listen(const lambda<failable<bool>(const value&, const value&, XMPPClient&)>& listener, XMPPClient& xc); + friend const failable<bool> listen(const lambda<const failable<bool>(const value&, const value&, XMPPClient&)>& listener, XMPPClient& xc); - bool owner; + const bool owner; xmpp_ctx_t* ctx; xmpp_conn_t* conn; - lambda<failable<bool>(const value&, const value&, XMPPClient&)> listener; + gc_mutable_ref<lambda<const failable<bool>(const value&, const value&, XMPPClient&)> > listener; bool connecting; bool connected; bool disconnecting; @@ -124,8 +112,8 @@ private: /** * Make a text stanza. */ -xmpp_stanza_t* textStanza(const char* text, xmpp_ctx_t* ctx) { - xmpp_stanza_t* stanza = xmpp_stanza_new(ctx); +xmpp_stanza_t* const textStanza(const char* const text, xmpp_ctx_t* const ctx) { + xmpp_stanza_t* const stanza = xmpp_stanza_new(ctx); xmpp_stanza_set_text(stanza, text); return stanza; } @@ -133,8 +121,8 @@ xmpp_stanza_t* textStanza(const char* text, xmpp_ctx_t* ctx) { /** * Make a named stanza. */ -xmpp_stanza_t* namedStanza(const char* ns, const char* name, xmpp_ctx_t* ctx) { - xmpp_stanza_t* stanza = xmpp_stanza_new(ctx); +xmpp_stanza_t* const namedStanza(const char* const ns, const char* const name, xmpp_ctx_t* const ctx) { + xmpp_stanza_t* const stanza = xmpp_stanza_new(ctx); xmpp_stanza_set_name(stanza, name); if (ns != NULL) xmpp_stanza_set_ns(stanza, ns); @@ -144,8 +132,8 @@ xmpp_stanza_t* namedStanza(const char* ns, const char* name, xmpp_ctx_t* ctx) { /** * Make a named stanza using a qualified name. */ -xmpp_stanza_t* namedStanza(const char* name, xmpp_ctx_t* ctx) { - xmpp_stanza_t* stanza = xmpp_stanza_new(ctx); +xmpp_stanza_t* const namedStanza(const char* const name, xmpp_ctx_t* const ctx) { + xmpp_stanza_t* const stanza = xmpp_stanza_new(ctx); xmpp_stanza_set_name(stanza, name); return stanza; } @@ -157,16 +145,16 @@ int versionHandler(xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, void* c XMPPClient& xc = *(XMPPClient*)udata; // Build version reply stanza - xmpp_stanza_t* reply = namedStanza("iq", xc.ctx); + xmpp_stanza_t* const reply = namedStanza("iq", xc.ctx); xmpp_stanza_set_type(reply, "result"); xmpp_stanza_set_id(reply, xmpp_stanza_get_id(stanza)); xmpp_stanza_set_attribute(reply, "to", xmpp_stanza_get_attribute(stanza, "from")); - xmpp_stanza_t* query = namedStanza(xmpp_stanza_get_ns(xmpp_stanza_get_children(stanza)), "query", xc.ctx); + xmpp_stanza_t* const query = namedStanza(xmpp_stanza_get_ns(xmpp_stanza_get_children(stanza)), "query", xc.ctx); xmpp_stanza_add_child(reply, query); - xmpp_stanza_t* name = namedStanza("name", xc.ctx); + xmpp_stanza_t* const name = namedStanza("name", xc.ctx); xmpp_stanza_add_child(query, name); xmpp_stanza_add_child(name, textStanza("Apache Tuscany", xc.ctx)); - xmpp_stanza_t* version = namedStanza("version", xc.ctx); + xmpp_stanza_t* const version = namedStanza("version", xc.ctx); xmpp_stanza_add_child(query, version); xmpp_stanza_add_child(version, textStanza("1.0", xc.ctx)); @@ -188,17 +176,18 @@ int messageHandler(unused xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, // Call the client listener function XMPPClient& xc = *(XMPPClient*)udata; - const char* from = xmpp_stanza_get_attribute(stanza, "from"); - const char* text = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body")); - if (isNil(xc.listener)) + const char* const from = xmpp_stanza_get_attribute(stanza, "from"); + const char* const text = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body")); + if (isNil((lambda<const failable<bool>(const value&, const value&, XMPPClient&)>)xc.listener)) return 1; - const value val(scheme::readValue(text)); + const value val(content(scheme::readValue(text))); debug(from, "chat::messageHandler::from"); debug(val, "chat::messageHandler::body"); - const failable<bool> r = xc.listener(value(string(from)), val, xc); + const lambda<const failable<bool>(const value&, const value&, XMPPClient&)> listener = xc.listener; + const failable<bool> r = listener(value(string(from)), val, xc); if (!hasContent(r) || !content(r)) { // Stop listening - xc.listener = lambda<failable<bool>(const value&, const value&, XMPPClient&)>(); + xc.listener = lambda<const failable<bool>(const value&, const value&, XMPPClient&)>(); return 0; } return 1; @@ -216,7 +205,7 @@ void connHandler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, unuse xmpp_handler_add(conn, versionHandler, "jabber:iq:version", "iq", NULL, &xc); // Send a <presence/> stanza so that we appear online to contacts - xmpp_stanza_t* pres = xmpp_stanza_new(xc.ctx); + xmpp_stanza_t* const pres = xmpp_stanza_new(xc.ctx); xmpp_stanza_set_name(pres, "presence"); xmpp_send(conn, pres); xmpp_stanza_release(pres); @@ -246,7 +235,7 @@ const failable<bool> connect(XMPPClient& xc) { /** * Send a buffer on an XMPP session. */ -const failable<size_t> send(const char* data, const size_t len, XMPPClient& xc) { +const failable<size_t> send(const char* const data, const size_t len, XMPPClient& xc) { if (len == 0) return 0; const size_t written = xc.conn->tls? tls_write(xc.conn->tls, data, len) : sock_write(xc.conn->sock, data, len); @@ -268,7 +257,7 @@ const failable<size_t> send(const string& data, XMPPClient& xc) { * Send a stanza on an XMPP session. */ const failable<size_t> send(xmpp_stanza_t* const stanza, XMPPClient& xc) { - char *buf; + char* buf; size_t len; const int rc = xmpp_stanza_to_text(stanza, &buf, &len); if (rc != 0) @@ -291,13 +280,13 @@ const failable<bool> post(const value& to, const value& val, XMPPClient& xc) { debug(val, "chat::post::body"); // Convert the value to a string - const string vs(scheme::writeValue(val)); + const string vs(write(content(scheme::writeValue(val)))); // Build message stanza - xmpp_stanza_t* stanza = namedStanza("message", xc.ctx); + xmpp_stanza_t* const stanza = namedStanza("message", xc.ctx); xmpp_stanza_set_type(stanza, "chat"); xmpp_stanza_set_attribute(stanza, "to", c_str(string(to))); - xmpp_stanza_t* body = namedStanza("body", xc.ctx); + xmpp_stanza_t* const body = namedStanza("body", xc.ctx); xmpp_stanza_add_child(stanza, body); xmpp_stanza_add_child(body, textStanza(c_str(vs), xc.ctx)); @@ -323,12 +312,12 @@ const failable<bool> disconnect(XMPPClient& xc) { /** * Listen to messages received by an XMPP client. */ -const failable<bool> listen(const lambda<failable<bool>(const value&, const value&, XMPPClient&)>& listener, XMPPClient& xc) { +const failable<bool> listen(const lambda<const failable<bool>(const value&, const value&, XMPPClient&)>& listener, XMPPClient& xc) { debug("chat::listen"); xc.listener = listener; xmpp_handler_add(xc.conn, messageHandler, NULL, "message", NULL, &xc); xc.ctx->loop_status = XMPP_LOOP_RUNNING; - while(xc.connected && !isNil(xc.listener) && xc.ctx->loop_status == XMPP_LOOP_RUNNING) + while(xc.connected && !isNil((lambda<const failable<bool>(const value&, const value&, XMPPClient&)>)xc.listener) && xc.ctx->loop_status == XMPP_LOOP_RUNNING) xmpp_run_once(xc.ctx, 1000L); return true; } |