Changeset - 098f6a995a82
[Not reviewed]
1 5 1
Jan Kaluza - 14 years ago 2011-06-01 15:50:54
hanzz.k@gmail.com
handleVCardRequired callback
6 files changed with 53 insertions and 14 deletions:
0 comments (0 inline, 0 general)
include/transport/networkpluginserver.h
Show inline comments
 
@@ -34,12 +34,13 @@ class UserManager;
 
class User;
 
class Component;
 
class Buddy;
 
class LocalBuddy;
 
class Config;
 
class NetworkConversation;
 
class VCardResponder;
 

	
 
class NetworkPluginServer {
 
	public:
 
		struct Client {
 
			bool pongReceived;
 
			std::list<User *> users;
 
@@ -68,19 +69,22 @@ class NetworkPluginServer {
 
		void handleUserCreated(User *user);
 
		void handleRoomJoined(User *user, const std::string &room, const std::string &nickname, const std::string &password);
 
		void handleRoomLeft(User *user, const std::string &room);
 
		void handleUserReadyToConnect(User *user);
 
		void handleUserDestroyed(User *user);
 

	
 
		void handleVCardRequired(User *user, const std::string &name, unsigned int id);
 

	
 
		void send(boost::shared_ptr<Swift::Connection> &, const std::string &data);
 

	
 
		void pingTimeout();
 
		void sendPing(Client *c);
 
		Client *getFreeClient();
 

	
 
		UserManager *m_userManager;
 
		VCardResponder *m_vcardResponder;
 
		Config *m_config;
 
		boost::shared_ptr<Swift::ConnectionServer> m_server;
 
		std::list<Client *>  m_clients;
 
		Swift::Timer::ref m_pingTimer;
 
};
 

	
include/transport/usermanager.h
Show inline comments
 
@@ -28,13 +28,12 @@ namespace Transport {
 

	
 
class User;
 
class Component;
 
class StorageBackend;
 
class StorageResponder;
 
class RosterResponder;
 
class VCardResponder;
 

	
 
/// Manages online XMPP Users.
 

	
 
/// This class handles presences and creates User classes when new user connects.
 
/// It also removes the User class once the last user's resource disconnected.
 
class UserManager {
 
@@ -82,11 +81,10 @@ class UserManager {
 
		User *m_cachedUser;
 
		std::map<std::string, User *> m_users;
 
		Component *m_component;
 
		StorageBackend *m_storageBackend;
 
		StorageResponder *m_storageResponder;
 
		RosterResponder *m_rosterResponder;
 
		VCardResponder *m_vcardResponder;
 
		friend class RosterResponder;
 
};
 

	
 
}
include/transport/vcardresponder.h
Show inline comments
 
file renamed from src/vcardresponder.h to include/transport/vcardresponder.h
 
@@ -26,21 +26,32 @@
 
#include "Swiften/Elements/VCard.h"
 

	
 
namespace Transport {
 

	
 
class StorageBackend;
 
class UserManager;
 
class User;
 

	
 
class VCardResponder : public Swift::Responder<Swift::VCard> {
 
	public:
 
		VCardResponder(Swift::IQRouter *router, StorageBackend *storageBackend, UserManager *userManager);
 
		VCardResponder(Swift::IQRouter *router, UserManager *userManager);
 
		~VCardResponder();
 
		boost::signal<void (const Swift::JID& from, const Swift::JID& to, const std::string& id)> onVCardRequired;
 

	
 
		void sendVCard(unsigned int id, boost::shared_ptr<Swift::VCard> vcard);
 

	
 
		boost::signal<void (User *, const std::string &name, unsigned int id)> onVCardRequired;
 

	
 
	private:
 
		struct VCardData {
 
			Swift::JID from;
 
			Swift::JID to;
 
			std::string id;
 
		};
 

	
 
		virtual bool handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::VCard> payload);
 
		virtual bool handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::VCard> payload);
 
		StorageBackend *m_storageBackend;
 
		UserManager *m_userManager;
 
		std::map<unsigned int, VCardData> m_queries;
 
		unsigned int m_id;
 
};
 

	
 
}
 
\ No newline at end of file
src/networkpluginserver.cpp
Show inline comments
 
@@ -25,12 +25,13 @@
 
#include "transport/rostermanager.h"
 
#include "transport/usermanager.h"
 
#include "transport/conversationmanager.h"
 
#include "transport/localbuddy.h"
 
#include "transport/config.h"
 
#include "transport/conversation.h"
 
#include "transport/vcardresponder.h"
 
#include "Swiften/Swiften.h"
 
#include "Swiften/Server/ServerStanzaChannel.h"
 
#include "Swiften/Elements/StreamError.h"
 
#include "pbnetwork.pb.h"
 
#include "sys/wait.h"
 
#include "sys/signal.h"
 
@@ -115,23 +116,28 @@ NetworkPluginServer::NetworkPluginServer(Component *component, Config *config, U
 
	m_userManager->onUserCreated.connect(boost::bind(&NetworkPluginServer::handleUserCreated, this, _1));
 
	m_userManager->onUserDestroyed.connect(boost::bind(&NetworkPluginServer::handleUserDestroyed, this, _1));
 

	
 
	m_pingTimer = component->getFactories()->getTimerFactory()->createTimer(10000);
 
	m_pingTimer->onTick.connect(boost::bind(&NetworkPluginServer::pingTimeout, this)); 
 

	
 
	m_vcardResponder = new VCardResponder(component->getIQRouter(), userManager);
 
	m_vcardResponder->onVCardRequired.connect(boost::bind(&NetworkPluginServer::handleVCardRequired, this, _1, _2, _3));
 
	m_vcardResponder->start();
 

	
 
	m_server = component->getFactories()->getConnectionFactory()->createConnectionServer(10000);
 
	m_server->onNewConnection.connect(boost::bind(&NetworkPluginServer::handleNewClientConnection, this, _1));
 
	m_server->start();
 

	
 
	signal(SIGCHLD, SigCatcher);
 

	
 
	exec_(CONFIG_STRING(m_config, "service.backend").c_str(), "localhost", "10000", m_config->getConfigFile().c_str());
 
}
 

	
 
NetworkPluginServer::~NetworkPluginServer() {
 
	m_pingTimer->stop();
 
	delete m_vcardResponder;
 
}
 

	
 
void NetworkPluginServer::handleNewClientConnection(boost::shared_ptr<Swift::Connection> c) {
 
	Client *client = new Client;
 
	client->pongReceived = true;
 
	client->connection = c;
 
@@ -477,12 +483,16 @@ void NetworkPluginServer::handleMessageReceived(NetworkConversation *conv, boost
 
	WRAP(message, pbnetwork::WrapperMessage_Type_TYPE_CONV_MESSAGE);
 

	
 
	Client *c = (Client *) conv->getConversationManager()->getUser()->getData();
 
	send(c->connection, message);
 
}
 

	
 
void NetworkPluginServer::handleVCardRequired(User *user, const std::string &name, unsigned int id) {
 
	
 
}
 

	
 
void NetworkPluginServer::sendPing(Client *c) {
 

	
 
	std::string message;
 
	pbnetwork::WrapperMessage wrap;
 
	wrap.set_type(pbnetwork::WrapperMessage_Type_TYPE_PING);
 
	wrap.SerializeToString(&message);
src/usermanager.cpp
Show inline comments
 
@@ -23,13 +23,12 @@
 
#include "transport/transport.h"
 
#include "transport/storagebackend.h"
 
#include "transport/conversationmanager.h"
 
#include "transport/rostermanager.h"
 
#include "storageresponder.h"
 
#include "rosterresponder.h"
 
#include "vcardresponder.h"
 

	
 
namespace Transport {
 

	
 
UserManager::UserManager(Component *component, StorageBackend *storageBackend) {
 
	m_cachedUser = NULL;
 
	m_onlineBuddies = 0;
 
@@ -39,26 +38,22 @@ UserManager::UserManager(Component *component, StorageBackend *storageBackend) {
 
	m_storageResponder = new StorageResponder(component->getIQRouter(), m_storageBackend, this);
 
	m_storageResponder->start();
 

	
 
	m_rosterResponder = new RosterResponder(component->getIQRouter(), m_storageBackend, this);
 
	m_rosterResponder->start();
 

	
 
	m_vcardResponder = new VCardResponder(component->getIQRouter(), m_storageBackend, this);
 
	m_vcardResponder->start();
 

	
 
	component->onUserPresenceReceived.connect(bind(&UserManager::handlePresence, this, _1));
 
	m_component->getStanzaChannel()->onMessageReceived.connect(bind(&UserManager::handleMessageReceived, this, _1));
 
	m_component->getStanzaChannel()->onPresenceReceived.connect(bind(&UserManager::handleGeneralPresenceReceived, this, _1));
 
// 	component->onDiscoInfoResponse.connect(bind(&UserManager::handleDiscoInfoResponse, this, _1, _2, _3));
 
}
 

	
 
UserManager::~UserManager(){
 
	m_storageResponder->stop();
 
	delete m_storageResponder;
 
	delete m_rosterResponder;
 
	delete m_vcardResponder;
 
}
 

	
 
void UserManager::addUser(User *user) {
 
	m_users[user->getJID().toBare().toString()] = user;
 
	onUserCreated(user);
 
}
src/vcardresponder.cpp
Show inline comments
 
@@ -15,13 +15,13 @@
 
 *
 
 * 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 "vcardresponder.h"
 
#include "transport/vcardresponder.h"
 

	
 
#include <iostream>
 
#include <boost/bind.hpp>
 
#include "Swiften/Queries/IQRouter.h"
 
#include "Swiften/Swiften.h"
 
#include "transport/user.h"
 
@@ -30,24 +30,45 @@
 

	
 
using namespace Swift;
 
using namespace boost;
 

	
 
namespace Transport {
 

	
 
VCardResponder::VCardResponder(Swift::IQRouter *router, StorageBackend *storageBackend, UserManager *userManager) : Swift::Responder<VCard>(router) {
 
	m_storageBackend = storageBackend;
 
VCardResponder::VCardResponder(Swift::IQRouter *router, UserManager *userManager) : Swift::Responder<VCard>(router) {
 
	m_id = 0;
 
	m_userManager = userManager;
 
}
 

	
 
VCardResponder::~VCardResponder() {
 
}
 

	
 
void VCardResponder::sendVCard(unsigned int id, boost::shared_ptr<Swift::VCard> vcard) {
 
	if (m_queries.find(id) == m_queries.end())
 
		return;
 

	
 
	sendResponse(m_queries[id].to, m_queries[id].from, m_queries[id].id, vcard);
 
	m_queries.erase(id);
 
}
 

	
 
bool VCardResponder::handleGetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::VCard> payload) {
 
	// Get means we're in server mode and user wants to fetch his roster.
 
	// For now we send empty reponse, but TODO: Get buddies from database and send proper stored roster.
 
	onVCardRequired(from, to, id);
 
	User *user = m_userManager->getUser(from.toBare().toString());
 
	if (!user) {
 
		return false;
 
	}
 

	
 
	std::string name = to.getUnescapedNode();
 
	if (name.find_last_of("%") != std::string::npos) {
 
		name.replace(name.find_last_of("%"), 1, "@");
 
	}
 

	
 
	m_queries[m_id].from = from;
 
	m_queries[m_id].to = to;
 
	m_queries[m_id].id = id; 
 
	onVCardRequired(user, name, m_id++);
 
	return true;
 
}
 

	
 
bool VCardResponder::handleSetRequest(const Swift::JID& from, const Swift::JID& to, const std::string& id, boost::shared_ptr<Swift::VCard> payload) {
 
	sendResponse(from, id, boost::shared_ptr<VCard>(new VCard()));
 
	return true;
0 comments (0 inline, 0 general)