Changeset - 03c7d96eab83
[Not reviewed]
0 2 2
HanzZ - 14 years ago 2011-10-03 10:59:29
hanzz.k@gmail.com
Created MemoryReadByteStream
4 files changed with 123 insertions and 44 deletions:
0 comments (0 inline, 0 general)
include/transport/memoryreadbytestream.h
Show inline comments
 
new file 100644
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#pragma once
 

	
 
#include <string>
 
#include <map>
 
#include "Swiften/Swiften.h"
 

	
 
namespace Transport {
 

	
 
class MemoryReadBytestream : public Swift::ReadBytestream {
 
	public:
 
		MemoryReadBytestream();
 
		virtual ~MemoryReadBytestream();
 

	
 
		unsigned long appendData(const std::string &data);
 

	
 
		virtual boost::shared_ptr<std::vector<unsigned char> > read(size_t size);
 

	
 
		bool isFinished() const;
 

	
 
		boost::signal<void ()> onDataNeeded;
 

	
 
	private:
 
		bool m_finished;
 
		std::string m_data;
 
		bool neededData;
 
};
 

	
 
}
include/transport/networkpluginserver.h
Show inline comments
 
@@ -134,48 +134,7 @@ class NetworkPluginServer {
 
		std::list<User *> m_waitingUsers;
 
		bool m_isNextLongRun;
 
		std::map<unsigned long, FileTransferManager::Transfer> m_filetransfers;
 
		FileTransferManager *m_ftManager;
 
};
 

	
 
class DummyReadBytestream : public Swift::ReadBytestream {
 
	public:
 
		DummyReadBytestream(NetworkPluginServer::Backend *b, unsigned long ftid) {neededData = false; m_finished = false; this->b = b; this->ftid = ftid;}
 
		virtual ~DummyReadBytestream() {}
 
		unsigned long appendData(const std::string &data) {
 
			m_data += data;
 
			onDataAvailable();
 
			neededData = false;
 
			return m_data.size();
 
		}
 

	
 
		virtual boost::shared_ptr<std::vector<unsigned char> > read(size_t size) {
 
			if (m_data.empty()) {
 
				return boost::shared_ptr<std::vector<unsigned char> >(new std::vector<unsigned char>());
 
			}
 

	
 
			if (m_data.size() < size) {
 
				boost::shared_ptr<std::vector<unsigned char> > ptr(new std::vector<unsigned char>(m_data.begin(), m_data.end()));
 
				m_data.clear();
 
				onDataNeeded(b, ftid);
 
				return ptr;
 
			}
 
			boost::shared_ptr<std::vector<unsigned char> > ptr(new std::vector<unsigned char>(m_data.begin(), m_data.begin() + size));
 
			m_data.erase(m_data.begin(), m_data.begin() + size);
 
			if (m_data.size() < 500000 && !neededData) {
 
				neededData = true;
 
				onDataNeeded(b, ftid);
 
			}
 
			return ptr;
 
		}
 
		boost::signal<void (NetworkPluginServer::Backend *b, unsigned long ftid)> onDataNeeded;
 
		virtual bool isFinished() const { return m_finished; }
 

	
 
	private:
 
		bool m_finished;
 
		NetworkPluginServer::Backend *b;
 
		unsigned long ftid;
 
		std::string m_data;
 
		bool neededData;
 
};
 

	
 
}
src/memoryreadbytestream.cpp
Show inline comments
 
new file 100644
 
/**
 
 * libtransport -- C++ library for easy XMPP Transports development
 
 *
 
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 
 *
 
 * This program is free software; you can redistribute it and/or modify
 
 * it under the terms of the GNU General Public License as published by
 
 * the Free Software Foundation; either version 2 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * This program is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU General Public License
 
 * along with this program; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 
 */
 

	
 
#include "transport/memoryreadbytestream.h"
 
#include "log4cxx/logger.h"
 
#include "memoryusage.h"
 
#include <boost/foreach.hpp>
 

	
 
using namespace log4cxx;
 

	
 
namespace Transport {
 
	
 
MemoryReadBytestream::MemoryReadBytestream() {
 
	neededData = false;
 
	m_finished = false;
 
}
 

	
 
MemoryReadBytestream::~MemoryReadBytestream() {
 
	
 
}
 

	
 
unsigned long MemoryReadBytestream::appendData(const std::string &data) {
 
	m_data += data;
 
	onDataAvailable();
 
	neededData = false;
 
	return m_data.size();
 
}
 

	
 
boost::shared_ptr<std::vector<unsigned char> > MemoryReadBytestream::read(size_t size) {
 
	if (m_data.empty()) {
 
		onDataNeeded();
 
		return boost::shared_ptr<std::vector<unsigned char> >(new std::vector<unsigned char>());
 
	}
 

	
 
	if (m_data.size() < size) {
 
		boost::shared_ptr<std::vector<unsigned char> > ptr(new std::vector<unsigned char>(m_data.begin(), m_data.end()));
 
		m_data.clear();
 
		onDataNeeded();
 
		return ptr;
 
	}
 
	boost::shared_ptr<std::vector<unsigned char> > ptr(new std::vector<unsigned char>(m_data.begin(), m_data.begin() + size));
 
	m_data.erase(m_data.begin(), m_data.begin() + size);
 
	if (m_data.size() < 500000 && !neededData) {
 
		neededData = true;
 
		onDataNeeded();
 
	}
 
	return ptr;
 
}
 

	
 
bool MemoryReadBytestream::isFinished() const {
 
	return m_finished;
 
}
 

	
 
}
src/networkpluginserver.cpp
Show inline comments
 
@@ -27,12 +27,13 @@
 
#include "transport/conversationmanager.h"
 
#include "transport/localbuddy.h"
 
#include "transport/config.h"
 
#include "transport/conversation.h"
 
#include "transport/vcardresponder.h"
 
#include "transport/rosterresponder.h"
 
#include "transport/memoryreadbytestream.h"
 
#include "blockresponder.h"
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Server/ServerStanzaChannel.h"
 
#include "Swiften/Elements/StreamError.h"
 
#include "Swiften/Network/BoostConnectionServer.h"
 
#include "Swiften/Elements/AttentionPayload.h"
 
@@ -597,14 +598,14 @@ void NetworkPluginServer::handleFTStartPayload(const std::string &data) {
 

	
 
	Swift::StreamInitiationFileInfo fileInfo;
 
	fileInfo.setSize(payload.size());
 
	fileInfo.setName(payload.filename());
 

	
 
	Backend *c = (Backend *) user->getData();
 
	boost::shared_ptr<DummyReadBytestream> bytestream(new DummyReadBytestream(c, bytestream_id + 1));
 
	bytestream->onDataNeeded.connect(boost::bind(&NetworkPluginServer::handleFTDataNeeded, this, _1, _2));
 
	boost::shared_ptr<MemoryReadBytestream> bytestream(new MemoryReadBytestream());
 
	bytestream->onDataNeeded.connect(boost::bind(&NetworkPluginServer::handleFTDataNeeded, this, c, bytestream_id + 1));
 

	
 
	LOG4CXX_INFO(logger, "jid=" << buddy->getJID());
 

	
 
	FileTransferManager::Transfer transfer = m_ftManager->sendFile(user, buddy, bytestream, fileInfo);
 
	if (!transfer.ft) {
 
		handleFTRejected(user, payload.buddyname(), payload.filename(), payload.size());
 
@@ -625,13 +626,13 @@ void NetworkPluginServer::handleFTDataPayload(Backend *b, const std::string &dat
 

	
 
// 	User *user = m_userManager->getUser(payload.username());
 
// 	if (!user)
 
// 		return;
 

	
 
	FileTransferManager::Transfer &transfer = m_filetransfers[payload.ftid()];
 
	DummyReadBytestream *bytestream = (DummyReadBytestream *) transfer.readByteStream.get();
 
	MemoryReadBytestream *bytestream = (MemoryReadBytestream *) transfer.readByteStream.get();
 

	
 
	if (bytestream->appendData(payload.data()) > 5000000) {
 
		pbnetwork::FileTransferData f;
 
		f.set_ftid(payload.ftid());
 
		f.set_data("");
 

	
0 comments (0 inline, 0 general)