Changeset - 2814db5bd6f3
[Not reviewed]
Merge
0 4 0
HanzZ - 14 years ago 2011-11-09 22:59:45
hanzz.k@gmail.com
Merge branch 'master' of github.com:hanzz/libtransport
3 files changed with 65 insertions and 0 deletions:
0 comments (0 inline, 0 general)
include/transport/rostermanager.h
Show inline comments
 
@@ -46,12 +46,13 @@ class AddressedRosterRequest : public Swift::GenericRequest<Swift::RosterPayload
 
		}
 
};
 

	
 
/// Manages roster of one XMPP user.
 
class RosterManager {
 
	public:
 
		typedef std::map<std::string, Buddy *, std::less<std::string>, boost::pool_allocator< std::pair<std::string, Buddy *> > > BuddiesMap;
 
		/// Creates new RosterManager.
 
		/// \param user User associated with this RosterManager.
 
		/// \param component Transport instance associated with this roster.
 
		RosterManager(User *user, Component *component);
 

	
 
		/// Destructor.
 
@@ -77,12 +78,16 @@ class RosterManager {
 
		Swift::RosterPayload::ref generateRosterPayload();
 

	
 
		/// Returns user associated with this roster.
 
		/// \return User
 
		User *getUser() { return m_user; }
 

	
 
		const BuddiesMap &getBuddies() {
 
			return m_buddies;
 
		}
 

	
 
		bool isRemoteRosterSupported() {
 
			return m_supportRemoteRoster;
 
		}
 

	
 
		/// Called when new Buddy is added to this roster.
 
		/// \param buddy newly added Buddy
include/transport/statsresponder.h
Show inline comments
 
@@ -38,12 +38,14 @@ class StatsResponder : public Swift::Responder<Swift::StatsPayload> {
 
		~StatsResponder();
 

	
 
	private:
 
		virtual bool handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::StatsPayload> payload);
 
		virtual bool handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::StatsPayload> payload);
 

	
 
		unsigned long usedMemory();
 

	
 
		Component *m_component;
 
		UserManager *m_userManager;
 
		NetworkPluginServer *m_server;
 
		StorageBackend *m_storageBackend;
 
		time_t m_start;
 
};
src/statsresponder.cpp
Show inline comments
 
@@ -26,12 +26,17 @@
 
#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;
 
@@ -49,23 +54,76 @@ StatsResponder::StatsResponder(Component *component, UserManager *userManager, N
 
}
 

	
 
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++) {
 
			const RosterManager::BuddiesMap &buddies = (*it).second->getRosterManager()->getBuddies();
 
			contactsTotal += buddies.size();
 
			for(RosterManager::BuddiesMap::const_iterator it = buddies.begin(); it != buddies.end(); it++) {
 
				if (!(*it).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;
0 comments (0 inline, 0 general)