From ce70d3478882c41d077028f182a3baf7e362f45c Mon Sep 17 00:00:00 2001 From: Tarun Gupta Date: Thu, 24 Aug 2017 21:16:31 +0530 Subject: [PATCH] Update 1 Change-Id: Ie68aba5e6ab3995b8195056c6a1cc20e69edac2e --- Limber/MIXMockServer.cpp | 90 +++++++++++-------- .../MIXListAndJoin/MIXListAndJoin.cpp | 1 - Swiften/MIX/MIX.h | 2 +- 3 files changed, 52 insertions(+), 41 deletions(-) diff --git a/Limber/MIXMockServer.cpp b/Limber/MIXMockServer.cpp index 4426efaa9a..4cfefc6ca2 100644 --- a/Limber/MIXMockServer.cpp +++ b/Limber/MIXMockServer.cpp @@ -18,19 +18,20 @@ #include #include -#include -#include +#include #include #include +#include #include #include #include #include +#include #include #include #include -#include #include +#include #include #include #include @@ -40,16 +41,14 @@ #include #include #include -#include #include #include +#include #include #include #include -#include - using namespace Swift; class SimpleMIXChannelRegistry { @@ -138,30 +137,23 @@ class Server { participantInformationMap_.insert(std::make_pair(session->getRemoteJID().toBare(), information_)); } - auto message = std::dynamic_pointer_cast(stanza); - if (message) { - handleMessageReceived(message, session); - return; - } - - auto iq = std::dynamic_pointer_cast(stanza); - if (!iq) { - return; - } - if (mixChannelRegistry_->hasMIXChannel(stanza->getTo())) { //If request comes to particular channel supported by service. - handleElementReceivedForMIXChannel(iq, session); + handleElementReceivedForMIXChannel(stanza, session); } else if (stanza->getTo() == session->getLocalJID()) { //If request comes to domain service. - handleElementReceivedForMIXService(iq, session); + handleElementReceivedForMIXService(stanza, session); } else if (stanza->getTo() == session->getRemoteJID().toBare() || stanza->getTo() == JID()) { //If request comes to own account. - handleElementReceivedForAccount(iq, session); + handleElementReceivedForAccount(stanza, session); } } - void handleMessageReceived(Message::ref message, std::shared_ptr session) { + /** + * If message is received by MIX channel, it sends it to all clients with JID as proxy JID of sender. + * It sends a replicated message with same submission ID as received message to the sender for verification. + */ + void handleMessageReceivedForMIXChannel(Message::ref message, std::shared_ptr session) { SWIFT_LOG(debug) << "Message Received" << std::endl; auto channelJID = message->getTo(); auto sender = message->getFrom(); @@ -169,6 +161,8 @@ class Server { SWIFT_LOG(debug) << "Sender " << sender << std::endl; SWIFT_LOG_ASSERT(mixChannelRegistry_->hasMIXChannel(channelJID), warning); + + // Send message to all clients. auto i = participantMap_.find(channelJID); if (i != participantMap_.end()) { auto participants = i->second; @@ -199,34 +193,47 @@ class Server { } } + // Sending replicated message to sender with same submission ID as received message. mixPayload->setSubmissionID(message->getID()); forwardMessage->setTo(sender); session->sendElement(forwardMessage); } } - void handleElementReceivedForAccount(IQ::ref iq, std::shared_ptr session) { - if(iq->getPayload()) { - handleRosterRequest(iq, session); - } else if (auto incomingJoinPayload = iq->getPayload()) { - handleJoin(iq, session, incomingJoinPayload); - } else if (auto incomingLeavePayload = iq->getPayload()) { - handleLeave(iq, session, incomingLeavePayload); + void handleElementReceivedForAccount(Stanza::ref stanza, std::shared_ptr session) { + if (auto iq = std::dynamic_pointer_cast(stanza)) { + if(iq->getPayload()) { + handleRosterRequest(iq, session); + } else if (auto incomingJoinPayload = iq->getPayload()) { + handleJoin(iq, session, incomingJoinPayload); + } else if (auto incomingLeavePayload = iq->getPayload()) { + handleLeave(iq, session, incomingLeavePayload); + } } } - void handleElementReceivedForMIXService(IQ::ref iq, std::shared_ptr session) { - if (iq->getPayload()) { - SWIFT_LOG(debug) << "Query: Channel List" << std::endl; - auto responsePayload = std::make_shared(); - for (auto channel : mixChannelRegistry_->getChannels()) { - responsePayload->addItem(DiscoItems::Item(JID(channel).getNode(), JID(channel))); + void handleElementReceivedForMIXService(Stanza::ref stanza, std::shared_ptr session) { + if (auto iq = std::dynamic_pointer_cast(stanza)) { + if (iq->getPayload()) { + SWIFT_LOG(debug) << "Query: Channel List" << std::endl; + auto responsePayload = std::make_shared(); + for (auto channel : mixChannelRegistry_->getChannels()) { + responsePayload->addItem(DiscoItems::Item(JID(channel).getNode(), JID(channel))); + } + session->sendElement(IQ::createResult(iq->getFrom(), iq->getTo(), iq->getID(), responsePayload)); } - session->sendElement(IQ::createResult(iq->getFrom(), iq->getTo(), iq->getID(), responsePayload)); } } - void handleElementReceivedForMIXChannel(IQ::ref iq, std::shared_ptr session) { + void handleElementReceivedForMIXChannel(Stanza::ref stanza, std::shared_ptr session) { + if (auto message = std::dynamic_pointer_cast(stanza)) { + handleMessageReceivedForMIXChannel(message, session); + } else if (auto iq = std::dynamic_pointer_cast(stanza)) { + handleIQReceivedForMIXChannel(iq, session); + } + } + + void handleIQReceivedForMIXChannel(IQ::ref iq, std::shared_ptr session) { if (auto pubSubPayload = iq->getPayload()) { SWIFT_LOG(debug) << "Query: PubSub" << std::endl; auto channelJID = iq->getTo(); @@ -234,7 +241,7 @@ class Server { auto itemsPayload = std::dynamic_pointer_cast(pubSubPayload->getPayload()); if (itemsPayload->getNode() == MIX::ParticipantsNode) { SWIFT_LOG(debug) << "Query: Participant List for " << channelJID << std::endl; - session->sendElement(IQ::createResult(iq->getFrom(), iq->getTo(), iq->getID(), getParticipantsOfChannel(channelJID))); + session->sendElement(IQ::createResult(iq->getFrom(), iq->getTo(), iq->getID(), retrieveParticipantsOfChannel(channelJID))); } else if (itemsPayload->getNode() == MIX::JIDMapNode) { SWIFT_LOG(debug) << "Query: Lookup Participants from " << channelJID << std::endl; session->sendElement(IQ::createResult(iq->getFrom(), iq->getTo(), iq->getID(), getRealJIDResponse(pubSubPayload))); @@ -326,6 +333,11 @@ class Server { if (j != sessionMap_.end()) { sessionMap_.erase(iq->getFrom()); } + + auto k = participantInformationMap_.find(iq->getFrom()); + if (k != participantInformationMap_.end()) { + participantInformationMap_.erase(iq->getFrom()); + } } else { SWIFT_LOG(debug) << "Initial roster not requested by client." < getParticipantsOfChannel(const JID& channelJID) { + std::shared_ptr retrieveParticipantsOfChannel(const JID& channelJID) { auto pubSubPayload = std::make_shared(); auto pubSubItems = std::make_shared(); pubSubItems->setNode(MIX::ParticipantsNode); @@ -419,7 +431,7 @@ class Server { auto itemsPayload = std::dynamic_pointer_cast(payload->getPayload()); for (auto item : itemsPayload->getItems()) { for( auto iter = participantInformationMap_.begin(), iend = participantInformationMap_.end(); iter != iend; ++iter ) { - if (*((iter->second)->getProxyJID()) == item->getID()) { + if ((iter->second)->getProxyJID() && *((iter->second)->getProxyJID()) == item->getID()) { auto mixParticipant = std::make_shared(); mixParticipant->setJID(iter->first); item->addData(mixParticipant); diff --git a/Swiften/Examples/MIXListAndJoin/MIXListAndJoin.cpp b/Swiften/Examples/MIXListAndJoin/MIXListAndJoin.cpp index bb4f4804f5..2c1564c800 100644 --- a/Swiften/Examples/MIXListAndJoin/MIXListAndJoin.cpp +++ b/Swiften/Examples/MIXListAndJoin/MIXListAndJoin.cpp @@ -51,7 +51,6 @@ static void handleMessageReceived(Message::ref message) { if (auto mixPayload = message->getPayload()) { if (mixPayload->getSubmissionID()) { SWIFT_LOG(debug) << "Ignoring the replicated message" << std::endl; - return; } else if (message->getFrom().toBare() == mixChannelJID) { std::cout << "[ " << mixChannelJID << " ] " << message->getFrom().getResource() << ": " << message->getBody().get_value_or("") << std::endl; } diff --git a/Swiften/MIX/MIX.h b/Swiften/MIX/MIX.h index 763457a31b..bd6760980a 100644 --- a/Swiften/MIX/MIX.h +++ b/Swiften/MIX/MIX.h @@ -76,6 +76,6 @@ namespace Swift { boost::signals2::signal onPreferencesFormResponse; boost::signals2::signal onPreferencesUpdateResponse; boost::signals2::signal /* responsePubSub */, ErrorPayload::ref /* lookupError */)> onLookupResponse; - boost::signals2::signal /* responsePubSub */, ErrorPayload::ref /* lookupError */)> onParticipantResponse; + boost::signals2::signal /* responsePubSub */, ErrorPayload::ref /* errorResponse */)> onParticipantResponse; }; }