diff options
Diffstat (limited to 'Swiften/Examples')
-rw-r--r-- | Swiften/Examples/BenchTool/BenchTool.cpp | 73 | ||||
-rw-r--r-- | Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp | 139 | ||||
-rw-r--r-- | Swiften/Examples/LinkLocalTool/SConscript | 4 | ||||
-rw-r--r-- | Swiften/Examples/LinkLocalTool/main.cpp | 64 | ||||
-rw-r--r-- | Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp | 135 | ||||
-rw-r--r-- | Swiften/Examples/NetworkTool/SConscript | 12 | ||||
-rw-r--r-- | Swiften/Examples/NetworkTool/main.cpp | 113 | ||||
-rw-r--r-- | Swiften/Examples/ParserTester/ParserTester.cpp | 83 | ||||
-rw-r--r-- | Swiften/Examples/SConscript | 16 | ||||
-rw-r--r-- | Swiften/Examples/SendFile/ReceiveFile.cpp | 203 | ||||
-rw-r--r-- | Swiften/Examples/SendFile/SendFile.cpp | 206 | ||||
-rw-r--r-- | Swiften/Examples/SendMessage/SendMessage.cpp | 121 |
12 files changed, 587 insertions, 582 deletions
diff --git a/Swiften/Examples/BenchTool/BenchTool.cpp b/Swiften/Examples/BenchTool/BenchTool.cpp index 5b6da5f..97d760d 100644 --- a/Swiften/Examples/BenchTool/BenchTool.cpp +++ b/Swiften/Examples/BenchTool/BenchTool.cpp @@ -1,21 +1,22 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include <boost/bind.hpp> -#include <boost/thread.hpp> #include <iostream> +#include <thread> + +#include <boost/bind.hpp> +#include <Swiften/Base/sleep.h> #include <Swiften/Client/Client.h> -#include <Swiften/Network/TimerFactory.h> -#include <Swiften/Network/BoostNetworkFactories.h> +#include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/EventLoop/EventLoop.h> #include <Swiften/EventLoop/SimpleEventLoop.h> +#include <Swiften/Network/BoostNetworkFactories.h> +#include <Swiften/Network/TimerFactory.h> #include <Swiften/Roster/GetRosterRequest.h> -#include <Swiften/Client/ClientXMLTracer.h> -#include <Swiften/Base/sleep.h> #include <Swiften/TLS/BlindCertificateTrustChecker.h> using namespace Swift; @@ -27,40 +28,40 @@ static int numberOfInstances = 100; static void handleConnected() { - numberOfConnectedClients++; - std::cout << "Connected " << numberOfConnectedClients << std::endl; + numberOfConnectedClients++; + std::cout << "Connected " << numberOfConnectedClients << std::endl; } int main(int, char**) { - char* jid = getenv("SWIFT_BENCHTOOL_JID"); - if (!jid) { - std::cerr << "Please set the SWIFT_BENCHTOOL_JID environment variable" << std::endl; - return -1; - } - char* pass = getenv("SWIFT_BENCHTOOL_PASS"); - if (!pass) { - std::cerr << "Please set the SWIFT_BENCHTOOL_PASS environment variable" << std::endl; - return -1; - } + char* jid = getenv("SWIFT_BENCHTOOL_JID"); + if (!jid) { + std::cerr << "Please set the SWIFT_BENCHTOOL_JID environment variable" << std::endl; + return -1; + } + char* pass = getenv("SWIFT_BENCHTOOL_PASS"); + if (!pass) { + std::cerr << "Please set the SWIFT_BENCHTOOL_PASS environment variable" << std::endl; + return -1; + } + + BlindCertificateTrustChecker trustChecker; + std::vector<CoreClient*> clients; + for (int i = 0; i < numberOfInstances; ++i) { + CoreClient* client = new Swift::CoreClient(JID(jid), createSafeByteArray(std::string(pass)), &networkFactories); + client->setCertificateTrustChecker(&trustChecker); + client->onConnected.connect(&handleConnected); + clients.push_back(client); + } - BlindCertificateTrustChecker trustChecker; - std::vector<CoreClient*> clients; - for (int i = 0; i < numberOfInstances; ++i) { - CoreClient* client = new Swift::CoreClient(JID(jid), createSafeByteArray(std::string(pass)), &networkFactories); - client->setCertificateTrustChecker(&trustChecker); - client->onConnected.connect(&handleConnected); - clients.push_back(client); - } - - for (size_t i = 0; i < clients.size(); ++i) { - clients[i]->connect(); - } + for (auto& client : clients) { + client->connect(); + } - eventLoop.run(); + eventLoop.run(); - for (size_t i = 0; i < clients.size(); ++i) { - delete clients[i]; - } + for (auto& client : clients) { + delete client; + } - return 0; + return 0; } diff --git a/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp b/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp index 306a0df..607038f 100644 --- a/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp +++ b/Swiften/Examples/ConnectivityTest/ConnectivityTest.cpp @@ -1,21 +1,22 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include <boost/bind.hpp> -#include <boost/thread.hpp> #include <iostream> +#include <thread> + +#include <boost/bind.hpp> #include <Swiften/Client/Client.h> -#include <Swiften/Network/Timer.h> -#include <Swiften/Network/TimerFactory.h> -#include <Swiften/Network/BoostNetworkFactories.h> -#include <Swiften/EventLoop/EventLoop.h> #include <Swiften/Client/ClientXMLTracer.h> -#include <Swiften/EventLoop/SimpleEventLoop.h> #include <Swiften/Disco/GetDiscoInfoRequest.h> +#include <Swiften/EventLoop/EventLoop.h> +#include <Swiften/EventLoop/SimpleEventLoop.h> +#include <Swiften/Network/BoostNetworkFactories.h> +#include <Swiften/Network/Timer.h> +#include <Swiften/Network/TimerFactory.h> using namespace Swift; @@ -24,78 +25,78 @@ enum ExitCodes {OK = 0, CANNOT_CONNECT, CANNOT_AUTH, NO_RESPONSE, DISCO_ERROR}; static SimpleEventLoop eventLoop; static BoostNetworkFactories networkFactories(&eventLoop); -static Client* client = 0; +static Client* client = nullptr; static JID recipient; static int exitCode = CANNOT_CONNECT; -static boost::bsignals::connection errorConnection; - -static void handleServerDiscoInfoResponse(boost::shared_ptr<DiscoInfo> /*info*/, ErrorPayload::ref error) { - if (!error) { - errorConnection.disconnect(); - client->disconnect(); - eventLoop.stop(); - exitCode = OK; - } else { - errorConnection.disconnect(); - exitCode = DISCO_ERROR; - } +static boost::signals2::connection errorConnection; + +static void handleServerDiscoInfoResponse(std::shared_ptr<DiscoInfo> /*info*/, ErrorPayload::ref error) { + if (!error) { + errorConnection.disconnect(); + client->disconnect(); + eventLoop.stop(); + exitCode = OK; + } else { + errorConnection.disconnect(); + exitCode = DISCO_ERROR; + } } static void handleConnected() { - exitCode = NO_RESPONSE; - GetDiscoInfoRequest::ref discoInfoRequest = GetDiscoInfoRequest::create(JID(), client->getIQRouter()); - discoInfoRequest->onResponse.connect(&handleServerDiscoInfoResponse); - discoInfoRequest->send(); + exitCode = NO_RESPONSE; + GetDiscoInfoRequest::ref discoInfoRequest = GetDiscoInfoRequest::create(JID(), client->getIQRouter()); + discoInfoRequest->onResponse.connect(&handleServerDiscoInfoResponse); + discoInfoRequest->send(); } static void handleDisconnected(const boost::optional<ClientError>&) { - exitCode = CANNOT_AUTH; - eventLoop.stop(); + 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; - - std::string jid = argv[argi++]; - std::string connectHost = ""; - if (argc == 5) { - connectHost = argv[argi++]; - } - - client = new Swift::Client(JID(jid), std::string(argv[argi++]), &networkFactories); - char* timeoutChar = argv[argi++]; - int timeout = atoi(timeoutChar); - timeout = (timeout ? timeout : 30) * 1000; - ClientXMLTracer* tracer = new ClientXMLTracer(client); - client->onConnected.connect(&handleConnected); - errorConnection = client->onDisconnected.connect(&handleDisconnected); - std::cout << "Connecting to JID " << jid << " with timeout " << timeout << "ms on host: "; ; - if (!connectHost.empty()) { - std::cout << connectHost << std::endl; - ClientOptions options; - options.manualHostname = connectHost; - client->connect(options); - } else { - std::cout << " Default" << std::endl; - client->connect(); - } - - { - Timer::ref timer = networkFactories.getTimerFactory()->createTimer(timeout); - timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); - timer->start(); - - eventLoop.run(); - } - - delete tracer; - delete client; - return exitCode; + if (argc < 4 || argc > 5) { + std::cerr << "Usage: " << argv[0] << " <jid> [<connect_host>] <password> <timeout_seconds>" << std::endl; + return -1; + } + + int argi = 1; + + std::string jid = argv[argi++]; + std::string connectHost = ""; + if (argc == 5) { + connectHost = argv[argi++]; + } + + client = new Swift::Client(JID(jid), std::string(argv[argi++]), &networkFactories); + char* timeoutChar = argv[argi++]; + int timeout = atoi(timeoutChar); + timeout = (timeout ? timeout : 30) * 1000; + ClientXMLTracer* tracer = new ClientXMLTracer(client); + client->onConnected.connect(&handleConnected); + errorConnection = client->onDisconnected.connect(&handleDisconnected); + std::cout << "Connecting to JID " << jid << " with timeout " << timeout << "ms on host: "; ; + if (!connectHost.empty()) { + std::cout << connectHost << std::endl; + ClientOptions options; + options.manualHostname = connectHost; + client->connect(options); + } else { + std::cout << " Default" << std::endl; + client->connect(); + } + + { + Timer::ref timer = networkFactories.getTimerFactory()->createTimer(timeout); + timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); + timer->start(); + + eventLoop.run(); + } + + delete tracer; + delete client; + return exitCode; } diff --git a/Swiften/Examples/LinkLocalTool/SConscript b/Swiften/Examples/LinkLocalTool/SConscript index 18eb91f..d7617ed 100644 --- a/Swiften/Examples/LinkLocalTool/SConscript +++ b/Swiften/Examples/LinkLocalTool/SConscript @@ -5,5 +5,5 @@ myenv.UseFlags(myenv["SWIFTEN_FLAGS"]) myenv.UseFlags(myenv["SWIFTEN_DEP_FLAGS"]) linkLocalTool = myenv.Program("LinkLocalTool", [ - "main.cpp" - ]) + "main.cpp" + ]) diff --git a/Swiften/Examples/LinkLocalTool/main.cpp b/Swiften/Examples/LinkLocalTool/main.cpp index 4f87810..a853f0e 100644 --- a/Swiften/Examples/LinkLocalTool/main.cpp +++ b/Swiften/Examples/LinkLocalTool/main.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -9,41 +9,41 @@ #include <iostream> #include <Swiften/EventLoop/SimpleEventLoop.h> -#include <Swiften/LinkLocal/DNSSD/PlatformDNSSDQuerierFactory.h> -#include <Swiften/LinkLocal/DNSSD/DNSSDQuerier.h> #include <Swiften/LinkLocal/DNSSD/DNSSDBrowseQuery.h> +#include <Swiften/LinkLocal/DNSSD/DNSSDQuerier.h> #include <Swiften/LinkLocal/DNSSD/DNSSDResolveServiceQuery.h> +#include <Swiften/LinkLocal/DNSSD/PlatformDNSSDQuerierFactory.h> using namespace Swift; int main(int argc, char* argv[]) { - if (argc < 2) { - std::cerr << "Invalid parameters" << std::endl; - return -1; - } - - SimpleEventLoop eventLoop; - PlatformDNSSDQuerierFactory factory(&eventLoop); - boost::shared_ptr<DNSSDQuerier> querier = factory.createQuerier(); - querier->start(); - - if (std::string(argv[1]) == "browse") { - boost::shared_ptr<DNSSDBrowseQuery> browseQuery = querier->createBrowseQuery(); - browseQuery->startBrowsing(); - eventLoop.run(); - browseQuery->stopBrowsing(); - } - else if (std::string(argv[1]) == "resolve-service") { - if (argc < 5) { - std::cerr << "Invalid parameters" << std::endl; - return -1; - } - boost::shared_ptr<DNSSDResolveServiceQuery> resolveQuery = querier->createResolveServiceQuery(DNSSDServiceID(argv[2], argv[3], argv[4])); - resolveQuery->start(); - eventLoop.run(); - std::cerr << "Done running" << std::endl; - resolveQuery->stop(); - } - - querier->stop(); + if (argc < 2) { + std::cerr << "Invalid parameters" << std::endl; + return -1; + } + + SimpleEventLoop eventLoop; + PlatformDNSSDQuerierFactory factory(&eventLoop); + std::shared_ptr<DNSSDQuerier> querier = factory.createQuerier(); + querier->start(); + + if (std::string(argv[1]) == "browse") { + std::shared_ptr<DNSSDBrowseQuery> browseQuery = querier->createBrowseQuery(); + browseQuery->startBrowsing(); + eventLoop.run(); + browseQuery->stopBrowsing(); + } + else if (std::string(argv[1]) == "resolve-service") { + if (argc < 5) { + std::cerr << "Invalid parameters" << std::endl; + return -1; + } + std::shared_ptr<DNSSDResolveServiceQuery> resolveQuery = querier->createResolveServiceQuery(DNSSDServiceID(argv[2], argv[3], argv[4])); + resolveQuery->start(); + eventLoop.run(); + std::cerr << "Done running" << std::endl; + resolveQuery->stop(); + } + + querier->stop(); } diff --git a/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp b/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp index 216d16d..10b0b40 100644 --- a/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp +++ b/Swiften/Examples/MUCListAndJoin/MUCListAndJoin.cpp @@ -5,11 +5,10 @@ */ #include <iostream> +#include <memory> #include <boost/optional.hpp> -#include <boost/shared_ptr.hpp> -#include <Swiften/Base/foreach.h> #include <Swiften/Client/Client.h> #include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/Disco/GetDiscoItemsRequest.h> @@ -27,94 +26,94 @@ using namespace std; static SimpleEventLoop eventLoop; static BoostNetworkFactories networkFactories(&eventLoop); -static boost::shared_ptr<Client> client; +static std::shared_ptr<Client> client; static MUC::ref muc; static JID mucJID; static JID roomJID; static void joinMUC() { - cout << "Joining " << roomJID.toString() << endl; + cout << "Joining " << roomJID.toString() << endl; - muc = client->getMUCManager()->createMUC(roomJID); - muc->joinAs("SwiftExample"); + muc = client->getMUCManager()->createMUC(roomJID); + muc->joinAs("SwiftExample"); } -static void handleRoomsItemsResponse(boost::shared_ptr<DiscoItems> items, ErrorPayload::ref error) { - if (error) { - cout << "Error fetching list of rooms." << endl; - return; - } - - int roomCount = 0; - cout << "List of rooms at " << mucJID.toString() << endl; - foreach (DiscoItems::Item item, items->getItems()) { - roomCount++; - cout << "\t" << roomCount << ". " << item.getJID().getNode() << " - " << item.getName() << std::endl; - if (roomCount == 1) { - roomJID = item.getJID(); - } - } - cout << endl; - joinMUC(); +static void handleRoomsItemsResponse(std::shared_ptr<DiscoItems> items, ErrorPayload::ref error) { + if (error) { + cout << "Error fetching list of rooms." << endl; + return; + } + + int roomCount = 0; + cout << "List of rooms at " << mucJID.toString() << endl; + for (auto&& item : items->getItems()) { + roomCount++; + cout << "\t" << roomCount << ". " << item.getJID().getNode() << " - " << item.getName() << std::endl; + if (roomCount == 1) { + roomJID = item.getJID(); + } + } + cout << endl; + joinMUC(); } static void handleConnected() { - cout << "Connected." << endl; - // search for MUC rooms + cout << "Connected." << endl; + // search for MUC rooms - GetDiscoItemsRequest::ref discoItemsRequest = GetDiscoItemsRequest::create(mucJID, client->getIQRouter()); - discoItemsRequest->onResponse.connect(&handleRoomsItemsResponse); + GetDiscoItemsRequest::ref discoItemsRequest = GetDiscoItemsRequest::create(mucJID, client->getIQRouter()); + discoItemsRequest->onResponse.connect(&handleRoomsItemsResponse); - cout << "Request list of rooms." << endl; - discoItemsRequest->send(); + cout << "Request list of rooms." << endl; + discoItemsRequest->send(); } static void handleDisconnected(const boost::optional<ClientError>&) { - cout << "Disconnected." << endl; + cout << "Disconnected." << endl; } -static void handleIncomingMessage(boost::shared_ptr<Message> message) { - if (message->getFrom().toBare() == roomJID) { - cout << "[ " << roomJID << " ] " << message->getFrom().getResource() << ": " << message->getBody().get_value_or("") << endl; - } +static void handleIncomingMessage(std::shared_ptr<Message> message) { + if (message->getFrom().toBare() == roomJID) { + cout << "[ " << roomJID << " ] " << message->getFrom().getResource() << ": " << message->getBody().get_value_or("") << endl; + } } /* * Usage: ./MUCListAndJoin <jid> <password> <muc_domain> */ int main(int argc, char* argv[]) { - int ret = 0; - - if (argc != 4) { - cout << "Usage: ./" << argv[0] << " <jid> <password> <muc_domain>" << endl; - ret = -1; - } - else { - mucJID = JID(argv[3]); - client = boost::make_shared<Client>(JID(argv[1]), string(argv[2]), &networkFactories); - client->setAlwaysTrustCertificates(); - - // Enable the following line for detailed XML logging - // ClientXMLTracer* tracer = new ClientXMLTracer(client.get()); - - client->onConnected.connect(&handleConnected); - client->onDisconnected.connect(&handleDisconnected); - client->onMessageReceived.connect(&handleIncomingMessage); - - cout << "Connecting..." << flush; - client->connect(); - { - Timer::ref timer = networkFactories.getTimerFactory()->createTimer(30000); - timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); - - Timer::ref disconnectTimer = networkFactories.getTimerFactory()->createTimer(25000); - disconnectTimer->onTick.connect(boost::bind(&Client::disconnect, client.get())); - - timer->start(); - disconnectTimer->start(); - - eventLoop.run(); - } - } - return ret; + int ret = 0; + + if (argc != 4) { + cout << "Usage: ./" << argv[0] << " <jid> <password> <muc_domain>" << endl; + ret = -1; + } + else { + mucJID = JID(argv[3]); + client = std::make_shared<Client>(JID(argv[1]), string(argv[2]), &networkFactories); + client->setAlwaysTrustCertificates(); + + // Enable the following line for detailed XML logging + // ClientXMLTracer* tracer = new ClientXMLTracer(client.get()); + + client->onConnected.connect(&handleConnected); + client->onDisconnected.connect(&handleDisconnected); + client->onMessageReceived.connect(&handleIncomingMessage); + + cout << "Connecting..." << flush; + client->connect(); + { + Timer::ref timer = networkFactories.getTimerFactory()->createTimer(30000); + timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); + + Timer::ref disconnectTimer = networkFactories.getTimerFactory()->createTimer(25000); + disconnectTimer->onTick.connect(boost::bind(&Client::disconnect, client.get())); + + timer->start(); + disconnectTimer->start(); + + eventLoop.run(); + } + } + return ret; } diff --git a/Swiften/Examples/NetworkTool/SConscript b/Swiften/Examples/NetworkTool/SConscript index 38622ff..c850232 100644 --- a/Swiften/Examples/NetworkTool/SConscript +++ b/Swiften/Examples/NetworkTool/SConscript @@ -1,10 +1,10 @@ Import("env") if env["experimental"] : - myenv = env.Clone() - myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) - myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) + myenv = env.Clone() + myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) + myenv.MergeFlags(myenv["SWIFTEN_DEP_FLAGS"]) - linkLocalTool = myenv.Program("NetworkTool", [ - "main.cpp" - ]) + linkLocalTool = myenv.Program("NetworkTool", [ + "main.cpp" + ]) diff --git a/Swiften/Examples/NetworkTool/main.cpp b/Swiften/Examples/NetworkTool/main.cpp index 895bdc1..c50ab67 100644 --- a/Swiften/Examples/NetworkTool/main.cpp +++ b/Swiften/Examples/NetworkTool/main.cpp @@ -1,17 +1,18 @@ /* - * Copyright (c) 2011 Isode Limited. + * Copyright (c) 2011-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <iostream> + #include <boost/lexical_cast.hpp> #include <Swiften/EventLoop/SimpleEventLoop.h> -#include <Swiften/Network/PlatformNATTraversalWorker.h> -#include <Swiften/Network/NATTraversalGetPublicIPRequest.h> #include <Swiften/Network/NATTraversalForwardPortRequest.h> +#include <Swiften/Network/NATTraversalGetPublicIPRequest.h> #include <Swiften/Network/NATTraversalRemovePortForwardingRequest.h> +#include <Swiften/Network/PlatformNATTraversalWorker.h> #include <Swiften/Network/PlatformNetworkEnvironment.h> using namespace Swift; @@ -19,67 +20,67 @@ using namespace Swift; static SimpleEventLoop eventLoop; static void handleGetPublicIPRequestResponse(const boost::optional<HostAddress>& result) { - if (result) { - std::cerr << "Result: " << result->toString() << std::endl;; - } - else { - std::cerr << "No result" << std::endl; - } - eventLoop.stop(); + if (result) { + std::cerr << "Result: " << result->toString() << std::endl;; + } + else { + std::cerr << "No result" << std::endl; + } + eventLoop.stop(); } static void handleGetForwardPortRequestResponse(const boost::optional<NATPortMapping>& result) { - if (result) { - std::cerr << "Result: " << result->getPublicPort() << " -> " << result->getLocalPort() << std::endl;; - } - else { - std::cerr << "No result" << std::endl; - } - eventLoop.stop(); + if (result) { + std::cerr << "Result: " << result->getPublicPort() << " -> " << result->getLocalPort() << std::endl;; + } + else { + std::cerr << "No result" << std::endl; + } + eventLoop.stop(); } static void handleRemovePortForwardingRequestResponse(const boost::optional<bool> result) { - if (result && result.get()) { - std::cerr << "Result: OK" << std::endl; - } - else { - std::cerr << "Result: ERROR" << std::endl; - } - eventLoop.stop(); + if (result && result.get()) { + std::cerr << "Result: OK" << std::endl; + } + else { + std::cerr << "Result: ERROR" << std::endl; + } + eventLoop.stop(); } int main(int argc, char* argv[]) { - if (argc < 2) { - std::cerr << "Invalid parameters" << std::endl; - return -1; - } + if (argc < 2) { + std::cerr << "Invalid parameters" << std::endl; + return -1; + } - PlatformNATTraversalWorker natTraverser(&eventLoop); - if (std::string(argv[1]) == "get-public-ip") { - boost::shared_ptr<NATTraversalGetPublicIPRequest> query = natTraverser.createGetPublicIPRequest(); - query->onResult.connect(boost::bind(&handleGetPublicIPRequestResponse, _1)); - query->start(); - eventLoop.run(); - } - else if (std::string(argv[1]) == "add-port-forward") { - if (argc < 4) { - std::cerr << "Invalid parameters" << std::endl; - } - boost::shared_ptr<NATTraversalForwardPortRequest> query = natTraverser.createForwardPortRequest(boost::lexical_cast<int>(argv[2]), boost::lexical_cast<int>(argv[3])); - query->onResult.connect(boost::bind(&handleGetForwardPortRequestResponse, _1)); - query->start(); - eventLoop.run(); - } - else if (std::string(argv[1]) == "remove-port-forward") { - if (argc < 4) { - std::cerr << "Invalid parameters" << std::endl; - } - boost::shared_ptr<NATTraversalRemovePortForwardingRequest> query = natTraverser.createRemovePortForwardingRequest(boost::lexical_cast<int>(argv[2]), boost::lexical_cast<int>(argv[3])); - query->onResult.connect(boost::bind(&handleRemovePortForwardingRequestResponse, _1)); - query->start(); - eventLoop.run(); - } - else if (std::string(argv[1]) == "get-local-ip") { - std::cout << PlatformNetworkEnvironment().getLocalAddress().toString() << std::endl; - } + PlatformNATTraversalWorker natTraverser(&eventLoop); + if (std::string(argv[1]) == "get-public-ip") { + std::shared_ptr<NATTraversalGetPublicIPRequest> query = natTraverser.createGetPublicIPRequest(); + query->onResult.connect(boost::bind(&handleGetPublicIPRequestResponse, _1)); + query->start(); + eventLoop.run(); + } + else if (std::string(argv[1]) == "add-port-forward") { + if (argc < 4) { + std::cerr << "Invalid parameters" << std::endl; + } + std::shared_ptr<NATTraversalForwardPortRequest> query = natTraverser.createForwardPortRequest(boost::lexical_cast<int>(argv[2]), boost::lexical_cast<int>(argv[3])); + query->onResult.connect(boost::bind(&handleGetForwardPortRequestResponse, _1)); + query->start(); + eventLoop.run(); + } + else if (std::string(argv[1]) == "remove-port-forward") { + if (argc < 4) { + std::cerr << "Invalid parameters" << std::endl; + } + std::shared_ptr<NATTraversalRemovePortForwardingRequest> query = natTraverser.createRemovePortForwardingRequest(boost::lexical_cast<int>(argv[2]), boost::lexical_cast<int>(argv[3])); + query->onResult.connect(boost::bind(&handleRemovePortForwardingRequestResponse, _1)); + query->start(); + eventLoop.run(); + } + else if (std::string(argv[1]) == "get-local-ip") { + std::cout << PlatformNetworkEnvironment().getLocalAddress().toString() << std::endl; + } } diff --git a/Swiften/Examples/ParserTester/ParserTester.cpp b/Swiften/Examples/ParserTester/ParserTester.cpp index 8bba86d..71b0611 100644 --- a/Swiften/Examples/ParserTester/ParserTester.cpp +++ b/Swiften/Examples/ParserTester/ParserTester.cpp @@ -1,60 +1,61 @@ /* - * Copyright (c) 2010-2014 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include <iostream> +#include <Swiften/Parser/UnitTest/ParserTester.h> + #include <fstream> +#include <iostream> #include <typeinfo> -#include <Swiften/Parser/UnitTest/ParserTester.h> +#include <Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h> +#include <Swiften/Parser/PlatformXMLParserFactory.h> #include <Swiften/Parser/XMPPParser.h> #include <Swiften/Parser/XMPPParserClient.h> -#include <Swiften/Parser/PlatformXMLParserFactory.h> -#include <Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h> using namespace Swift; class MyXMPPParserClient : public XMPPParserClient { - public: - virtual void handleStreamStart(const ProtocolHeader&) { - std::cout << "-> Stream start" << std::endl; - } - virtual void handleElement(boost::shared_ptr<ToplevelElement> element) { - std::cout << "-> Element " << typeid(*element.get()).name() << std::endl; - } - virtual void handleStreamEnd() { - std::cout << "-> Stream end" << std::endl; - } + public: + virtual void handleStreamStart(const ProtocolHeader&) { + std::cout << "-> Stream start" << std::endl; + } + virtual void handleElement(std::shared_ptr<ToplevelElement> element) { + std::cout << "-> Element " << typeid(*element.get()).name() << std::endl; + } + virtual void handleStreamEnd() { + std::cout << "-> Stream end" << std::endl; + } }; int main(int argc, char* argv[]) { - if (argc != 2) { - std::cerr << "Usage: " << argv[0] << " file" << std::endl; - return 0; - } - FullPayloadParserFactoryCollection factories; - MyXMPPParserClient parserClient; - PlatformXMLParserFactory xmlParserFactory; - XMPPParser parser(&parserClient, &factories, &xmlParserFactory); - ParserTester<XMLParserClient> tester(&parser); + if (argc != 2) { + std::cerr << "Usage: " << argv[0] << " file" << std::endl; + return 0; + } + FullPayloadParserFactoryCollection factories; + MyXMPPParserClient parserClient; + PlatformXMLParserFactory xmlParserFactory; + XMPPParser parser(&parserClient, &factories, &xmlParserFactory); + ParserTester<XMLParserClient> tester(&parser); - std::string line; - std::ifstream myfile (argv[1]); - if (myfile.is_open()) { - while (!myfile.eof()) { - getline (myfile,line); - std::cout << "Parsing: " << line << std::endl; - if (!tester.parse(line)) { - std::cerr << "PARSE ERROR" << std::endl; - return -1; - } - } - myfile.close(); - } - else { - std::cerr << "Unable to open file " << argv[1] << std::endl; - } - return 0; + std::string line; + std::ifstream myfile (argv[1]); + if (myfile.is_open()) { + while (!myfile.eof()) { + getline (myfile,line); + std::cout << "Parsing: " << line << std::endl; + if (!tester.parse(line)) { + std::cerr << "PARSE ERROR" << std::endl; + return -1; + } + } + myfile.close(); + } + else { + std::cerr << "Unable to open file " << argv[1] << std::endl; + } + return 0; } diff --git a/Swiften/Examples/SConscript b/Swiften/Examples/SConscript index 8a0a48f..601cba2 100644 --- a/Swiften/Examples/SConscript +++ b/Swiften/Examples/SConscript @@ -3,12 +3,12 @@ Import("swiften_env") myenv = swiften_env.Clone() SConscript(dirs = [ - "SendMessage", - "SendFile", - "ConnectivityTest", - "LinkLocalTool", - "NetworkTool", - "ParserTester", - "BenchTool", - "MUCListAndJoin", + "SendMessage", + "SendFile", + "ConnectivityTest", + "LinkLocalTool", + "NetworkTool", + "ParserTester", + "BenchTool", + "MUCListAndJoin", ]) diff --git a/Swiften/Examples/SendFile/ReceiveFile.cpp b/Swiften/Examples/SendFile/ReceiveFile.cpp index 8f045bd..193c1b7 100644 --- a/Swiften/Examples/SendFile/ReceiveFile.cpp +++ b/Swiften/Examples/SendFile/ReceiveFile.cpp @@ -1,28 +1,28 @@ /* - * Copyright (c) 2010-2013 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ +#include <iostream> +#include <memory> + #include <boost/bind.hpp> #include <boost/filesystem.hpp> -#include <boost/smart_ptr/make_shared.hpp> -#include <iostream> -#include <Swiften/Elements/Presence.h> #include <Swiften/Base/Log.h> -#include <Swiften/Base/foreach.h> #include <Swiften/Client/Client.h> -#include <Swiften/Elements/DiscoInfo.h> -#include <Swiften/Network/BoostNetworkFactories.h> -#include <Swiften/EventLoop/SimpleEventLoop.h> #include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/Disco/ClientDiscoManager.h> -#include <Swiften/FileTransfer/IncomingFileTransferManager.h> +#include <Swiften/Elements/DiscoInfo.h> +#include <Swiften/Elements/Presence.h> +#include <Swiften/EventLoop/SimpleEventLoop.h> +#include <Swiften/FileTransfer/FileTransferManager.h> #include <Swiften/FileTransfer/FileWriteBytestream.h> -#include <Swiften/Jingle/JingleSessionManager.h> +#include <Swiften/FileTransfer/IncomingFileTransferManager.h> #include <Swiften/FileTransfer/SOCKS5BytestreamRegistry.h> -#include <Swiften/FileTransfer/FileTransferManager.h> +#include <Swiften/Jingle/JingleSessionManager.h> +#include <Swiften/Network/BoostNetworkFactories.h> using namespace Swift; @@ -35,101 +35,102 @@ static const std::string CLIENT_NAME = "Swiften FT Test"; static const std::string CLIENT_NODE = "http://swift.im"; class FileReceiver { - public: - FileReceiver(const JID& jid, const std::string& password) : jid(jid), password(password) { - client = new Swift::Client(jid, password, &networkFactories); - client->onConnected.connect(boost::bind(&FileReceiver::handleConnected, this)); - client->onDisconnected.connect(boost::bind(&FileReceiver::handleDisconnected, this, _1)); - tracer = new ClientXMLTracer(client); - } - - ~FileReceiver() { - delete tracer; - client->onDisconnected.disconnect(boost::bind(&FileReceiver::handleDisconnected, this, _1)); - client->onConnected.disconnect(boost::bind(&FileReceiver::handleConnected, this)); - delete client; - } - - void start() { - client->connect(); - } - - void stop() { - foreach(const IncomingFileTransfer::ref transfer, incomingFileTransfers) { - //transfer->stop(); - } - client->disconnect(); - } - - private: - void handleConnected() { - Log::setLogLevel(Log::debug); - client->getFileTransferManager()->onIncomingFileTransfer.connect(boost::bind(&FileReceiver::handleIncomingFileTransfer, this, _1)); - - DiscoInfo discoInfo; - discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc")); - discoInfo.addFeature(DiscoInfo::JingleFeature); - discoInfo.addFeature(DiscoInfo::JingleFTFeature); - discoInfo.addFeature(DiscoInfo::Bytestream); - discoInfo.addFeature(DiscoInfo::JingleTransportsIBBFeature); - discoInfo.addFeature(DiscoInfo::JingleTransportsS5BFeature); - client->getDiscoManager()->setCapsNode(CLIENT_NODE); - client->getDiscoManager()->setDiscoInfo(discoInfo); - client->getPresenceSender()->sendPresence(Presence::create()); - } - - void handleIncomingFileTransfer(IncomingFileTransfer::ref transfer) { - SWIFT_LOG(debug) << "foo" << std::endl; - incomingFileTransfers.push_back(transfer); - boost::shared_ptr<FileWriteBytestream> out = boost::make_shared<FileWriteBytestream>("out"); - transfer->onFinished.connect(boost::bind(&FileReceiver::handleFileTransferFinished, this, _1, out)); - transfer->accept(out); - } - - void handleDisconnected(const boost::optional<ClientError>&) { - std::cerr << "Error!" << std::endl; - exit(-1); - } - - void handleFileTransferFinished( - const boost::optional<FileTransferError>& error, - boost::shared_ptr<FileWriteBytestream> out) { - std::cout << "File transfer finished" << std::endl; - out->close(); - if (error) { - exit(-1); - } - else { - exit(0); - } - } - - void exit(int code) { - exitCode = code; - stop(); - eventLoop.stop(); - } - - private: - JID jid; - std::string password; - Client* client; - ClientXMLTracer* tracer; - std::vector<IncomingFileTransfer::ref> incomingFileTransfers; + public: + FileReceiver(const JID& jid, const std::string& password) : jid(jid), password(password) { + client = new Swift::Client(jid, password, &networkFactories); + client->onConnected.connect(boost::bind(&FileReceiver::handleConnected, this)); + client->onDisconnected.connect(boost::bind(&FileReceiver::handleDisconnected, this, _1)); + tracer = new ClientXMLTracer(client); + } + + ~FileReceiver() { + delete tracer; + client->onDisconnected.disconnect(boost::bind(&FileReceiver::handleDisconnected, this, _1)); + client->onConnected.disconnect(boost::bind(&FileReceiver::handleConnected, this)); + delete client; + } + + void start() { + client->connect(); + } + + void stop() { + for (const auto& transfer : incomingFileTransfers) { + (void)transfer; + //transfer->stop(); + } + client->disconnect(); + } + + private: + void handleConnected() { + Log::setLogLevel(Log::debug); + client->getFileTransferManager()->onIncomingFileTransfer.connect(boost::bind(&FileReceiver::handleIncomingFileTransfer, this, _1)); + + DiscoInfo discoInfo; + discoInfo.addIdentity(DiscoInfo::Identity(CLIENT_NAME, "client", "pc")); + discoInfo.addFeature(DiscoInfo::JingleFeature); + discoInfo.addFeature(DiscoInfo::JingleFTFeature); + discoInfo.addFeature(DiscoInfo::Bytestream); + discoInfo.addFeature(DiscoInfo::JingleTransportsIBBFeature); + discoInfo.addFeature(DiscoInfo::JingleTransportsS5BFeature); + client->getDiscoManager()->setCapsNode(CLIENT_NODE); + client->getDiscoManager()->setDiscoInfo(discoInfo); + client->getPresenceSender()->sendPresence(Presence::create()); + } + + void handleIncomingFileTransfer(IncomingFileTransfer::ref transfer) { + SWIFT_LOG(debug) << "foo" << std::endl; + incomingFileTransfers.push_back(transfer); + std::shared_ptr<FileWriteBytestream> out = std::make_shared<FileWriteBytestream>("out"); + transfer->onFinished.connect(boost::bind(&FileReceiver::handleFileTransferFinished, this, _1, out)); + transfer->accept(out); + } + + void handleDisconnected(const boost::optional<ClientError>&) { + std::cerr << "Error!" << std::endl; + exit(-1); + } + + void handleFileTransferFinished( + const boost::optional<FileTransferError>& error, + std::shared_ptr<FileWriteBytestream> out) { + std::cout << "File transfer finished" << std::endl; + out->close(); + if (error) { + exit(-1); + } + else { + exit(0); + } + } + + void exit(int code) { + exitCode = code; + stop(); + eventLoop.stop(); + } + + private: + JID jid; + std::string password; + Client* client; + ClientXMLTracer* tracer; + std::vector<IncomingFileTransfer::ref> incomingFileTransfers; }; int main(int argc, char* argv[]) { - if (argc != 3) { - std::cerr << "Usage: " << argv[0] << " <jid> <password>" << std::endl; - return -1; - } + if (argc != 3) { + std::cerr << "Usage: " << argv[0] << " <jid> <password>" << std::endl; + return -1; + } - JID jid(argv[1]); - FileReceiver fileReceiver(jid, std::string(argv[2])); - fileReceiver.start(); + JID jid(argv[1]); + FileReceiver fileReceiver(jid, std::string(argv[2])); + fileReceiver.start(); - eventLoop.run(); + eventLoop.run(); - return exitCode; + return exitCode; } diff --git a/Swiften/Examples/SendFile/SendFile.cpp b/Swiften/Examples/SendFile/SendFile.cpp index 5f2403a..0e1fb23 100644 --- a/Swiften/Examples/SendFile/SendFile.cpp +++ b/Swiften/Examples/SendFile/SendFile.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2015 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -38,110 +38,110 @@ static BoostNetworkFactories networkFactories(&eventLoop); static int exitCode = 2; class FileSender { - public: - FileSender(const JID& jid, const std::string& password, const JID& recipient, const boost::filesystem::path& file) : jid(jid), password(password), recipient(recipient), file(file), tracer(NULL) { - client = new Swift::Client(jid, password, &networkFactories); - client->onConnected.connect(boost::bind(&FileSender::handleConnected, this)); - client->onDisconnected.connect(boost::bind(&FileSender::handleDisconnected, this, _1)); - //tracer = new ClientXMLTracer(client); - client->getEntityCapsProvider()->onCapsChanged.connect(boost::bind(&FileSender::handleCapsChanged, this, _1)); - } - - ~FileSender() { - delete tracer; - client->onDisconnected.disconnect(boost::bind(&FileSender::handleDisconnected, this, _1)); - client->onConnected.disconnect(boost::bind(&FileSender::handleConnected, this)); - delete client; - } - - void start() { - client->connect(); - } - - private: - void handleConnected() { - std::cout << "Connected. Awaiting presence from receipient." << std::endl; - client->sendPresence(Presence::create()); - } - - void handleCapsChanged(JID jid) { - if (jid.toBare() == recipient) { - // create ReadBytestream from file - boost::shared_ptr<FileReadBytestream> fileStream = boost::make_shared<FileReadBytestream>(file); - - outgoingFileTransfer = client->getFileTransferManager()->createOutgoingFileTransfer(recipient, file, "Some File!", fileStream); - - if (outgoingFileTransfer) { - outgoingFileTransfer->onFinished.connect(boost::bind(&FileSender::handleFileTransferFinished, this, _1)); - std::cout << "Starting file-transfer to " << recipient.toString() << "." << std::endl; - outgoingFileTransfer->start(); - // TODO: getting notified about FT status and end - } else { - std::cout << recipient << " doesn't support any kind of file transfer!" << std::endl; - client->disconnect(); - } - } - } - - void handleDisconnected(const boost::optional<ClientError>& err) { - if (err) { - std::cout << "Disconnected due to error ( " << err.get().getType() << " )." << std::endl; - exit(-1); - } - else { - std::cout << "Successfully disconnected." << std::endl; - } - } - - void handleFileTransferFinished(const boost::optional<FileTransferError>& error) { - std::cout << "File transfer finished." << std::endl; - outgoingFileTransfer.reset(); - if (error) { - client->disconnect(); - exit(-1); - } - else { - client->disconnect(); - exit(0); - } - } - - void exit(int code) { - exitCode = code; - eventLoop.stop(); - } - - private: - BoostConnectionServer::ref connectionServer; - OutgoingFileTransfer::ref outgoingFileTransfer; - JID jid; - std::string password; - JID recipient; - boost::filesystem::path file; - Client* client; - ClientXMLTracer* tracer; + public: + FileSender(const JID& jid, const std::string& password, const JID& recipient, const boost::filesystem::path& file) : jid(jid), password(password), recipient(recipient), file(file), tracer(nullptr) { + client = new Swift::Client(jid, password, &networkFactories); + client->onConnected.connect(boost::bind(&FileSender::handleConnected, this)); + client->onDisconnected.connect(boost::bind(&FileSender::handleDisconnected, this, _1)); + //tracer = new ClientXMLTracer(client); + client->getEntityCapsProvider()->onCapsChanged.connect(boost::bind(&FileSender::handleCapsChanged, this, _1)); + } + + ~FileSender() { + delete tracer; + client->onDisconnected.disconnect(boost::bind(&FileSender::handleDisconnected, this, _1)); + client->onConnected.disconnect(boost::bind(&FileSender::handleConnected, this)); + delete client; + } + + void start() { + client->connect(); + } + + private: + void handleConnected() { + std::cout << "Connected. Awaiting presence from receipient." << std::endl; + client->sendPresence(Presence::create()); + } + + void handleCapsChanged(JID jid) { + if (jid.toBare() == recipient) { + // create ReadBytestream from file + std::shared_ptr<FileReadBytestream> fileStream = std::make_shared<FileReadBytestream>(file); + + outgoingFileTransfer = client->getFileTransferManager()->createOutgoingFileTransfer(recipient, file, "Some File!", fileStream); + + if (outgoingFileTransfer) { + outgoingFileTransfer->onFinished.connect(boost::bind(&FileSender::handleFileTransferFinished, this, _1)); + std::cout << "Starting file-transfer to " << recipient.toString() << "." << std::endl; + outgoingFileTransfer->start(); + // TODO: getting notified about FT status and end + } else { + std::cout << recipient << " doesn't support any kind of file transfer!" << std::endl; + client->disconnect(); + } + } + } + + void handleDisconnected(const boost::optional<ClientError>& err) { + if (err) { + std::cout << "Disconnected due to error ( " << err.get().getType() << " )." << std::endl; + exit(-1); + } + else { + std::cout << "Successfully disconnected." << std::endl; + } + } + + void handleFileTransferFinished(const boost::optional<FileTransferError>& error) { + std::cout << "File transfer finished." << std::endl; + outgoingFileTransfer.reset(); + if (error) { + client->disconnect(); + exit(-1); + } + else { + client->disconnect(); + exit(0); + } + } + + void exit(int code) { + exitCode = code; + eventLoop.stop(); + } + + private: + BoostConnectionServer::ref connectionServer; + OutgoingFileTransfer::ref outgoingFileTransfer; + JID jid; + std::string password; + JID recipient; + boost::filesystem::path file; + Client* client; + ClientXMLTracer* tracer; }; int main(int argc, char* argv[]) { - if (argc != 5) { - std::cerr << "Usage: " << argv[0] << " <jid> <password> <recipient> <file>" << std::endl; - return -1; - } - - //Log::setLogLevel(Log::debug); - - JID sender(argv[1]); - JID recipient(argv[3]); - FileSender fileSender(sender, std::string(argv[2]), recipient, boost::filesystem::path(argv[4])); - fileSender.start(); - { - Timer::ref timer = networkFactories.getTimerFactory()->createTimer(30000); - timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); - timer->start(); - - eventLoop.run(); - } - - return exitCode; + if (argc != 5) { + std::cerr << "Usage: " << argv[0] << " <jid> <password> <recipient> <file>" << std::endl; + return -1; + } + + //Log::setLogLevel(Log::debug); + + JID sender(argv[1]); + JID recipient(argv[3]); + FileSender fileSender(sender, std::string(argv[2]), recipient, boost::filesystem::path(argv[4])); + fileSender.start(); + { + Timer::ref timer = networkFactories.getTimerFactory()->createTimer(30000); + timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); + timer->start(); + + eventLoop.run(); + } + + return exitCode; } diff --git a/Swiften/Examples/SendMessage/SendMessage.cpp b/Swiften/Examples/SendMessage/SendMessage.cpp index e32c9e8..0b46b41 100644 --- a/Swiften/Examples/SendMessage/SendMessage.cpp +++ b/Swiften/Examples/SendMessage/SendMessage.cpp @@ -1,90 +1,91 @@ /* - * Copyright (c) 2010 Isode Limited. + * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ -#include <boost/bind.hpp> -#include <boost/thread.hpp> #include <iostream> +#include <thread> + +#include <boost/bind.hpp> #include <Swiften/Client/Client.h> +#include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/Elements/Message.h> -#include <Swiften/Network/BoostNetworkFactories.h> -#include <Swiften/Network/TimerFactory.h> #include <Swiften/EventLoop/EventLoop.h> -#include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/EventLoop/SimpleEventLoop.h> +#include <Swiften/Network/BoostNetworkFactories.h> +#include <Swiften/Network/TimerFactory.h> using namespace Swift; static SimpleEventLoop eventLoop; static BoostNetworkFactories networkFactories(&eventLoop); -static Client* client = 0; +static Client* client = nullptr; static JID recipient; static std::string messageBody; static int exitCode = 2; -static boost::bsignals::connection errorConnection; +static boost::signals2::connection errorConnection; static void handleConnected() { - boost::shared_ptr<Message> message(new Message()); - message->setBody(messageBody); - message->setTo(recipient); - client->sendMessage(message); - exitCode = 0; - errorConnection.disconnect(); - client->disconnect(); - eventLoop.stop(); + std::shared_ptr<Message> message(new Message()); + message->setBody(messageBody); + message->setTo(recipient); + client->sendMessage(message); + exitCode = 0; + errorConnection.disconnect(); + client->disconnect(); + eventLoop.stop(); } static void handleDisconnected(const boost::optional<ClientError>&) { - std::cerr << "Error!" << std::endl; - exitCode = 1; - eventLoop.stop(); + std::cerr << "Error!" << std::endl; + exitCode = 1; + eventLoop.stop(); } int main(int argc, char* argv[]) { - if (argc < 5 || argc > 6) { - std::cerr << "Usage: " << argv[0] << " <jid> [<connect_host>]<password> <recipient> <message>" << std::endl; - return -1; - } - - int argi = 1; - - std::string jid = argv[argi++]; - std::string connectHost = ""; - if (argc == 6) { - connectHost = argv[argi++]; - } - - client = new Swift::Client(JID(jid), std::string(argv[argi++]), &networkFactories); - client->setAlwaysTrustCertificates(); - - recipient = JID(argv[argi++]); - messageBody = std::string(argv[argi++]); - - ClientXMLTracer* tracer = new ClientXMLTracer(client); - client->onConnected.connect(&handleConnected); - errorConnection = client->onDisconnected.connect(&handleDisconnected); - if (!connectHost.empty()) { - ClientOptions options; - options.manualHostname = connectHost; - client->connect(options); - } else { - client->connect(); - } - - { - Timer::ref timer = networkFactories.getTimerFactory()->createTimer(30000); - timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); - timer->start(); - - eventLoop.run(); - } - - delete tracer; - delete client; - return exitCode; + if (argc < 5 || argc > 6) { + std::cerr << "Usage: " << argv[0] << " <jid> [<connect_host>]<password> <recipient> <message>" << std::endl; + return -1; + } + + int argi = 1; + + std::string jid = argv[argi++]; + std::string connectHost = ""; + if (argc == 6) { + connectHost = argv[argi++]; + } + + client = new Swift::Client(JID(jid), std::string(argv[argi++]), &networkFactories); + client->setAlwaysTrustCertificates(); + + recipient = JID(argv[argi++]); + messageBody = std::string(argv[argi++]); + + ClientXMLTracer* tracer = new ClientXMLTracer(client); + client->onConnected.connect(&handleConnected); + errorConnection = client->onDisconnected.connect(&handleDisconnected); + if (!connectHost.empty()) { + ClientOptions options; + options.manualHostname = connectHost; + client->connect(options); + } else { + client->connect(); + } + + { + Timer::ref timer = networkFactories.getTimerFactory()->createTimer(30000); + timer->onTick.connect(boost::bind(&SimpleEventLoop::stop, &eventLoop)); + timer->start(); + + eventLoop.run(); + } + + delete tracer; + delete client; + return exitCode; } |