From 6c88e6be876f37eedf0138786d5cdf7a3bf2cd4b Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 8 Feb 2018 12:46:47 +0100 Subject: [PATCH] feat(utils): add a BackgroundTask component --- src/CMakeLists.txt | 2 ++ src/utils/background-task.cpp | 48 +++++++++++++++++++++++++++++++ src/utils/background-task.h | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/utils/background-task.cpp create mode 100644 src/utils/background-task.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 59ddaffc1..6246f1d3e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -144,6 +144,7 @@ set(LINPHONE_CXX_OBJECTS_PRIVATE_HEADER_FILES object/property-container.h object/singleton.h sal/sal.h + utils/background-task.h utils/payload-type-handler.h variant/variant.h xml/conference-info.h @@ -248,6 +249,7 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES sal/refer-op.cpp sal/register-op.cpp sal/sal.cpp + utils/background-task.cpp utils/fs.cpp utils/general.cpp utils/payload-type-handler.cpp diff --git a/src/utils/background-task.cpp b/src/utils/background-task.cpp new file mode 100644 index 000000000..f977c517f --- /dev/null +++ b/src/utils/background-task.cpp @@ -0,0 +1,48 @@ +/* + * background-task.cpp + * Copyright (C) 2010-2018 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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 02110-1301, USA. + */ + +#include "background-task.h" +#include "c-wrapper/internal/c-sal.h" +#include "logger/logger.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +static void notifier (void *context) { + static_cast(context)->handleTimeout(); +} + +void BackgroundTask::start () { + unsigned long newId = sal_begin_background_task(mName.c_str(), notifier, this); + lInfo() << "Starting background task [" << newId << "] with name : [" << mName << "]."; + stop(); + mId = newId; +} + +void BackgroundTask::stop () { + if (mId == 0) + return; + + lInfo() << "Ending background task [" << mId << "] with name : [" << mName << "]."; + sal_end_background_task(mId); + mId = 0; +} + +LINPHONE_END_NAMESPACE diff --git a/src/utils/background-task.h b/src/utils/background-task.h new file mode 100644 index 000000000..cc9f3a3cd --- /dev/null +++ b/src/utils/background-task.h @@ -0,0 +1,54 @@ +/* + * background-task.h + * Copyright (C) 2010-2018 Belledonne Communications SARL + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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 02110-1301, USA. + */ + +#ifndef _L_BACKGROUND_TASK_H_ +#define _L_BACKGROUND_TASK_H_ + +#include + +#include "linphone/utils/general.h" + +// ============================================================================= + +LINPHONE_BEGIN_NAMESPACE + +class BackgroundTask { +public: + BackgroundTask (const std::string &name) : mName(name) {} + virtual ~BackgroundTask () { + stop(); + } + + void start (); + void stop (); + + const std::string &getName () const { + return mName; + } + + virtual void handleTimeout () {} + +private: + std::string mName; + unsigned long mId; +}; + +LINPHONE_END_NAMESPACE + +#endif // ifndef _L_BACKGROUND_TASK_H_