soc.2009.transport: a49058de: Rewritten stats handler

hanzz at soc.pidgin.im hanzz at soc.pidgin.im
Tue Jun 2 07:06:11 EDT 2009


-----------------------------------------------------------------
Revision: a49058de2afabbbb92ca92a33c84d58669d6a8c5
Ancestor: 41b99efae03aa9e02d9e75fb49a23c465a01caa0
Author: hanzz at soc.pidgin.im
Date: 2009-06-02T10:36:00
Branch: im.pidgin.soc.2009.transport
URL: http://d.pidgin.im/viewmtn/revision/info/a49058de2afabbbb92ca92a33c84d58669d6a8c5

Modified files:
        main.cpp statshandler.cpp statshandler.h

ChangeLog: 

Rewritten stats handler

-------------- next part --------------
============================================================
--- main.cpp	a522899b936a6838113941f0d5d24d74bc12c9a7
+++ main.cpp	fbd6a69a1fea5b4d6d8f52d8ab5f7303e7f7a114
@@ -503,10 +503,11 @@ GlooxMessageHandler::GlooxMessageHandler
 	j->registerIqHandler(gatewayHandler,ExtGateway);
 	m_reg = new GlooxRegisterHandler(this);
 	j->registerIqHandler(m_reg,ExtRegistration);
-	m_xping = new GlooxXPingHandler(this);
-	j->registerIqHandler(m_xping,"urn:xmpp:ping");
+	// PING is now implemented in Gloox
+// 	m_xping = new GlooxXPingHandler(this);
+// 	j->registerIqHandler(m_xping,"urn:xmpp:ping");
 	m_stats = new GlooxStatsHandler(this);
-	j->registerIqHandler(m_stats,"http://jabber.org/protocol/stats");
+	j->registerIqHandler(m_stats,ExtStats);
 	m_vcard = new GlooxVCardHandler(this);
 	j->registerIqHandler(m_vcard,"vcard-temp");
 	j->registerPresenceHandler(this);
============================================================
--- statshandler.cpp	fa01863ecf8e59a4812115d0885748b56e75663b
+++ statshandler.cpp	5a58faba0a755da41c14bc4f2872fc70ffe42b0c
@@ -29,10 +29,38 @@
 #include "sql.h"
 #include <sstream>
 
+StatsExtension::StatsExtension() : StanzaExtension( ExtGateway )
+{
+	m_tag = NULL;
+}
+
+StatsExtension::StatsExtension(const Tag *tag) : StanzaExtension( ExtStats )
+{
+	m_tag = tag->clone();
+}
+
+StatsExtension::~StatsExtension()
+{
+	Log().Get("StatsExtension") << "deleting StatsExtension()";
+	delete m_tag;
+}
+
+const std::string& StatsExtension::filterString() const
+{
+	static const std::string filter = "iq/query[@xmlns='http://jabber.org/protocol/stats']";
+	return filter;
+}
+
+Tag* StatsExtension::tag() const
+{
+	return m_tag->clone();
+}
+
 GlooxStatsHandler::GlooxStatsHandler(GlooxMessageHandler *parent) : IqHandler(){
 	p=parent;
 	m_messagesIn = m_messagesOut = 0;
 	m_startTime = time(NULL);
+	p->j->registerStanzaExtension( new StatsExtension() );
 }
 
 GlooxStatsHandler::~GlooxStatsHandler(){
@@ -65,36 +93,39 @@ bool GlooxStatsHandler::handleIq (const 
 		_s.setFrom(from);
 
 		Tag *s = _s.tag();
-		s->setXmlns("http://jabber.org/protocol/stats");
+		Tag *query = new Tag("query");
+		query->setXmlns("http://jabber.org/protocol/stats");
 		Tag *t;
 
 		t = new Tag("stat");
 		t->addAttribute("name","uptime");
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
 		t = new Tag("stat");
 		t->addAttribute("name","users/registered");
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 		
 		t = new Tag("stat");
 		t->addAttribute("name","users/online");
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
 		t = new Tag("stat");
 		t->addAttribute("name","legacy-network-users/online");
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
 		t = new Tag("stat");
 		t->addAttribute("name","legacy-network-users/registered");
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
 		t = new Tag("stat");
 		t->addAttribute("name","messages/in");
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
 		t = new Tag("stat");
 		t->addAttribute("name","messages/out");
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
+		
+		s->addChild(query);
 
 		p->j->send( s );
 
@@ -107,7 +138,9 @@ bool GlooxStatsHandler::handleIq (const 
 		_s.setFrom(from);
 
 		Tag *s = _s.tag();
-		s->setXmlns("http://jabber.org/protocol/stats");
+		Tag *query = new Tag("query");
+		query->setXmlns("http://jabber.org/protocol/stats");
+
 		Tag *t;
 		long registered = p->sql()->getRegisteredUsersCount();
 		long registeredUsers = p->sql()->getRegisteredUsersRosterCount();
@@ -123,45 +156,46 @@ bool GlooxStatsHandler::handleIq (const 
 		t->addAttribute("name","uptime");
 		t->addAttribute("units","seconds");
 		t->addAttribute("value",seconds - m_startTime);
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
 		t = new Tag("stat");
 		t->addAttribute("name","users/registered");
 		t->addAttribute("units","users");
 		t->addAttribute("value",out.str());
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
 		t = new Tag("stat");
 		t->addAttribute("name","users/online");
 		t->addAttribute("units","users");
 		t->addAttribute("value",p->userManager()->userCount());
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
 		t = new Tag("stat");
 		t->addAttribute("name","legacy-network-users/registered");
 		t->addAttribute("units","users");
 		t->addAttribute("value",registeredUsers);
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
 		t = new Tag("stat");
 		t->addAttribute("name","legacy-network-users/online");
 		t->addAttribute("units","users");
 		t->addAttribute("value",users);
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 		
 
 		t = new Tag("stat");
 		t->addAttribute("name","messages/in");
 		t->addAttribute("units","messages");
 		t->addAttribute("value",m_messagesIn);
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
 		t = new Tag("stat");
 		t->addAttribute("name","messages/out");
 		t->addAttribute("units","messages");
 		t->addAttribute("value",m_messagesOut);
-		s->findChild("query")->addChild(t);
+		query->addChild(t);
 
+		s->addChild(query);
 
 		p->j->send(s);
 
============================================================
--- statshandler.h	4ee6ac32e70e19f65fb01a93224c240c0be51025
+++ statshandler.h	f34fc5e58118e81e47e2b3c040c19d46878631b8
@@ -30,6 +30,44 @@ using namespace gloox;
 using namespace gloox;
 
 
+class StatsExtension : public StanzaExtension
+{
+
+public:
+	/**
+	* Constructs a new object from the given Tag.
+	*/
+	StatsExtension();
+	StatsExtension(const Tag *tag);
+
+	/**
+	* Virtual Destructor.
+	*/
+	virtual ~StatsExtension();
+
+	// reimplemented from StanzaExtension
+	virtual const std::string& filterString() const;
+
+	// reimplemented from StanzaExtension
+	virtual StanzaExtension* newInstance( const Tag* tag ) const
+	{
+	return new StatsExtension(tag);
+	}
+
+	// reimplemented from StanzaExtension
+	virtual Tag* tag() const;
+
+	// reimplemented from StanzaExtension
+	virtual StanzaExtension* clone() const
+	{
+		return new StatsExtension(m_tag);
+	}
+	
+private:
+	Tag *m_tag;
+
+};
+
 class GlooxStatsHandler : public IqHandler
 {
 	public:


More information about the Commits mailing list