Changeset - a87a0484add4
[Not reviewed]
0 5 0
Jan Kaluza - 14 years ago 2011-08-22 15:44:47
hanzz.k@gmail.com
VCard timeout
5 files changed with 34 insertions and 6 deletions:
0 comments (0 inline, 0 general)
include/transport/vcardresponder.h
Show inline comments
 
@@ -33,7 +33,7 @@ class User;
 

	
 
class VCardResponder : public Swift::Responder<Swift::VCard> {
 
	public:
 
		VCardResponder(Swift::IQRouter *router, UserManager *userManager);
 
		VCardResponder(Swift::IQRouter *router, Swift::NetworkFactories *factories, UserManager *userManager);
 
		~VCardResponder();
 

	
 
		void sendVCard(unsigned int id, boost::shared_ptr<Swift::VCard> vcard);
 
@@ -41,11 +41,14 @@ class VCardResponder : public Swift::Responder<Swift::VCard> {
 
		boost::signal<void (User *, const std::string &name, unsigned int id)> onVCardRequired;
 
		boost::signal<void (User *, boost::shared_ptr<Swift::VCard> vcard)> onVCardUpdated;
 

	
 
		void collectTimeouted();
 

	
 
	private:
 
		struct VCardData {
 
			Swift::JID from;
 
			Swift::JID to;
 
			std::string id;
 
			time_t received;
 
		};
 

	
 
		virtual bool handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::VCard> payload);
 
@@ -53,6 +56,7 @@ class VCardResponder : public Swift::Responder<Swift::VCard> {
 
		UserManager *m_userManager;
 
		std::map<unsigned int, VCardData> m_queries;
 
		unsigned int m_id;
 
		Swift::Timer::ref m_collectTimer;
 
};
 

	
 
}
 
\ No newline at end of file
spectrum/src/sample.cfg
Show inline comments
 
@@ -20,7 +20,7 @@ protocol=any
 

	
 
[backend]
 
#default_avatar=catmelonhead.jpg
 
no_vcard_fetch=true
 
#no_vcard_fetch=true
 

	
 
[logging]
 
#config=logging.cfg # log4cxx/log4j logging configuration file
src/networkpluginserver.cpp
Show inline comments
 
@@ -186,7 +186,7 @@ NetworkPluginServer::NetworkPluginServer(Component *component, Config *config, U
 
	m_collectTimer->onTick.connect(boost::bind(&NetworkPluginServer::collectBackend, this));
 
	m_collectTimer->start();
 

	
 
	m_vcardResponder = new VCardResponder(component->getIQRouter(), userManager);
 
	m_vcardResponder = new VCardResponder(component->getIQRouter(), component->getNetworkFactories(), userManager);
 
	m_vcardResponder->onVCardRequired.connect(boost::bind(&NetworkPluginServer::handleVCardRequired, this, _1, _2, _3));
 
	m_vcardResponder->onVCardUpdated.connect(boost::bind(&NetworkPluginServer::handleVCardUpdated, this, _1, _2));
 
	m_vcardResponder->start();
src/usermanager.cpp
Show inline comments
 
@@ -31,7 +31,7 @@
 
#include "Swiften/Server/ServerStanzaChannel.h"
 
#include "Swiften/Elements/StreamError.h"
 
#include "malloc.h"
 
// #include "valgrind/memcheck.h"
 
#include "valgrind/memcheck.h"
 

	
 
using namespace log4cxx;
 

	
 
@@ -100,7 +100,7 @@ void UserManager::removeUser(User *user) {
 
	onUserDestroyed(user);
 
	delete user;
 
	malloc_trim(0);
 
// 	VALGRIND_DO_LEAK_CHECK;
 
	VALGRIND_DO_LEAK_CHECK;
 
}
 

	
 
int UserManager::getUserCount() {
src/vcardresponder.cpp
Show inline comments
 
@@ -39,9 +39,12 @@ namespace Transport {
 

	
 
static LoggerPtr logger = Logger::getLogger("VCardResponder");
 

	
 
VCardResponder::VCardResponder(Swift::IQRouter *router, UserManager *userManager) : Swift::Responder<VCard>(router) {
 
VCardResponder::VCardResponder(Swift::IQRouter *router, Swift::NetworkFactories *factories, UserManager *userManager) : Swift::Responder<VCard>(router) {
 
	m_id = 0;
 
	m_userManager = userManager;
 
	m_collectTimer = factories->getTimerFactory()->createTimer(20);
 
	m_collectTimer->onTick.connect(boost::bind(&VCardResponder::collectTimeouted, this));
 
	m_collectTimer->start();
 
}
 

	
 
VCardResponder::~VCardResponder() {
 
@@ -59,6 +62,26 @@ void VCardResponder::sendVCard(unsigned int id, boost::shared_ptr<Swift::VCard>
 
	m_queries.erase(id);
 
}
 

	
 
void VCardResponder::collectTimeouted() {
 
	time_t now = time(NULL);
 

	
 
	std::vector<unsigned int> candidates;
 
	for(std::map<unsigned int, VCardData>::iterator it = m_queries.begin(); it != m_queries.end(); it++) {
 
		if (now - (*it).second.received > 40) {
 
			candidates.push_back((*it).first);
 
		}
 
	}
 

	
 
	if (candidates.size() != 0) {
 
		LOG4CXX_INFO(logger, "Removing " << candidates.size() << " timeouted VCard requests");
 
	}
 

	
 
	BOOST_FOREACH(unsigned int id, candidates) {
 
		sendVCard(id, boost::shared_ptr<Swift::VCard>(new Swift::VCard()));
 
	}
 
	m_collectTimer->start();
 
}
 

	
 
bool VCardResponder::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::VCard> payload) {
 
	// Get means we're in server mode and user wants to fetch his roster.
 
	// For now we send empty reponse, but TODO: Get buddies from database and send proper stored roster.
 
@@ -85,6 +108,7 @@ bool VCardResponder::handleGetRequest(const Swift::JID& from, const Swift::JID&
 
	m_queries[m_id].from = from;
 
	m_queries[m_id].to = to_;
 
	m_queries[m_id].id = id; 
 
	m_queries[m_id].received = time(NULL);
 
	onVCardRequired(user, name, m_id++);
 
	return true;
 
}
0 comments (0 inline, 0 general)