diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-10-17 12:13:36 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-10-21 18:25:00 (GMT) |
commit | 1b58ef2af54456004390a0888c3edf104e3baa99 (patch) | |
tree | dbe4ae29de1b765a88ea704dfaa1c03af4b196b3 /Swiften/Examples/SendFile | |
parent | 07402c4e3451f2084a1c3ddc5bacfb38a66899a7 (diff) | |
download | swift-contrib-1b58ef2af54456004390a0888c3edf104e3baa99.zip swift-contrib-1b58ef2af54456004390a0888c3edf104e3baa99.tar.bz2 |
Added beginnings of outgoing file transfer to Swiften.
Diffstat (limited to 'Swiften/Examples/SendFile')
-rw-r--r-- | Swiften/Examples/SendFile/.gitignore | 1 | ||||
-rw-r--r-- | Swiften/Examples/SendFile/SConscript | 13 | ||||
-rw-r--r-- | Swiften/Examples/SendFile/SendFile.cpp | 124 |
3 files changed, 138 insertions, 0 deletions
diff --git a/Swiften/Examples/SendFile/.gitignore b/Swiften/Examples/SendFile/.gitignore new file mode 100644 index 0000000..f8b7625 --- /dev/null +++ b/Swiften/Examples/SendFile/.gitignore @@ -0,0 +1 @@ +SendFile diff --git a/Swiften/Examples/SendFile/SConscript b/Swiften/Examples/SendFile/SConscript new file mode 100644 index 0000000..50cbe40 --- /dev/null +++ b/Swiften/Examples/SendFile/SConscript @@ -0,0 +1,13 @@ +Import("env") + +myenv = env.Clone() +myenv.MergeFlags(myenv["SWIFTEN_FLAGS"]) +myenv.MergeFlags(myenv["LIBIDN_FLAGS"]) +myenv.MergeFlags(myenv["BOOST_FLAGS"]) +myenv.MergeFlags(myenv["ZLIB_FLAGS"]) +myenv.MergeFlags(myenv["OPENSSL_FLAGS"]) +myenv.MergeFlags(myenv.get("SQLITE_FLAGS", {})) +myenv.MergeFlags(myenv.get("LIBXML_FLAGS", "")) +myenv.MergeFlags(myenv.get("EXPAT_FLAGS", "")) +myenv.MergeFlags(myenv["PLATFORM_FLAGS"]) +tester = myenv.Program("SendFile", ["SendFile.cpp"]) diff --git a/Swiften/Examples/SendFile/SendFile.cpp b/Swiften/Examples/SendFile/SendFile.cpp new file mode 100644 index 0000000..da0b2fe --- /dev/null +++ b/Swiften/Examples/SendFile/SendFile.cpp @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <boost/bind.hpp> +#include <boost/filesystem.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/MainBoostIOServiceThread.h" +#include "Swiften/FileTransfer/OutgoingFileTransfer.h" +#include "Swiften/FileTransfer/FileReadBytestream.h" +#include "Swiften/FileTransfer/SOCKS5BytestreamServer.h" +#include "Swiften/Network/BoostConnectionServer.h" +#include "Swiften/Network/BoostIOServiceThread.h" + +using namespace Swift; + +SimpleEventLoop eventLoop; +int exitCode = 2; + +class FileSender { + public: + FileSender(const JID& jid, const String& password, const JID& recipient, const boost::filesystem::path& file, int port) : jid(jid), password(password), recipient(recipient), file(file), transfer(NULL) { + connectionServer = BoostConnectionServer::create(port, &MainBoostIOServiceThread::getInstance().getIOService()); + socksBytestreamServer = new SOCKS5BytestreamServer(connectionServer); + + client = new Swift::Client(jid, password); + client->onConnected.connect(boost::bind(&FileSender::handleConnected, this)); + client->onError.connect(boost::bind(&FileSender::handleError, this, _1)); + //tracer = new ClientXMLTracer(client); + } + + ~FileSender() { + //delete tracer; + client->onError.disconnect(boost::bind(&FileSender::handleError, this, _1)); + client->onConnected.disconnect(boost::bind(&FileSender::handleConnected, this)); + delete client; + delete socksBytestreamServer; + } + + void start() { + connectionServer->start(); + socksBytestreamServer->start(); + client->connect(); + } + + void stop() { + if (transfer) { + transfer->stop(); + } + client->disconnect(); + socksBytestreamServer->stop(); + connectionServer->stop(); + } + + private: + void handleConnected() { + client->sendPresence(Presence::create()); + transfer = new OutgoingFileTransfer("myid", client->getJID(), recipient, file.filename(), boost::filesystem::file_size(file), "A file", boost::shared_ptr<FileReadBytestream>(new FileReadBytestream(file)), client->getIQRouter(), socksBytestreamServer); + transfer->onFinished.connect(boost::bind(&FileSender::handleFileTransferFinished, this, _1)); + transfer->start(); + } + + void handleError(const 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; + stop(); + eventLoop.stop(); + } + + private: + BoostConnectionServer::ref connectionServer; + SOCKS5BytestreamServer* socksBytestreamServer; + JID jid; + String password; + JID recipient; + boost::filesystem::path file; + Client* client; + ClientXMLTracer* tracer; + OutgoingFileTransfer* transfer; +}; + + +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]); + FileSender fileSender(sender, String(argv[2]), recipient, boost::filesystem::path(argv[4]), 8888); + 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; +} |