Comments + refactorization

This commit is contained in:
HanzZ 2011-06-06 10:48:27 +02:00
parent 7a0e5e31e6
commit d39705842a
13 changed files with 178 additions and 54 deletions

View file

@ -39,12 +39,18 @@ typedef enum { BUDDY_NO_FLAG = 0,
class Buddy {
public:
/// Constructor.
/// \param rosterManager RosterManager associated with this buddy.
/// \param id ID which identifies the buddy in database or -1 if it's new buddy which is
/// not in database yet.
Buddy(RosterManager *rosterManager, long id = -1);
/// Destructor
virtual ~Buddy();
/// Sets unique ID used to identify this buddy by StorageBackend. This is set
/// Sets unique ID used to identify this buddy by StorageBackend.
/// This is set
/// by RosterStorage class once the buddy is stored into database or when the
/// buddy is loaded from database.
/// You should not need to set this ID manually.
@ -52,16 +58,19 @@ class Buddy {
void setID(long id);
/// Returns unique ID used to identify this buddy by StorageBackend.
/// \see Buddy::setID(long)
/// \return ID
/// \return ID which identifies the buddy in database or -1 if it's new buddy which is
/// not in database yet.
long getID();
/// Returns full JID of this buddy.
/// \param hostname hostname used as domain in returned JID
/// \return full JID of this buddy
const Swift::JID &getJID();
/// Generates whole Presennce stanza with current status/show for this buddy.
/// Presence stanza does not containt "to" attribute, it has to be added manually.
/// \param features features used in returned stanza
/// \param only_new if True, this function returns Presence stanza only if it's different
@ -76,65 +85,84 @@ class Buddy {
void setOffline();
/// Returns true if this buddy is marked as available/online.
/// \return true if this buddy is marked as available/online.
bool isOnline();
/// Sets current subscription.
/// \param subscription "to", "from", "both", "ask"
void setSubscription(const std::string &subscription);
/// Returns current subscription
/// \return subscription "to", "from", "both", "ask"
const std::string &getSubscription();
/// Sets this buddy's flags.
/// \param flags flags
void setFlags(BuddyFlag flags);
/// Returns this buddy's flags.
/// \param flags flags
BuddyFlag getFlags();
/// Returns RosterManager associated with this buddy
/// \return rosterManager
/// Returns RosterManager associated with this buddy.
/// \return RosterManager associated with this buddy.
RosterManager *getRosterManager() { return m_rosterManager; }
/// Returns legacy network username which does not contain unsafe characters,
/// so it can be used in JIDs.
std::string getSafeName();
void buddyChanged();
/// This method should be called whenever some information returned by virtual functions changes.
void handleVCardReceived(const std::string &id, const Swift::JID &to, Swift::VCard::ref vcard);
/// This method sends presence to XMPP user.
void handleBuddyChanged();
/// Handles VCard from legacy network and forwards it to XMPP user.
/// \param id ID used in IQ-result.
/// \param vcard VCard which will be sent.
void handleVCardReceived(const std::string &id, Swift::VCard::ref vcard);
/// This signal is emitted when buddyChanged method is called.
boost::signal<void ()> onBuddyChanged;
virtual void getVCard(const std::string &id, const Swift::JID &to) = 0;
/// Returns legacy network username of this buddy. (for example UIN for ICQ, JID for Jabber, ...).
/// Returns legacy network username of this buddy. (for example UIN for ICQ,
/// JID for Jabber, ...).
/// \return legacy network username
virtual std::string getName() = 0;
/// Returns alias (nickname) of this buddy.
/// \return alias (nickname)
virtual std::string getAlias() = 0;
/// Returns list of groups this buddy is in.
/// \return groups
virtual std::vector<std::string> getGroups() = 0;
/// Returns current legacy network status and statuMessage of this buddy.
/// \param status current status/show is stored here
/// \param statusMessage current status message is stored here
/// \return true if status was stored successfully
virtual bool getStatus(Swift::StatusShow &status, std::string &statusMessage) = 0;
/// Returns SHA-1 hash of buddy icon (avatar) or empty string if there is no avatar
/// for this buddy.
/// Returns SHA-1 hash of buddy icon (avatar) or empty string if there is no avatar for this buddy.
/// \return avatar hash or empty string.
virtual std::string getIconHash() = 0;
/// Returns legacy name of buddy from JID.
/// \param jid Jabber ID.
/// \return legacy name of buddy from JID.
static std::string JIDToLegacyName(const Swift::JID &jid);
private:

View file

@ -53,23 +53,30 @@ class Config {
/// Destructor
virtual ~Config() {}
/// Loads data from config file. You can pass your extra options which will be recognized by
/// Loads data from config file.
/// You can pass your extra options which will be recognized by
/// the parser using opts parameter.
/// \param configfile path to config file
/// \param opts extra options which will be recognized by a parser
bool load(const std::string &configfile, boost::program_options::options_description &opts);
/// Loads data from config file. This function loads only config variables needed by libtransport.
/// Loads data from config file.
/// This function loads only config variables needed by libtransport.
/// \see load(const std::string &, boost::program_options::options_description &)
/// \param configfile path to config file
bool load(const std::string &configfile);
/// Returns value of variable defined by key. For variables in sections you can use "section.variable" key format.
/// Returns value of variable defined by key.
/// For variables in sections you can use "section.variable" key format.
/// \param key config variable name
const boost::program_options::variable_value &operator[] (const std::string &key) {
return m_variables[key];
}
/// Returns path to config file from which data were loaded.
const std::string &getConfigFile() { return m_file; }
/// This signal is emitted when config is loaded/reloaded.

View file

@ -31,38 +31,79 @@ namespace Transport {
class ConversationManager;
/// Represents one XMPP-Legacy network conversation.
class Conversation {
public:
/// Type of participants in MUC rooms.
enum ParticipantFlag {None, Moderator};
/// Constructor.
Conversation(ConversationManager *conversationManager, const std::string &legacyName, bool m_muc = false);
/// Creates new conversation.
/// Destructor
/// \param conversationManager ConversationManager associated with this Conversation.
/// \param legacyName Legacy network name of recipient.
/// \param muc True if this conversation is Multi-user chat.
Conversation(ConversationManager *conversationManager, const std::string &legacyName, bool muc = false);
/// Destructor.
virtual ~Conversation();
/// Returns legacy network name of this conversation.
/// \return legacy network name of this conversation.
const std::string &getLegacyName() { return m_legacyName; }
/// Handles new message from Legacy network and forwards it to XMPP.
/// \param message Message received from legacy network.
/// \param nickname For MUC conversation this is nickname of room participant who sent this message.
void handleMessage(boost::shared_ptr<Swift::Message> &message, const std::string &nickname = "");
/// Handles participant change in MUC.
/// \param nickname Nickname of participant which changed.
/// \param flag ParticipantFlag.
/// \param status Current status of this participant.
/// \param statusMessage Current status message of this participant.
/// \param newname If participant was renamed, this variable contains his new name.
void handleParticipantChanged(const std::string &nickname, int flag, int status = Swift::StatusShow::None, const std::string &statusMessage = "", const std::string &newname = "");
/// Sets XMPP user nickname in MUC rooms.
/// \param nickname XMPP user nickname in MUC rooms.
void setNickname(const std::string &nickname) {
m_nickname = nickname;
}
/// Sends message to Legacy network.
/// \param message Message.
virtual void sendMessage(boost::shared_ptr<Swift::Message> &message) = 0;
/// Returns ConversationManager associated with this Conversation.
/// \return ConversationManager associated with this Conversation.
ConversationManager *getConversationManager() {
return m_conversationManager;
}
/// Returns True if this conversation is MUC room.
/// \return True if this conversation is MUC room.
bool isMUC() {
return m_muc;
}
/// Sets room name associated with this Conversation.
/// This is used to detect Private messages associated with particular room.
/// \param room room name associated with this Conversation.
void setRoom(const std::string &room) {
m_room = room;
}
/// Returns room name associated with this Conversation.
/// \return room name associated with this Conversation.
const std::string &getRoom() {
return m_room;
}

View file

@ -31,29 +31,45 @@ class Conversation;
class User;
class Component;
/// Manages all Conversations of particular User.
class ConversationManager {
public:
/// Creates new ConversationManager.
/// \param user User associated with this ConversationManager.
/// \param component Transport instance associated with this roster.
/// \param component Transport instance associated with this ConversationManager.
ConversationManager(User *user, Component *component);
/// Destructor.
virtual ~ConversationManager();
/// Returns user associated with this manager.
/// \return User
User *getUser() { return m_user; }
/// Returns component associated with this ConversationManager.
/// \return component associated with this ConversationManager.
Component *getComponent() { return m_component; }
/// Returns Conversation by its legacy network name (for example by UIN in case of ICQ).
/// \param name legacy network name.
/// \return Conversation or NULL.
Conversation *getConversation(const std::string &name) {
return m_convs[name];
}
void setConversation(Conversation *conv);
/// Adds new Conversation to the manager.
void unsetConversation(Conversation *conv);
/// \param conv Conversation.
void addConversation(Conversation *conv);
/// Removes Conversation from the manager.
/// \param conv Conversation.
void removeConversation(Conversation *conv);
private:
void handleMessageReceived(Swift::Message::ref message);

View file

@ -56,8 +56,6 @@ class LocalBuddy : public Buddy {
std::vector<std::string> getGroups() { return m_groups; }
void setGroups(const std::vector<std::string> &groups) { m_groups = groups; }
void getVCard(const std::string &id, const Swift::JID &to) {}
private:
std::string m_name;
std::string m_alias;

View file

@ -52,61 +52,92 @@ namespace Transport {
/// Represents one transport instance.
/// It's used to connect
/// the Jabber server and provides transaction layer between Jabber server
/// and other classes.
/// It's used to connect the Jabber server and provides transaction layer
/// between Jabber server and other classes.
///
/// In server mode it represents Jabber server to which users can connect and use
/// it as transport.
class Component {
public:
/// Creates new Component instance.
/// \param loop main event loop
/// \param config cofiguration, this class uses following Config values:
/// \param loop Main event loop.
/// \param config Cofiguration; this class uses following Config values:
/// - service.jid
/// - service.password
/// - service.server
/// - service.port
/// - service.server_mode
/// \param factory Transport Abstract factory used to create basic transport structures.
Component(Swift::EventLoop *loop, Config *config, Factory *factory);
/// Component destructor.
~Component();
/// Returns Swift::Component associated with this Transport::Component.
/// You can use it to send presences and other stanzas.
/// \return Swift::Component associated with this Transport::Component
/// Returns Swift::StanzaChannel associated with this Transport::Component.
/// It can be used to send presences and other stanzas.
/// \return Swift::StanzaChannel associated with this Transport::Component.
Swift::StanzaChannel *getStanzaChannel();
/// Returns Swift::IQRouter associated with this Component.
/// \return Swift::IQRouter associated with this Component.
Swift::IQRouter *getIQRouter() { return m_iqRouter; }
/// Returns Swift::PresenceOracle associated with this Transport::Component.
/// You can use it to check current resource connected for particular user.
/// \return Swift::PresenceOracle associated with this Transport::Component
/// \return Swift::PresenceOracle associated with this Transport::Component.
Swift::PresenceOracle *getPresenceOracle();
/// Returns True if the component is in server mode.
/// \return True if the component is in server mode.
bool inServerMode() { return m_server != NULL; }
/// Returns user password from internal UserRegistry.
/// In server mode, the password user used for login can be obtained by
/// this method.
/// \param barejid User's bare JID.
/// \return User's password.
const std::string &getUserRegistryPassword(const std::string &barejid);
/// Connects the Jabber server.
/// \see Component()
/// In server mode this function does nothing.
void connect();
/// Sets disco#info features which are sent as answer to
/// disco#info IQ-get. This sets features of transport contact (For example "j2j.domain.tld").
/// Sets disco#info features which are sent as answer to disco#info IQ-get.
/// This sets features of transport contact (For example "j2j.domain.tld").
/// \param features list of features as sent in disco#info response
void setTransportFeatures(std::list<std::string> &features);
/// Sets disco#info features which are sent as answer to
/// disco#info IQ-get. This sets features of legacy network buddies (For example "me\40gmail.com@j2j.domain.tld").
/// Sets disco#info features which are sent as answer to disco#info IQ-get.
/// This sets features of legacy network buddies (For example "me\40gmail.com@j2j.domain.tld").
/// \param features list of features as sent in disco#info response
void setBuddyFeatures(std::list<std::string> &features);
/// Returns Jabber ID of this transport.
/// \return Jabber ID of this transport
Swift::JID &getJID() { return m_jid; }
Swift::BoostNetworkFactories *getFactories() { return m_factories; }
/// Returns Swift::NetworkFactories which can be used to create new connections.
/// \return Swift::NetworkFactories which can be used to create new connections.
Swift::BoostNetworkFactories *getNetworkFactories() { return m_factories; }
/// Returns Transport Factory used to create basic Transport components.
/// \return Transport Factory used to create basic Transport components.
Factory *getFactory() { return m_factory; }
/// This signal is emitted when server disconnects the transport because of some error.
/// \param error disconnection error
boost::signal<void (const Swift::ComponentError &error)> onConnectionError;
@ -114,15 +145,18 @@ namespace Transport {
boost::signal<void ()> onConnected;
/// This signal is emitted when XML stanza is sent to server.
/// \param xml xml stanza
boost::signal<void (const std::string &xml)> onXMLOut;
/// This signal is emitted when XML stanza is received from server.
/// \param xml xml stanza
boost::signal<void (const std::string &xml)> onXMLIn;
/// This signal is emitted when presence from XMPP user (for example "user@domain.tld")
/// is received. It's emitted only for presences addressed to transport itself
/// This signal is emitted when presence from XMPP user is received.
/// It's emitted only for presences addressed to transport itself
/// (for example to="j2j.domain.tld").
/// \param presence presence data
boost::signal<void (Swift::Presence::ref presence)> onUserPresenceReceived;

View file

@ -146,7 +146,7 @@ std::string Buddy::getSafeName() {
return name;
}
void Buddy::buddyChanged() {
void Buddy::handleBuddyChanged() {
Swift::Presence::ref presence = generatePresenceStanza(255);
if (presence) {
m_rosterManager->getUser()->getComponent()->getStanzaChannel()->sendPresence(presence);
@ -154,7 +154,7 @@ void Buddy::buddyChanged() {
onBuddyChanged();
}
void Buddy::handleVCardReceived(const std::string &id, const Swift::JID &to, Swift::VCard::ref vcard) {
void Buddy::handleVCardReceived(const std::string &id, Swift::VCard::ref vcard) {
boost::shared_ptr<Swift::GenericRequest<Swift::VCard> > request(new Swift::GenericRequest<Swift::VCard>(Swift::IQ::Result, m_rosterManager->getUser()->getJID(), vcard, m_rosterManager->getUser()->getComponent()->getIQRouter()));
request->send();
}

View file

@ -30,12 +30,12 @@ namespace Transport {
Conversation::Conversation(ConversationManager *conversationManager, const std::string &legacyName, bool isMUC) : m_conversationManager(conversationManager) {
m_legacyName = legacyName;
m_conversationManager->setConversation(this);
m_conversationManager->addConversation(this);
m_muc = isMUC;
}
Conversation::~Conversation() {
m_conversationManager->unsetConversation(this);
m_conversationManager->removeConversation(this);
}
void Conversation::handleMessage(boost::shared_ptr<Swift::Message> &message, const std::string &nickname) {

View file

@ -41,11 +41,11 @@ ConversationManager::~ConversationManager() {
}
}
void ConversationManager::setConversation(Conversation *conv) {
void ConversationManager::addConversation(Conversation *conv) {
m_convs[conv->getLegacyName()] = conv;
}
void ConversationManager::unsetConversation(Conversation *conv) {
void ConversationManager::removeConversation(Conversation *conv) {
for (std::map<std::string, Conversation *>::const_iterator it = m_convs.begin(); it != m_convs.end(); it++) {
if ((*it).second->getRoom() == conv->getLegacyName()) {
(*it).second->setRoom("");

View file

@ -120,7 +120,7 @@ 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 = component->getNetworkFactories()->getTimerFactory()->createTimer(10000);
m_pingTimer->onTick.connect(boost::bind(&NetworkPluginServer::pingTimeout, this));
m_vcardResponder = new VCardResponder(component->getIQRouter(), userManager);
@ -130,7 +130,7 @@ NetworkPluginServer::NetworkPluginServer(Component *component, Config *config, U
m_rosterResponder = new RosterResponder(component->getIQRouter(), userManager);
m_rosterResponder->start();
m_server = component->getFactories()->getConnectionFactory()->createConnectionServer(10000);
m_server = component->getNetworkFactories()->getConnectionFactory()->createConnectionServer(10000);
m_server->onNewConnection.connect(boost::bind(&NetworkPluginServer::handleNewClientConnection, this, _1));
m_server->start();
@ -234,7 +234,7 @@ void NetworkPluginServer::handleBuddyChangedPayload(const std::string &data) {
LocalBuddy *buddy = (LocalBuddy *) user->getRosterManager()->getBuddy(payload.buddyname());
if (buddy) {
handleBuddyPayload(buddy, payload);
buddy->buddyChanged();
buddy->handleBuddyChanged();
}
else {
buddy = new LocalBuddy(user->getRosterManager(), -1);

View file

@ -36,8 +36,8 @@ RosterManager::RosterManager(User *user, Component *component){
m_rosterStorage = NULL;
m_user = user;
m_component = component;
m_setBuddyTimer = m_component->getFactories()->getTimerFactory()->createTimer(1000);
m_RIETimer = m_component->getFactories()->getTimerFactory()->createTimer(5000);
m_setBuddyTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(1000);
m_RIETimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(5000);
m_RIETimer->onTick.connect(boost::bind(&RosterManager::sendRIE, this));
@ -121,7 +121,7 @@ void RosterManager::unsetBuddy(Buddy *buddy) {
void RosterManager::handleBuddyRosterPushResponse(Swift::ErrorPayload::ref error, const std::string &key) {
if (m_buddies[key] != NULL) {
m_buddies[key]->buddyChanged();
m_buddies[key]->handleBuddyChanged();
}
}

View file

@ -77,7 +77,7 @@ namespace Transport {
RosterStorage::RosterStorage(User *user, StorageBackend *storageBackend) {
m_user = user;
m_storageBackend = storageBackend;
m_storageTimer = m_user->getComponent()->getFactories()->getTimerFactory()->createTimer(5000);
m_storageTimer = m_user->getComponent()->getNetworkFactories()->getTimerFactory()->createTimer(5000);
m_storageTimer->onTick.connect(boost::bind(&RosterStorage::storeBuddies, this));
}

View file

@ -42,7 +42,7 @@ User::User(const Swift::JID &jid, UserInfo &userInfo, Component *component, User
m_connected = false;
m_readyForConnect = false;
m_reconnectTimer = m_component->getFactories()->getTimerFactory()->createTimer(10000);
m_reconnectTimer = m_component->getNetworkFactories()->getTimerFactory()->createTimer(10000);
m_reconnectTimer->onTick.connect(boost::bind(&User::onConnectingTimeout, this));
m_rosterManager = new RosterManager(this, m_component);