Files
@ 098a8d4c944c
Branch filter:
Location: libtransport.git/src/adhocmanager.cpp - annotation
098a8d4c944c
6.8 KiB
text/x-c++hdr
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 8bb71e514842 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 8bb71e514842 851ef182607a 851ef182607a 8bb71e514842 8bb71e514842 851ef182607a 851ef182607a 851ef182607a 851ef182607a 0b0e020cdf91 0b0e020cdf91 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 0b0e020cdf91 0b0e020cdf91 0b0e020cdf91 0b0e020cdf91 f7dc91ccb482 f7dc91ccb482 f7dc91ccb482 f7dc91ccb482 0b0e020cdf91 0b0e020cdf91 0b0e020cdf91 0b0e020cdf91 0b0e020cdf91 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 00475b428add 974bf2de0a2c 974bf2de0a2c 974bf2de0a2c 974bf2de0a2c 974bf2de0a2c 974bf2de0a2c 974bf2de0a2c 974bf2de0a2c 851ef182607a 851ef182607a 851ef182607a 8bb71e514842 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 974bf2de0a2c 0b0e020cdf91 0b0e020cdf91 0b0e020cdf91 0b0e020cdf91 0b0e020cdf91 0b0e020cdf91 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a 851ef182607a | /**
* XMPP - libpurple transport
*
* Copyright (C) 2012, Jan Kaluza <hanzz@soc.pidgin.im>
*
* 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/adhocmanager.h"
#include "transport/adhoccommandfactory.h"
#include "transport/discoitemsresponder.h"
#include "transport/conversation.h"
#include "transport/usermanager.h"
#include "transport/buddy.h"
#include "transport/factory.h"
#include "transport/user.h"
#include "transport/logging.h"
#include "transport/storagebackend.h"
namespace Transport {
DEFINE_LOGGER(logger, "AdHocManager");
AdHocManager::AdHocManager(Component *component, DiscoItemsResponder *discoItemsResponder, UserManager *userManager, StorageBackend *storageBackend) : Swift::Responder<Swift::Command>(component->getIQRouter()){
m_component = component;
m_discoItemsResponder = discoItemsResponder;
m_userManager = userManager;
m_storageBackend = storageBackend;
m_collectTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(20);
m_collectTimer->onTick.connect(boost::bind(&AdHocManager::removeOldSessions, this));
m_collectTimer->start();
m_userManager->onUserCreated.connect(boost::bind(&AdHocManager::handleUserCreated, this, _1));
}
AdHocManager::~AdHocManager() {
m_collectTimer->stop();
stop();
}
void AdHocManager::start() {
Swift::Responder<Swift::Command>::start();
}
void AdHocManager::stop() {
Swift::Responder<Swift::Command>::stop();
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++) {
delete ct->second;
}
}
m_sessions.clear();
}
void AdHocManager::handleUserCreated(User *user) {
for (std::map<std::string, AdHocCommandFactory *>::const_iterator it = m_factories.begin(); it != m_factories.end(); it++) {
for (std::map<std::string, std::string>::const_iterator it2 = it->second->getUserSettings().begin(); it2 != it->second->getUserSettings().end(); it2++) {
std::string value = CONFIG_STRING_DEFAULTED(m_component->getConfig(), it->second->getNode() + "." + it2->first, it2->second);
if (m_storageBackend) {
int type = (int) TYPE_BOOLEAN;
m_storageBackend->getUserSetting(user->getUserInfo().id, it2->first, type, value);
}
user->addUserSetting(it2->first, value);
}
}
}
void AdHocManager::addAdHocCommand(AdHocCommandFactory *factory) {
if (m_factories.find(factory->getNode()) != m_factories.end()) {
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 (!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) {
// update userSettings map if user is already connected
User *user = m_userManager->getUser(from.toBare().toString());
if (user) {
handleUserCreated(user);
}
m_sessions[from].erase(command->getId());
if (m_sessions[from].empty()) {
m_sessions.erase(from);
}
delete command;
}
return true;
}
}
|