summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Smith <git@kismith.co.uk>2010-03-30 14:34:50 (GMT)
committerKevin Smith <git@kismith.co.uk>2010-03-30 14:34:50 (GMT)
commitf5cb5e389a5720247d81e48d3e404a3311a538cb (patch)
tree38c9de625aa75c6e3741e5ea5c7f9f3c7982d571 /Swiften/Examples/ConnectivityTest
parent5fc2378d29ca59cf2d3bf0633912d5336fd43de0 (diff)
downloadswift-contrib-f5cb5e389a5720247d81e48d3e404a3311a538cb.zip
swift-contrib-f5cb5e389a5720247d81e48d3e404a3311a538cb.tar.bz2
Connectivity test Swiften example
Diffstat (limited to 'Swiften/Examples/ConnectivityTest')
-rw-r--r--Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp87
-rw-r--r--Swiften/Examples/ConnectivityTest/SConscript13
2 files changed, 100 insertions, 0 deletions
diff --git a/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp b/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp
new file mode 100644
index 0000000..09d9cd6
--- /dev/null
+++ b/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp
@@ -0,0 +1,87 @@
+#include <boost/bind.hpp>
+#include <boost/thread.hpp>
+
+#include "Swiften/Client/Client.h"
+#include "Swiften/Network/BoostTimer.h"
+#include "Swiften/EventLoop/MainEventLoop.h"
+#include "Swiften/Client/ClientXMLTracer.h"
+#include "Swiften/EventLoop/SimpleEventLoop.h"
+#include "Swiften/Network/BoostIOServiceThread.h"
+#include "Swiften/Network/MainBoostIOServiceThread.h"
+#include "Swiften/Queries/Requests/GetDiscoInfoRequest.h"
+
+using namespace Swift;
+
+enum ExitCodes {OK = 0, CANNOT_CONNECT, CANNOT_AUTH, NO_RESPONSE, DISCO_ERROR};
+
+SimpleEventLoop eventLoop;
+
+Client* client = 0;
+JID recipient;
+int exitCode = CANNOT_CONNECT;
+boost::bsignals::connection errorConnection;
+
+void handleServerDiscoInfoResponse(boost::shared_ptr<DiscoInfo> /*info*/, const boost::optional<ErrorPayload>& error) {
+ if (!error) {
+ errorConnection.disconnect();
+ client->disconnect();
+ eventLoop.stop();
+ exitCode = OK;
+ } else {
+ errorConnection.disconnect();
+ exitCode = DISCO_ERROR;
+ }
+}
+
+void handleConnected() {
+ exitCode = NO_RESPONSE;
+ boost::shared_ptr<GetDiscoInfoRequest> discoInfoRequest(new GetDiscoInfoRequest(JID(), client));
+ discoInfoRequest->onResponse.connect(handleServerDiscoInfoResponse);
+ discoInfoRequest->send();
+}
+
+void handleError(const ClientError&) {
+ exitCode = CANNOT_AUTH;
+ eventLoop.stop();
+}
+
+
+
+int main(int argc, char* argv[]) {
+ if (argc < 4 || argc > 5) {
+ std::cerr << "Usage: " << argv[0] << " <jid> [<connect_host>] <password> <timeout_seconds>" << std::endl;
+ return -1;
+ }
+
+ int argi = 1;
+
+ String jid = argv[argi++];
+ String connectHost = "";
+ if (argc == 5) {
+ connectHost = argv[argi++];
+ }
+
+ client = new Swift::Client(JID(jid), String(argv[argi++]));
+ char* timeoutChar = argv[argi++];
+ int timeout = atoi(timeoutChar);
+ ClientXMLTracer* tracer = new ClientXMLTracer(client);
+ client->onConnected.connect(&handleConnected);
+ errorConnection = client->onError.connect(&handleError);
+ if (!connectHost.isEmpty()) {
+ client->connect(connectHost);
+ } else {
+ client->connect();
+ }
+
+ {
+ boost::shared_ptr<BoostTimer> timer(new BoostTimer((timeout ? timeout : 30) * 1000, &MainBoostIOServiceThread::getInstance().getIOService()));
+ timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop));
+ timer->start();
+
+ eventLoop.run();
+ }
+
+ delete tracer;
+ delete client;
+ return exitCode;
+}
diff --git a/Swiften/Examples/ConnectivityTest/SConscript b/Swiften/Examples/ConnectivityTest/SConscript
new file mode 100644
index 0000000..7a943dc
--- /dev/null
+++ b/Swiften/Examples/ConnectivityTest/SConscript
@@ -0,0 +1,13 @@
+Import("env")
+
+myenv = env.Clone()
+myenv.MergeFlags(myenv["SWIFTEN_FLAGS"])
+myenv.MergeFlags(myenv["CPPUNIT_FLAGS"])
+myenv.MergeFlags(myenv["LIBIDN_FLAGS"])
+myenv.MergeFlags(myenv["BOOST_FLAGS"])
+myenv.MergeFlags(myenv["SQLITE_FLAGS"])
+myenv.MergeFlags(myenv["ZLIB_FLAGS"])
+myenv.MergeFlags(myenv["OPENSSL_FLAGS"])
+myenv.MergeFlags(myenv.get("LIBXML_FLAGS", ""))
+myenv.MergeFlags(myenv.get("EXPAT_FLAGS", ""))
+tester = myenv.Program("ConnectivityTest", ["ConnectivityTest.cpp"])