background-task.cpp 2.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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"
23 24
#include "core/core-p.h"
#include "private.h" //to get access to the Sal
25 26 27 28 29

// =============================================================================

LINPHONE_BEGIN_NAMESPACE

30
void BackgroundTask::sHandleTimeout(void *context) {
31 32 33
	static_cast<BackgroundTask *>(context)->handleTimeout();
}

34 35 36 37 38 39 40 41 42 43 44
int BackgroundTask::sHandleSalTimeout(void *data, unsigned int events){
	static_cast<BackgroundTask *>(data)->handleSalTimeout();
	return BELLE_SIP_STOP;
}

void BackgroundTask::handleSalTimeout(){
	lWarning() << "Background task [" << mId << "] with name : [" << mName << "] is now expiring.";
	stop();
}

void BackgroundTask::start (const std::shared_ptr<Core> &core, int max_duration_seconds) {
45 46 47 48 49
	if (mName.empty()) {
		lError() << "No name was set on background task.";
		return;
	}

50 51
	unsigned long newId = sal_begin_background_task(mName.c_str(), sHandleTimeout, this);
	lInfo() << "Starting background task [" << newId << "] with name : [" << mName << "] and expiration of ["<<max_duration_seconds<<"].";
52 53
	stop();
	mId = newId;
54 55 56 57
	if (max_duration_seconds > 0){
		mSal = core->getCCore()->sal;
		mTimeout = mSal->create_timer(sHandleSalTimeout, this, (unsigned int) max_duration_seconds * 1000, mName.c_str());
	}
58 59 60 61 62 63 64 65
}

void BackgroundTask::stop () {
	if (mId == 0)
		return;

	lInfo() << "Ending background task [" << mId << "] with name : [" << mName << "].";
	sal_end_background_task(mId);
66 67 68 69 70
	if (mTimeout){
		mSal->cancel_timer(mTimeout);
		belle_sip_object_unref(mTimeout);
		mTimeout = NULL;
	}
71 72 73
	mId = 0;
}

74
void BackgroundTask::handleTimeout () {
75
	lWarning() << "Background task [" << mId << "] with name : [" << mName << "] is expiring from OS before completion...";
76 77 78
	stop();
}

79
LINPHONE_END_NAMESPACE