summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Examples/SendFile/SendFile.cpp')
-rw-r--r--Swiften/Examples/SendFile/SendFile.cpp242
1 files changed, 116 insertions, 126 deletions
diff --git a/Swiften/Examples/SendFile/SendFile.cpp b/Swiften/Examples/SendFile/SendFile.cpp
index b056842..2b701c7 100644
--- a/Swiften/Examples/SendFile/SendFile.cpp
+++ b/Swiften/Examples/SendFile/SendFile.cpp
@@ -1,34 +1,34 @@
/*
- * Copyright (c) 2010-2013 Remko Tronçon
- * Licensed under the GNU General Public License v3.
- * See Documentation/Licenses/GPLv3.txt for more information.
+ * Copyright (c) 2010-2016 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
*/
-#include <boost/date_time/posix_time/posix_time.hpp>
+#include <iostream>
+
#include <boost/bind.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
-#include <iostream>
+#include <Swiften/Base/ByteArray.h>
+#include <Swiften/Base/Log.h>
#include <Swiften/Client/Client.h>
+#include <Swiften/Client/ClientXMLTracer.h>
+#include <Swiften/Disco/EntityCapsManager.h>
#include <Swiften/Elements/Presence.h>
-#include <Swiften/Base/Log.h>
-#include <Swiften/Network/BoostTimer.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/FileTransfer/OutgoingSIFileTransfer.h>
#include <Swiften/FileTransfer/FileReadBytestream.h>
-#include <Swiften/FileTransfer/SOCKS5BytestreamServer.h>
-#include <Swiften/Network/BoostConnectionServer.h>
-#include <Swiften/FileTransfer/OutgoingFileTransferManager.h>
+#include <Swiften/FileTransfer/FileTransferManager.h>
#include <Swiften/FileTransfer/OutgoingFileTransfer.h>
+#include <Swiften/FileTransfer/OutgoingFileTransferManager.h>
+#include <Swiften/FileTransfer/SOCKS5BytestreamServer.h>
#include <Swiften/Jingle/JingleSessionManager.h>
-#include <Swiften/Disco/EntityCapsManager.h>
-#include <Swiften/Base/ByteArray.h>
+#include <Swiften/Network/BoostConnectionServer.h>
+#include <Swiften/Network/BoostNetworkFactories.h>
+#include <Swiften/Network/BoostTimer.h>
+#include <Swiften/Network/TimerFactory.h>
#include <Swiften/StringCodecs/Hexify.h>
-#include <Swiften/FileTransfer/FileTransferManager.h>
using namespace Swift;
@@ -38,117 +38,107 @@ 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) {
-
- 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() {
- client->sendPresence(Presence::create());
-
- //ByteArray fileData;
- //readByteArrayFromFile(fileData, file);
-
- // gather file information
- /*StreamInitiationFileInfo fileInfo;
-
- fileInfo.setName(file.filename());
- fileInfo.setSize(boost::filesystem::file_size(file));
- fileInfo.setDescription("Some file!");
- fileInfo.setDate(boost::posix_time::from_time_t(boost::filesystem::last_write_time(file)));*/
- //fileInfo.setHash(Hexify::hexify(MD5::getHash(fileData)));
- /*
- transfer = new OutgoingSIFileTransfer("myid", client->getJID(), recipient, file.filename(), boost::filesystem::file_size(file), "A file", boost::make_shared<FileReadBytestream>(file)), client->getIQRouter(), socksBytestreamServer);
- transfer->onFinished.connect(boost::bind(&FileSender::handleFileTransferFinished, this, _1));
- transfer->start();
- */
- }
-
- 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) {
- std::cout << "started FT" << std::endl;
- outgoingFileTransfer->start();
- // TODO: getting notified about FT status and end
- } else {
- std::cout << "[ ERROR ] " << recipient << " doesn't support any kind of file transfer!" << std::endl;
- //client->disconnect();
- }
- }
- }
-
- void handleDisconnected(const boost::optional<ClientError>&) {
- std::cerr << "Error!" << std::endl;
- exit(-1);
- }
-
- void handleFileTransferFinished(const boost::optional<FileTransferError>& error) {
- std::cout << "File transfer finished" << std::endl;
- if (error) {
- exit(-1);
- }
- else {
- 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(std::unique_ptr<Swift::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));
+ }
+
+ 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;
+ 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;
+ ClientXMLTracer* tracer;
+ std::unique_ptr<Swift::Client> client;
};
int main(int argc, char* argv[]) {
- if (argc != 5) {
- std::cerr << "Usage: " << argv[0] << " <jid> <password> <recipient> <file>" << std::endl;
- return -1;
- }
-
- JID sender(argv[1]);
- JID recipient(argv[3]);
- Log::setLogLevel(Log::debug);
- FileSender fileSender(sender, std::string(argv[2]), recipient, boost::filesystem::path(argv[4]));
- fileSender.start();
- {
- /*BoostTimer::ref timer(BoostTimer::create(30000, &MainBoostIOServiceThread::getInstance().getIOService()));
- 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;
}