Files
@ f2d21db482c7
Branch filter:
Location: libtransport.git/src/statsresponder.cpp
f2d21db482c7
4.8 KiB
text/x-c++hdr
Merge branch 'master' of https://github.com/hanzz/libtransport
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 | /**
* XMPP - libpurple transport
*
* Copyright (C) 2009, 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/statsresponder.h"
#include <iostream>
#include <boost/bind.hpp>
#include "Swiften/Queries/IQRouter.h"
#include "transport/BlockPayload.h"
#include "Swiften/Swiften.h"
#include "transport/usermanager.h"
#include "transport/user.h"
#include "transport/buddy.h"
#include "transport/rostermanager.h"
#include "transport/memoryusage.h"
#include "transport/conversationmanager.h"
#include "transport/rostermanager.h"
#include "transport/usermanager.h"
#include "transport/networkpluginserver.h"
#include "log4cxx/logger.h"
using namespace log4cxx;
using namespace Swift;
using namespace boost;
namespace Transport {
static LoggerPtr logger = Logger::getLogger("StatsResponder");
StatsResponder::StatsResponder(Component *component, UserManager *userManager, NetworkPluginServer *server, StorageBackend *storageBackend) : Swift::Responder<StatsPayload>(component->getIQRouter()) {
m_component = component;
m_userManager = userManager;
m_server = server;
m_storageBackend = storageBackend;
m_start = time(0);
}
StatsResponder::~StatsResponder() {
}
unsigned long StatsResponder::usedMemory() {
double shared = 0;
double rss = 0;
#ifndef WIN32
process_mem_usage(shared, rss);
#endif
rss -= shared;
const std::list <NetworkPluginServer::Backend *> &backends = m_server->getBackends();
BOOST_FOREACH(NetworkPluginServer::Backend * backend, backends) {
rss += backend->res - backend->shared;
}
return (unsigned long) rss;
}
bool StatsResponder::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<StatsPayload> stats) {
boost::shared_ptr<StatsPayload> response(new StatsPayload());
if (stats->getItems().empty()) {
response->addItem(StatsPayload::Item("uptime"));
response->addItem(StatsPayload::Item("users/online"));
response->addItem(StatsPayload::Item("contacts/online"));
response->addItem(StatsPayload::Item("contacts/total"));
response->addItem(StatsPayload::Item("backends"));
response->addItem(StatsPayload::Item("memory-usage"));
}
else {
unsigned long contactsOnline = 0;
unsigned long contactsTotal = 0;
Swift::StatusShow s;
std::string statusMessage;
for (std::map<std::string, User *>::const_iterator it = m_userManager->getUsers().begin(); it != m_userManager->getUsers().end(); it++) {
if (!(*it).second) {
continue;
}
const RosterManager::BuddiesMap &buddies = (*it).second->getRosterManager()->getBuddies();
contactsTotal += buddies.size();
for(RosterManager::BuddiesMap::const_iterator bt = buddies.begin(); bt != buddies.end(); bt++) {
if (!(*bt).second) {
continue;
}
if (!(*bt).second->getStatus(s, statusMessage))
continue;
if (s.getType() != Swift::StatusShow::None) {
contactsOnline++;
}
}
}
BOOST_FOREACH(const StatsPayload::Item &item, stats->getItems()) {
if (item.getName() == "uptime") {
response->addItem(StatsPayload::Item("uptime", "seconds", boost::lexical_cast<std::string>(time(0) - m_start)));
}
else if (item.getName() == "users/online") {
response->addItem(StatsPayload::Item("users/online", "users", boost::lexical_cast<std::string>(m_userManager->getUserCount())));
}
else if (item.getName() == "backends") {
response->addItem(StatsPayload::Item("backends", "backends", boost::lexical_cast<std::string>(m_server->getBackendCount())));
}
else if (item.getName() == "memory-usage") {
response->addItem(StatsPayload::Item("memory-usage", "KB", boost::lexical_cast<std::string>(usedMemory())));
}
else if (item.getName() == "contacts/online") {
response->addItem(StatsPayload::Item("contacts/online", "contacts", boost::lexical_cast<std::string>(contactsOnline)));
}
else if (item.getName() == "contacts/total") {
response->addItem(StatsPayload::Item("contacts/total", "contacts", boost::lexical_cast<std::string>(contactsTotal)));
}
}
}
sendResponse(from, id, response);
return true;
}
bool StatsResponder::handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<StatsPayload> stats) {
return false;
}
}
|