From f9363b1062345eed79b1b344799f8a01d43ec699 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Wed, 6 Dec 2017 10:39:03 +0100 Subject: [PATCH] Correctly handle ClientGroupChatRoom subscriptions. --- .../remote-conference-event-handler-p.h | 10 +++- .../remote-conference-event-handler.cpp | 57 +++++++++++++------ src/core/core-listener.h | 2 +- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/conference/handlers/remote-conference-event-handler-p.h b/src/conference/handlers/remote-conference-event-handler-p.h index 87992f7b5..b949a5b42 100644 --- a/src/conference/handlers/remote-conference-event-handler-p.h +++ b/src/conference/handlers/remote-conference-event-handler-p.h @@ -23,6 +23,7 @@ #include "linphone/types.h" #include "chat/chat-room/chat-room-id.h" +#include "core/core-listener.h" #include "object/object-p.h" #include "remote-conference-event-handler.h" @@ -30,9 +31,15 @@ LINPHONE_BEGIN_NAMESPACE -class RemoteConferenceEventHandlerPrivate : public ObjectPrivate { +class RemoteConferenceEventHandlerPrivate : public ObjectPrivate, public CoreListener { private: void simpleNotifyReceived (const std::string &xmlBody); + void subscribe (); + void unsubscribe (); + + // CoreListener + void onNetworkReachable (bool reachable) override; + void onRegistrationStateChanged (LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const std::string &message) override; ChatRoomId chatRoomId; @@ -40,6 +47,7 @@ private: LinphoneEvent *lev = nullptr; unsigned int lastNotify = 0; + bool subscriptionWanted = false; L_DECLARE_PUBLIC(RemoteConferenceEventHandler); }; diff --git a/src/conference/handlers/remote-conference-event-handler.cpp b/src/conference/handlers/remote-conference-event-handler.cpp index 4f0904dd6..e4edc7c67 100644 --- a/src/conference/handlers/remote-conference-event-handler.cpp +++ b/src/conference/handlers/remote-conference-event-handler.cpp @@ -166,6 +166,43 @@ void RemoteConferenceEventHandlerPrivate::simpleNotifyReceived (const string &xm // ----------------------------------------------------------------------------- +void RemoteConferenceEventHandlerPrivate::subscribe () { + if (lev || !subscriptionWanted) + return; // Already subscribed or application did not request subscription + const string &peerAddress = chatRoomId.getPeerAddress().asString(); + LinphoneAddress *lAddr = linphone_address_new(peerAddress.c_str()); + lev = linphone_core_create_subscribe(conf->getCore()->getCCore(), lAddr, "conference", 600); + lev->op->set_from(chatRoomId.getLocalAddress().asString().c_str()); + const string &lastNotifyStr = Utils::toString(lastNotify); + linphone_event_add_custom_header(lev, "Last-Notify-Version", lastNotifyStr.c_str()); + linphone_address_unref(lAddr); + linphone_event_set_internal(lev, TRUE); + linphone_event_set_user_data(lev, this); + lInfo() << "Subscribing to chat room: " << peerAddress << "with last notify: " << lastNotifyStr; + linphone_event_send_subscribe(lev, nullptr); +} + +void RemoteConferenceEventHandlerPrivate::unsubscribe () { + if (lev) { + linphone_event_terminate(lev); + lev = nullptr; + } +} + +// ----------------------------------------------------------------------------- + +void RemoteConferenceEventHandlerPrivate::onNetworkReachable (bool reachable) { + if (!reachable) + unsubscribe(); +} + +void RemoteConferenceEventHandlerPrivate::onRegistrationStateChanged (LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const std::string &message) { + if (state == LinphoneRegistrationOk) + subscribe(); +} + +// ----------------------------------------------------------------------------- + RemoteConferenceEventHandler::RemoteConferenceEventHandler (RemoteConference *remoteConference) : Object(*new RemoteConferenceEventHandlerPrivate) { L_D(); @@ -182,25 +219,14 @@ RemoteConferenceEventHandler::~RemoteConferenceEventHandler () { void RemoteConferenceEventHandler::subscribe (const ChatRoomId &chatRoomId) { L_D(); d->chatRoomId = chatRoomId; - const string &peerAddress = d->chatRoomId.getPeerAddress().asString(); - LinphoneAddress *lAddr = linphone_address_new(peerAddress.c_str()); - d->lev = linphone_core_create_subscribe(d->conf->getCore()->getCCore(), lAddr, "conference", 600); - d->lev->op->set_from(d->chatRoomId.getLocalAddress().asString().c_str()); - const string &lastNotify = Utils::toString(d->lastNotify); - linphone_event_add_custom_header(d->lev, "Last-Notify-Version", lastNotify.c_str()); - linphone_address_unref(lAddr); - linphone_event_set_internal(d->lev, TRUE); - linphone_event_set_user_data(d->lev, this); - lInfo() << "Subscribing to chat room: " << peerAddress << "with last notify: " << lastNotify; - linphone_event_send_subscribe(d->lev, nullptr); + d->subscriptionWanted = true; + d->subscribe(); } void RemoteConferenceEventHandler::unsubscribe () { L_D(); - if (d->lev) { - linphone_event_terminate(d->lev); - d->lev = nullptr; - } + d->unsubscribe(); + d->subscriptionWanted = false; } void RemoteConferenceEventHandler::notifyReceived (const string &xmlBody) { @@ -246,5 +272,4 @@ void RemoteConferenceEventHandler::resetLastNotify () { setLastNotify(0); } - LINPHONE_END_NAMESPACE diff --git a/src/core/core-listener.h b/src/core/core-listener.h index 789b9e6df..1f69a5d86 100644 --- a/src/core/core-listener.h +++ b/src/core/core-listener.h @@ -31,7 +31,7 @@ public: virtual ~CoreListener () = default; virtual void onNetworkReachable (bool reachable) {} - virtual void onRegistrationStateChanged (LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const std::string &message) {} + virtual void onRegistrationStateChanged (LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const std::string &message) {} }; LINPHONE_END_NAMESPACE