diff --git a/src/chat/chat-room/client-group-chat-room.cpp b/src/chat/chat-room/client-group-chat-room.cpp index c796f5fee..496be28ac 100644 --- a/src/chat/chat-room/client-group-chat-room.cpp +++ b/src/chat/chat-room/client-group-chat-room.cpp @@ -139,8 +139,8 @@ void ClientGroupChatRoomPrivate::onCallSessionStateChanged ( if (q->getState() == ChatRoom::State::CreationPending) { IdentityAddress addr(session->getRemoteContactAddress()->asStringUriOnly()); q->onConferenceCreated(addr); - if (session->getRemoteContactAddress()->hasParam("isfocus")){ - bgTask.start(q->getCore(), 32); /*it will be stopped when receiving the first notify*/ + if (session->getRemoteContactAddress()->hasParam("isfocus")) { + bgTask.start(q->getCore(), 32); // It will be stopped when receiving the first notify qConference->getPrivate()->eventHandler->subscribe(q->getChatRoomId()); } } else if (q->getState() == ChatRoom::State::TerminationPending) @@ -484,12 +484,13 @@ void ClientGroupChatRoom::onFirstNotifyReceived (const IdentityAddress &addr) { shared_ptr chatRoom = getCore()->findChatRoom(id); if (chatRoom && (chatRoom->getCapabilities() & ChatRoom::Capabilities::Basic)) { BasicToClientGroupChatRoom::migrate(getSharedFromThis(), chatRoom); - goto end; + d->bgTask.stop(); + return; } } d->chatRoomListener->onChatRoomInsertInDatabaseRequested(getSharedFromThis()); -end: + d->bgTask.stop(); // TODO: Bug. Event is inserted many times. // Avoid this in the future. Deal with signals/slots system. diff --git a/src/utils/background-task.cpp b/src/utils/background-task.cpp index bca543c35..0aaeb3397 100644 --- a/src/utils/background-task.cpp +++ b/src/utils/background-task.cpp @@ -21,39 +21,45 @@ #include "c-wrapper/internal/c-sal.h" #include "logger/logger.h" #include "core/core-p.h" -#include "private.h" //to get access to the Sal + +// TODO: Remove me +#include "private.h" // To get access to the Sal // ============================================================================= LINPHONE_BEGIN_NAMESPACE -void BackgroundTask::sHandleTimeout(void *context) { +void BackgroundTask::sHandleTimeout (void *context) { static_cast(context)->handleTimeout(); } -int BackgroundTask::sHandleSalTimeout(void *data, unsigned int events){ +int BackgroundTask::sHandleSalTimeout (void *data, unsigned int events) { static_cast(data)->handleSalTimeout(); return BELLE_SIP_STOP; } -void BackgroundTask::handleSalTimeout(){ - lWarning() << "Background task [" << mId << "] with name : [" << mName << "] is now expiring."; +void BackgroundTask::handleSalTimeout () { + lWarning() << "Background task [" << mId << "] with name: [" << mName << "] is now expiring"; stop(); } -void BackgroundTask::start (const std::shared_ptr &core, int max_duration_seconds) { +void BackgroundTask::start (const std::shared_ptr &core, int maxDurationSeconds) { if (mName.empty()) { - lError() << "No name was set on background task."; + lError() << "No name was set on background task"; return; } unsigned long newId = sal_begin_background_task(mName.c_str(), sHandleTimeout, this); - lInfo() << "Starting background task [" << newId << "] with name : [" << mName << "] and expiration of ["< 0){ + if (maxDurationSeconds > 0) { mSal = core->getCCore()->sal; - mTimeout = mSal->create_timer(sHandleSalTimeout, this, (unsigned int) max_duration_seconds * 1000, mName.c_str()); + mTimeout = mSal->create_timer(sHandleSalTimeout, this, (unsigned int)maxDurationSeconds * 1000, mName.c_str()); } } @@ -61,18 +67,19 @@ void BackgroundTask::stop () { if (mId == 0) return; - lInfo() << "Ending background task [" << mId << "] with name : [" << mName << "]."; + lInfo() << "Ending background task [" << mId << "] with name: [" << mName << "]"; sal_end_background_task(mId); - if (mTimeout){ + if (mTimeout) { mSal->cancel_timer(mTimeout); belle_sip_object_unref(mTimeout); - mTimeout = NULL; + mTimeout = nullptr; } mId = 0; } void BackgroundTask::handleTimeout () { - lWarning() << "Background task [" << mId << "] with name : [" << mName << "] is expiring from OS before completion..."; + lWarning() << "Background task [" << mId << "] with name: [" << mName + << "] is expiring from OS before completion..."; stop(); } diff --git a/src/utils/background-task.h b/src/utils/background-task.h index 5aa7edb89..2567a5b6d 100644 --- a/src/utils/background-task.h +++ b/src/utils/background-task.h @@ -34,8 +34,8 @@ class Core; class BackgroundTask { public: - BackgroundTask() : mTimeout(NULL), mSal(NULL), mId(0){} - BackgroundTask (const std::string &name) : mTimeout(NULL), mSal(NULL), mName(name), mId(0) {} + BackgroundTask () {} + BackgroundTask (const std::string &name) {} virtual ~BackgroundTask () { stop(); } @@ -43,26 +43,29 @@ public: void setName (const std::string &name) { mName = name; } - /** - * Start a long running task for at most max_duration_seconds, after which it is automatically terminated - */ - void start(const std::shared_ptr &core, int max_duration_seconds = 15*60); /*15 mn by default, like on iOS*/ - void stop(); const std::string &getName () const { return mName; } + + /** + * Start a long running task for at most max_duration_seconds, after which it is automatically terminated + */ + void start (const std::shared_ptr &core, int maxDurationSeconds = 15 * 60); // 15 min by default, like on iOS + void stop (); + protected: virtual void handleTimeout (); private: static int sHandleSalTimeout(void *data, unsigned int events); static void sHandleTimeout(void *data); - belle_sip_source_t *mTimeout; - Sal *mSal; + void handleSalTimeout(); + + belle_sip_source_t *mTimeout = nullptr; + Sal *mSal = nullptr; std::string mName; unsigned long mId = 0; - void handleSalTimeout(); }; LINPHONE_END_NAMESPACE