soc.2009.transport: 553e69c3: Small rewrite of reconnecting system and...

hanzz at soc.pidgin.im hanzz at soc.pidgin.im
Fri Apr 24 17:50:45 EDT 2009


-----------------------------------------------------------------
Revision: 553e69c3dd3b766d79d4519f283c59f06a85f43a
Ancestor: 578765420f7b57f40bf3c3397c3ec7a565e1edb4
Author: hanzz at soc.pidgin.im
Date: 2009-04-24T21:48:37
Branch: im.pidgin.soc.2009.transport
URL: http://d.pidgin.im/viewmtn/revision/info/553e69c3dd3b766d79d4519f283c59f06a85f43a

Modified files:
        main.cpp main.h receivefile.cpp registerhandler.cpp user.cpp
        user.h vcardhandler.cpp

ChangeLog: 

Small rewrite of reconnecting system and more clean code in User class.

-------------- next part --------------
============================================================
--- main.cpp	90d8854f1f8552f9fa479138a171c2c1245d128e
+++ main.cpp	37b91d3fb250f912efc41c14b6144aedc1dd7ba3
@@ -354,7 +354,7 @@ static gboolean connectUser(gpointer dat
 static gboolean connectUser(gpointer data){
 	std::string name((char*)data);
 	User *user = GlooxMessageHandler::instance()->userManager()->getUserByJID(name);
-	if (user && user->readyForConnect() && !user->connected){
+	if (user && user->readyForConnect() && !user->isConnected()){
 		user->connect();
 	}
 	g_free(data);
@@ -472,21 +472,6 @@ void GlooxMessageHandler::onSessionCreat
 	Log().Get("gloox") << "sessionCreateError";
 }
 
-// depracted.... TODO: remove me, I'm not used...
-void GlooxMessageHandler::purpleConnectionError(PurpleConnection *gc){
-	PurpleAccount *account = purple_connection_get_account(gc);
-	User *user = userManager()->getUserByAccount(account);
-	std::cout << "disconnect because of error\n";
-	if (user!=NULL){
-		std::cout << "*** "<< user->jid() <<": disconnected from legacy network because of error\n";
-		if (user->connected==true){
-			user->connected=false;
-		}
-		
-		m_userManager->removeUser(user);
-	}
-}
-
 void GlooxMessageHandler::purpleConnectionError(PurpleConnection *gc,PurpleConnectionError reason,const char *text){
 	PurpleAccount *account = purple_connection_get_account(gc);
 	User *user = userManager()->getUserByAccount(account);
@@ -502,29 +487,26 @@ void GlooxMessageHandler::purpleConnecti
 				s->addAttribute("from",jid());
 				j->send(s);
 			}
-			if (user->connected==true){
-				user->connected=false;
-			}
+// 			if (user->isConnected()==true){
+// 				user->isConnected()=false;
+// 			}
 			m_userManager->removeUserTimer(user);
 		}
 		else{
-			if (user->reconnectCount==1){
+			if (user->reconnectCount()==1){
 				if (text){
 					Stanza *s = Stanza::createMessageStanza(user->jid(), (std::string)text);
 					std::string from;
 					s->addAttribute("from",jid());
 					j->send(s);
 				}
-				if (user->connected==true){
-					user->connected=false;
-				}
+// 				if (user->isConnected()==true){
+// 					user->isConnected()=false;
+// 				}
 				m_userManager->removeUserTimer(user);
 			}
 			else{
-				if (user->connected==true){
-					user->connected=false;
-				}
-				user->reconnectCount+=1;
+
 				g_timeout_add(5000,&reconnect,g_strdup(user->jid().c_str()));
 			}
 		}
@@ -560,8 +542,7 @@ void GlooxMessageHandler::signedOn(Purpl
 	User *user = userManager()->getUserByAccount(account);
 	if (user!=NULL){
 		Log().Get(user->jid()) << "logged in to legacy network";
-		user->connected=true;
-		user->reconnectCount=0;
+		user->connected();
 	}
 }
 
@@ -585,7 +566,7 @@ void * GlooxMessageHandler::purpleAuthor
 		return NULL;
 	User *user = userManager()->getUserByAccount(account);
 	if (user!=NULL){
-		if (user->connected){
+		if (user->isConnected()){
 			user->purpleAuthorizeReceived(account,remote_user,id,alias,message,on_list,authorize_cb,deny_cb,user_data);
 			authData *data = new authData;
 			data->account = account;
@@ -699,7 +680,7 @@ void GlooxMessageHandler::purpleFileRece
 	std::string basename(g_path_get_basename(purple_xfer_get_local_filename(xfer)));
 	User *user = userManager()->getUserByAccount(purple_xfer_get_account(xfer));
 	if (user!=NULL){
-		if (user->connected){
+		if (user->isConnected()){
 			Log().Get(user->jid()) << "Trying to send file " << filename;
 			if(user->hasFeature(GLOOX_FEATURE_FILETRANSFER)){
 				if (user->isVIP()){
@@ -743,7 +724,7 @@ void GlooxMessageHandler::purpleMessageR
 	m_stats->messageFromLegacy();
 	User *user = userManager()->getUserByAccount(account);
 	if (user){
-		if (user->connected){
+		if (user->isConnected()){
 			user->purpleMessageReceived(account,name,msg,conv,flags);
 		}
 		else {
@@ -904,15 +885,15 @@ void GlooxMessageHandler::handlePresence
 		user->receivedPresence(stanza);
 	}
 	if(stanza->to().username() == "" && user!=NULL){
-		if(stanza->presence() == PresenceUnavailable && user->connected==true && user->resources().empty()) {
+		if(stanza->presence() == PresenceUnavailable && user->isConnected()==true && user->resources().empty()) {
 			Log().Get(stanza->from().full()) << "Logging out";
 			m_userManager->removeUser(user);
 		}
-		else if (stanza->presence() == PresenceUnavailable && user->connected==false && int(time(NULL))>int(user->connectionStart)+10 && user->resources().empty()){
+		else if (stanza->presence() == PresenceUnavailable && user->isConnected()==false && int(time(NULL))>int(user->connectionStart())+10 && user->resources().empty()){
 			Log().Get(stanza->from().full()) << "Logging out, but he's not connected...";
 			m_userManager->removeUser(user);
 		}
-		else if (stanza->presence() == PresenceUnavailable && user->connected==false){
+		else if (stanza->presence() == PresenceUnavailable && user->isConnected()==false){
 			Log().Get(stanza->from().full()) << "Can't logout because we're connecting now...";
 		}
 	}
@@ -952,7 +933,7 @@ void GlooxMessageHandler::handleMessage(
 void GlooxMessageHandler::handleMessage( Stanza* stanza, MessageSession* session = 0 ){
 	User *user = userManager()->getUserByJID(stanza->from().bare());
 	if (user!=NULL){
-		if (user->connected){
+		if (user->isConnected()){
 			Tag *chatstates = stanza->findChildWithAttrib("xmlns","http://jabber.org/protocol/chatstates");
 			if (chatstates!=NULL){
 				user->receivedChatState(stanza->to().username(),chatstates->name());
============================================================
--- main.h	bb85c6def1be97732b45930eadee2431f37785dc
+++ main.h	9bd7b3ba7bf94444a044850327ead3c5898b65e7
@@ -136,7 +136,6 @@ public:
 	
 	// Purple related
 	void purpleBuddyChanged(PurpleBuddy* buddy);
-	void purpleConnectionError(PurpleConnection *gc);
 	void purpleConnectionError(PurpleConnection *gc,PurpleConnectionError reason,const char *text);
 	void purpleMessageReceived(PurpleAccount* account,char * name,char *msg,PurpleConversation *conv,PurpleMessageFlags flags);
 	void * purpleAuthorizeReceived(PurpleAccount *account,const char *remote_user,const char *id,const char *alias,const char *message,gboolean on_list,PurpleAccountRequestAuthorizationCb authorize_cb,PurpleAccountRequestAuthorizationCb deny_cb,void *user_data);
============================================================
--- receivefile.cpp	862e89c33bd0e9c7cc33df0e3725184918560a0c
+++ receivefile.cpp	4b2b3783537a315f29a847cf15b5655bda3d2560
@@ -35,7 +35,7 @@ static gboolean transferFinished(gpointe
 				if (user){
 					Log().Get(user->jid()) << "trying to send file "<< (*u).second.filename <<" to " << (*u).second.from.username();
 					if (user->account()){
-						if (user->connected){
+						if (user->isConnected()){
 							if (user->isVIP()){
 								Log().Get(user->jid()) << "sending file";
 								serv_send_file(purple_account_get_connection(user->account()),(*u).second.from.username().c_str(),(*u).second.filename.c_str());
============================================================
--- registerhandler.cpp	dcdf63f699417863289d82814e4705a143ad72c0
+++ registerhandler.cpp	eeff68c0c70da91fec0d17fd45fc78d8c2f0e9ea
@@ -83,9 +83,9 @@ bool GlooxRegisterHandler::handleIq (Sta
 		if(query->hasChild( "remove" )) {
 			std::cout << "* removing user from database and disconnecting from legacy network\n";
 			if (user!=NULL){
-				if (user->connected==true){
+				if (user->isConnected()==true){
 					purple_account_disconnect(user->account());
-					user->connected=false;
+					user->disconnected();
 				}
 			}
 			// TODO: uncomment this line when jabbim will know rosterx
============================================================
--- user.cpp	e9dce38dca438c72ae55c613a03b78d1db3f62e5
+++ user.cpp	d8cb9c175da4cc6ac88e188ed5d6f95c8ccfe17c
@@ -34,21 +34,21 @@ User::User(GlooxMessageHandler *parent, 
 }
 
 User::User(GlooxMessageHandler *parent, const std::string &jid, const std::string &username, const std::string &password){
-	p=parent;
+	p = parent;
 	m_jid = jid;
 	m_username = username;
 	m_password = password;
-	this->connected=false;
+	m_connected = false;
 	m_roster = p->sql()->getRosterByJid(m_jid);
 	m_vip = p->sql()->isVIP(m_jid);
 	m_syncTimer = 0;
 	m_readyForConnect = false;
 	m_rosterXCalled = false;
-	m_account=NULL;
-	this->connectionStart = time(NULL);
+	m_account = NULL;
+	m_connectionStart = time(NULL);
 	m_subscribeLastCount = -1;
-	this->reconnectCount=0;
-	this->features=6; // TODO: I can't be hardcoded
+	m_reconnectCount = 0;
+	this->features = 6; // TODO: I can't be hardcoded
 }
 
 bool User::syncCallback() {
@@ -292,7 +292,7 @@ void User::purpleReauthorizeBuddy(Purple
  * Re-requests authorization for buddy if we can do it (if buddy is not authorized).
  */
 void User::purpleReauthorizeBuddy(PurpleBuddy *buddy){
-	if (!this->connected)
+	if (!m_connected)
 		return;
 	if (!buddy)
 		return;
@@ -545,7 +545,7 @@ void User::connect(){
 
 		purple_accounts_add(m_account);
 	}
-	this->connectionStart = time(NULL);
+	m_connectionStart = time(NULL);
 	m_readyForConnect = false;
 	purple_account_set_string(m_account,"bind",std::string(m_bindIP).c_str());
 	purple_account_set_string(m_account,"lastUsedJid",std::string(m_jid +"/"+m_resource).c_str());
@@ -564,11 +564,27 @@ void User::connect(){
 }
 
 /*
+ * called when we are disconnected from legacy network
+ */
+void User::disconnected() {
+	m_connected = false;
+	m_reconnectCount += 1;
+}
+
+/*
+ * called when we are disconnected from legacy network
+ */
+void User::connected() {
+	m_connected = true;
+	m_reconnectCount = 0;
+}
+
+/*
  * Received jabber presence...
  */
 void User::receivedPresence(Stanza *stanza){
 	// we're connected
-	if (this->connected){
+	if (m_connected){
 	
 		// respond to probe presence
 		if (stanza->subtype() == StanzaPresenceProbe && stanza->to().username()!=""){
@@ -710,13 +726,13 @@ void User::receivedPresence(Stanza *stan
 		if(stanza->presence() == PresenceUnavailable) {
 			// disconnect from legacy network if we are connected
 			std::map<std::string,int> ::iterator iter = m_resources.begin();
-			if ((this->connected==false && int(time(NULL))>int(this->connectionStart)+10) || this->connected==true){
+			if ((m_connected==false && int(time(NULL))>int(m_connectionStart)+10) || m_connected==true){
 				iter = m_resources.find(stanza->from().resource());
 				if(iter != m_resources.end()){
 					m_resources.erase(stanza->from().resource());
 				}
 			}
-			if (this->connected){
+			if (m_connected){
 				if (m_resources.empty()){
 					Log().Get(m_jid) << "disconecting";
 					purple_account_disconnect(m_account);
@@ -726,10 +742,10 @@ void User::receivedPresence(Stanza *stan
 					iter = m_resources.begin();
 					m_resource=(*iter).first;
 				}
-// 				this->connected=false;
+// 				m_connected=false;
 			}
 			else {
-				if (!m_resources.empty() && int(time(NULL))>int(this->connectionStart)+10){
+				if (!m_resources.empty() && int(time(NULL))>int(m_connectionStart)+10){
 					iter = m_resources.begin();
 					m_resource=(*iter).first;
 				}
@@ -747,7 +763,7 @@ void User::receivedPresence(Stanza *stan
 			}
 
 			Log().Get(m_jid) << "resource: " << m_resource;
-			if (!this->connected){
+			if (!m_connected){
 				// we are not connected to legacy network, so we should do it when disco#info arrive :)
 				Log().Get(m_jid) << "connecting: capsVersion=" << m_capsVersion;
 				if (m_readyForConnect==false){
@@ -810,7 +826,7 @@ void User::receivedPresence(Stanza *stan
 		}
 		
 		// send presence about tranport status to user
-		if(this->connected || m_readyForConnect) {
+		if(m_connected || m_readyForConnect) {
 			Stanza *tag = Stanza::createPresenceStanza(m_jid, stanza->status(),stanza->presence());
 			tag->addAttribute( "from", p->jid() );
 			p->j->send( tag );
============================================================
--- user.h	4ee4fa4242e65cf1f852e12821d2dfdea0d7def3
+++ user.h	fd7434d5d35b75272a00cfebf0b5b8f65579d42e
@@ -31,25 +31,6 @@ class GlooxMessageHandler;
 #include "sql.h"
 #include "caps.h"
 
-#include "account.h"
-#include "conversation.h"
-#include "core.h"
-#include "debug.h"
-#include "eventloop.h"
-#include "ft.h"
-#include "log.h"
-#include "notify.h"
-#include "prefs.h"
-#include "prpl.h"
-#include "pounce.h"
-#include "savedstatuses.h"
-#include "sound.h"
-#include "status.h"
-#include "util.h"
-#include "whiteboard.h"
-#include "privacy.h"
-#include "striphtmltags.h"
-
 class RosterRow;
 
 using namespace gloox;
@@ -71,61 +52,71 @@ struct subscribeContact {
 	std::string	group;
 };
 
-class User
-{
+class User {
+	public:
+		User(GlooxMessageHandler *parent, const std::string &jid, const std::string &username, const std::string &password);
+		~User();
 
-public:
-	User(GlooxMessageHandler *parent, const std::string &jid, const std::string &username, const std::string &password);
-	~User();
-// 	void init(GlooxMessageHandler *parent);
-	GlooxMessageHandler *p;
-	bool syncCallback();
-	bool connected;
-	void receivedPresence(Stanza *stanza);
-	void purpleBuddyChanged(PurpleBuddy *buddy);
-	void purpleMessageReceived(PurpleAccount* account,char * name,char *msg,PurpleConversation *conv,PurpleMessageFlags flags);
-	void purpleAuthorizeReceived(PurpleAccount *account,const char *remote_user,const char *id,const char *alias,const char *message,gboolean on_list,PurpleAccountRequestAuthorizationCb authorize_cb,PurpleAccountRequestAuthorizationCb deny_cb,void *user_data);
-	Tag *generatePresenceStanza(PurpleBuddy *buddy);
-	bool isInRoster(const std::string &name,const std::string &subscription);
-	bool isOpenedConversation(const std::string &name);
-	bool hasFeature(int feature);
-	void receivedMessage( Stanza* stanza);
-	void receivedChatState(const std::string &uin,const std::string &state);
-	void purpleBuddyTypingStopped(const std::string &uin);
-	void purpleBuddyTyping(const std::string &uin);
-	void sendRosterX();
-	void syncContacts();
-	void remove();
-	void connect();
-	bool hasTransportFeature(int feature);
-	void purpleReauthorizeBuddy(PurpleBuddy *buddy);
+		void connect();
+		void sendRosterX();
+		void syncContacts();
+		bool hasTransportFeature(int feature); // TODO: move me to p->hasTransportFeature and rewrite my API
 
-	std::string actionData;
-	time_t connectionStart;
+		// Utils
+		Tag *generatePresenceStanza(PurpleBuddy *buddy);
+		bool syncCallback();
+		bool isInRoster(const std::string &name,const std::string &subscription);
+		bool isOpenedConversation(const std::string &name);
+		bool hasFeature(int feature);
+
+		// Gloox callbacks
+		void receivedPresence(Stanza *stanza);
+		void receivedMessage( Stanza* stanza);
+		void receivedChatState(const std::string &uin,const std::string &state);
+
+		// Libpurple callbacks
+		void purpleBuddyChanged(PurpleBuddy *buddy);
+		void purpleMessageReceived(PurpleAccount* account,char * name,char *msg,PurpleConversation *conv,PurpleMessageFlags flags);
+		void purpleAuthorizeReceived(PurpleAccount *account,const char *remote_user,const char *id,const char *alias,const char *message,gboolean on_list,PurpleAccountRequestAuthorizationCb authorize_cb,PurpleAccountRequestAuthorizationCb deny_cb,void *user_data);
+		void purpleBuddyTypingStopped(const std::string &uin);
+		void purpleBuddyTyping(const std::string &uin);
+		void purpleReauthorizeBuddy(PurpleBuddy *buddy);
+		void connected();
+		void disconnected();
+
+
+		std::string actionData;
+		int features;
+
+		// Connected
+		bool isConnected() { return m_connected; }
+
+		// Entity Capabilities
+		std::string capsVersion() { return m_capsVersion; }
+		void setCapsVersion(const std::string &capsVersion) { m_capsVersion = capsVersion; }
+		
+		// Authorization requests
+		bool hasAuthRequest(const std::string &name);
+		void removeAuthRequest(const std::string &name);
+		
+		// bind IP
+		void setBindIP(const std::string& bindIP) { m_bindIP = bindIP; }
+		
+		// connection start
+		time_t connectionStart() { return m_connectionStart; }
+		
+		PurpleAccount *account() { return m_account; }
+		std::map<std::string,int> resources() { return m_resources; }
+		int reconnectCount() { return m_reconnectCount; }
+		bool isVIP() { return m_vip; }
+		bool readyForConnect() { return m_readyForConnect; }
+		std::string username() { return m_username; }
+		std::string jid() { return m_jid; }
+		std::string resource() { return m_resource; }
+		std::map<std::string,RosterRow> roster() { return m_roster; }
+		
+		GlooxMessageHandler *p;
 	
-	int reconnectCount;
-	int features;
-	
-	// Entity Capabilities
-	std::string capsVersion() { return m_capsVersion; }
-	void setCapsVersion(const std::string &capsVersion) { m_capsVersion = capsVersion; }
-	
-	// Authorization requests
-	bool hasAuthRequest(const std::string &name);
-	void removeAuthRequest(const std::string &name);
-	
-	// bind IP
-	void setBindIP(const std::string& bindIP) { m_bindIP = bindIP; }
-	
-	PurpleAccount *account() { return m_account; }
-	std::map<std::string,int> resources() { return m_resources; }
-	bool isVIP() { return m_vip; }
-	bool readyForConnect() { return m_readyForConnect; }
-	std::string username() { return m_username; }
-	std::string jid() { return m_jid; }
-	std::string resource() { return m_resource; }
-	std::map<std::string,RosterRow> roster() { return m_roster; }
-	
 	private:
 		PurpleAccount *m_account;	// PurpleAccount to which this user is connected
 		guint m_syncTimer;			// timer used for syncing purple buddy list and roster
@@ -133,12 +124,15 @@ public:
 		bool m_vip;					// true if the user is VIP
 		bool m_readyForConnect;		// true if the user user wants to connect and we're ready to do it
 		bool m_rosterXCalled;		// true if we are counting buddies for roster X
+		bool m_connected;			// true if this user is connected to legacy account
+		bool m_reconnectCount;		// number of passed reconnect tries
 		std::string m_bindIP;		// IP address to which libpurple will be binded
 		std::string m_password;		// password used to connect to legacy network
 		std::string m_username;		// legacy network user name
 		std::string m_jid;			// Jabber ID of this user
 		std::string m_resource;		// active resource
 		std::string m_capsVersion;	// caps version of client which connected as first (TODO: this should be changed with active resource)
+		time_t m_connectionStart;		// connection start timestamp
 		std::map<std::string,RosterRow> m_roster;	// jabber roster of this user
 		std::map<std::string,int> m_resources;	// list of all resources which are connected to the transport
 		std::map<std::string,authRequest> m_authRequests;	// list of authorization requests (holds callbacks and user data)
============================================================
--- vcardhandler.cpp	c2b671cdb378bbd5444228332fce05c70908a0a1
+++ vcardhandler.cpp	034dc1640f92c658ca044dbe5e9fbcce1f9f145a
@@ -97,7 +97,7 @@ bool GlooxVCardHandler::handleIq (Stanza
 	User *user = p->userManager()->getUserByJID(stanza->from().bare());
 	if (user==NULL)
 		return false;
-	if (!user->connected){
+	if (!user->isConnected()){
 		return false;
 	}
 
@@ -128,7 +128,7 @@ void GlooxVCardHandler::userInfoArrived(
 	PurpleNotifyUserInfoEntry *vcardEntry;
 
 	if (user!=NULL){
-		if (!user->connected)
+		if (!user->isConnected())
 			return;
 		if (!hasVCardRequest(who))
 			return;


More information about the Commits mailing list