Changeset - 00475b428add
[Not reviewed]
0 1 0
Jan Kaluza - 13 years ago 2012-12-11 11:42:31
hanzz.k@gmail.com
Try to find out existing adhoc session only when session id is set in IQ
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/adhocmanager.cpp
Show inline comments
 
@@ -71,97 +71,97 @@ void AdHocManager::addAdHocCommand(AdHocCommandFactory *factory) {
 
		LOG4CXX_ERROR(logger, "Command with node " << factory->getNode() << " is already registered. Ignoring this attempt.");
 
		return;
 
	}
 

	
 
	m_factories[factory->getNode()] = factory;
 
	m_discoItemsResponder->addAdHocCommand(factory->getNode(), factory->getName());
 
}
 

	
 
void AdHocManager::removeOldSessions() {
 
	unsigned long removedCommands = 0;
 
	time_t now = time(NULL);
 

	
 
	std::vector<std::string> toRemove;
 
	for (SessionsMap::iterator it = m_sessions.begin(); it != m_sessions.end(); it++) {
 
		std::vector<std::string> candidates;
 
		for (CommandsMap::iterator ct = it->second.begin(); ct != it->second.end(); ct++) {
 
			if (now - ct->second->getLastActivity() > 15*60) {
 
				candidates.push_back(it->first);
 
				delete ct->second;
 
				removedCommands++;
 
			}
 
		}
 

	
 
		BOOST_FOREACH(std::string &key, candidates) {
 
			it->second.erase(key);
 
		}
 

	
 
		if (it->second.empty()) {
 
			toRemove.push_back(it->first);
 
		}
 
	}
 

	
 
	BOOST_FOREACH(std::string &key, toRemove) {
 
		m_sessions.erase(key);
 
	}
 

	
 
	if (removedCommands > 0) {
 
		LOG4CXX_INFO(logger, "Removed " << removedCommands << " old commands sessions.");
 
	}
 
}
 

	
 
bool AdHocManager::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::Command> payload) {
 
	return false;
 
}
 

	
 
bool AdHocManager::handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::Command> payload) {
 
	AdHocCommand *command = NULL;
 
	// Try to find AdHocCommand according to 'from' and session_id
 
	if (m_sessions.find(from) != m_sessions.end()) {
 
	if (!payload->getSessionID().empty() && m_sessions.find(from) != m_sessions.end()) {
 
		if (m_sessions[from].find(payload->getSessionID()) != m_sessions[from].end()) {
 
			command = m_sessions[from][payload->getSessionID()];
 
		}
 
		else {
 
			LOG4CXX_ERROR(logger, from.toString() << ": Unknown session id " << payload->getSessionID() << " - ignoring");
 
			sendError(from, id, Swift::ErrorPayload::BadRequest, Swift::ErrorPayload::Modify);
 
			return true;
 
		}
 
	}
 
	// Check if we can create command with this node
 
	else if (m_factories.find(payload->getNode()) != m_factories.end()) {
 
		command = m_factories[payload->getNode()]->createAdHocCommand(m_component, m_userManager, m_storageBackend, from, to);
 
		m_sessions[from][command->getId()] = command;
 
		LOG4CXX_INFO(logger, from.toString() << ": Started new AdHoc command session with node " << payload->getNode());
 
	}
 
	else {
 
		LOG4CXX_INFO(logger, from.toString() << ": Unknown node " << payload->getNode() << ". Can't start AdHoc command session.");
 
		sendError(from, id, Swift::ErrorPayload::BadRequest, Swift::ErrorPayload::Modify);
 
		return true;
 
	}
 

	
 
	if (!command) {
 
		LOG4CXX_ERROR(logger, from.toString() << ": createAdHocCommand for node " << payload->getNode() << " returned NULL pointer");
 
		sendError(from, id, Swift::ErrorPayload::BadRequest, Swift::ErrorPayload::Modify);
 
		return true;
 
	}
 

	
 
	boost::shared_ptr<Swift::Command> response = command->handleRequest(payload);
 
	if (!response) {
 
		LOG4CXX_ERROR(logger, from.toString() << ": handleRequest for node " << payload->getNode() << " returned NULL pointer");
 
		sendError(from, id, Swift::ErrorPayload::BadRequest, Swift::ErrorPayload::Modify);
 
		return true;
 
	}
 

	
 
	response->setSessionID(command->getId());
 

	
 
	sendResponse(from, id, response);
 

	
 
	command->refreshLastActivity();
 

	
 
	// Command completed, so we can remove it now
 
	if (response->getStatus() == Swift::Command::Completed || response->getStatus() == Swift::Command::Canceled) {
 
		m_sessions[from].erase(command->getId());
 
		if (m_sessions[from].empty()) {
 
			m_sessions.erase(from);
 
		}
 
		delete command;
 
	}
0 comments (0 inline, 0 general)