<commit_before>/*
 Flexisip, a flexible SIP proxy server with media capabilities.
 Copyright (C) 2012  Belledonne Communications SARL.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "forkbasiccontext.hh"
#include "registrardb.hh"
#include "common.hh"
#include <algorithm>
#include <sofia-sip/sip_status.h>

using namespace ::std;

ForkBasicContext::ForkBasicContext(Agent *agent, const std::shared_ptr<RequestSipEvent> &event, shared_ptr<ForkContextConfig> cfg, ForkContextListener* listener) :
		ForkContext(agent, event,cfg,listener) {
	LOGD("New ForkBasicContext %p", this);
	mDecisionTimer=NULL;
	//start the acceptance timer immediately
	mDecisionTimer=su_timer_create(su_root_task(mAgent->getRoot()), 0);
	su_timer_set_interval(mDecisionTimer, &ForkBasicContext::sOnDecisionTimer, this, (su_duration_t)20000);
}

ForkBasicContext::~ForkBasicContext() {
	if (mDecisionTimer)
		su_timer_destroy(mDecisionTimer);
	LOGD("Destroy ForkBasicContext %p", this);
}

void ForkBasicContext::onResponse(const shared_ptr<BranchInfo> &br, const shared_ptr<ResponseSipEvent> &event) {
	int code=br->getStatus();
	if (code>=200){
		if (code<300){
			forwardResponse(br);
			if (mDecisionTimer){
				su_timer_destroy(mDecisionTimer);
				mDecisionTimer=NULL;
			}
		}else{
			if (allBranchesAnswered()){
				finishIncomingTransaction();
			}
		}
	}
}

void ForkBasicContext::finishIncomingTransaction(){
	if (mDecisionTimer){
		su_timer_destroy(mDecisionTimer);
		mDecisionTimer=NULL;
	}
	shared_ptr<BranchInfo> best=findBestBranch(sUrgentCodes);
	if (best==NULL) {
		// Create response
		shared_ptr<MsgSip> msgsip(mIncoming->createResponse(SIP_408_REQUEST_TIMEOUT));
		shared_ptr<ResponseSipEvent> ev(new ResponseSipEvent(dynamic_pointer_cast<OutgoingAgent>(mAgent->shared_from_this()), msgsip));
		forwardResponse(ev);
	}else{
		forwardResponse(best);
	}
}

void ForkBasicContext::onDecisionTimer(){
	LOGD("ForkBasicContext::onDecisionTimer()");
	finishIncomingTransaction();
}

void ForkBasicContext::sOnDecisionTimer(su_root_magic_t* magic, su_timer_t* t, su_timer_arg_t* arg){
	static_cast<ForkBasicContext*>(arg)->onDecisionTimer();
}

bool ForkBasicContext::onNewRegister(const url_t *url, const string &uid){
	return false;
}
<commit_msg>fix crash when trying to respond a bit lately to a transaction that expired.<commit_after>/*
 Flexisip, a flexible SIP proxy server with media capabilities.
 Copyright (C) 2012  Belledonne Communications SARL.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "forkbasiccontext.hh"
#include "registrardb.hh"
#include "common.hh"
#include <algorithm>
#include <sofia-sip/sip_status.h>

using namespace ::std;

ForkBasicContext::ForkBasicContext(Agent *agent, const std::shared_ptr<RequestSipEvent> &event, shared_ptr<ForkContextConfig> cfg, ForkContextListener* listener) :
		ForkContext(agent, event,cfg,listener) {
	LOGD("New ForkBasicContext %p", this);
	mDecisionTimer=NULL;
	//start the acceptance timer immediately
	mDecisionTimer=su_timer_create(su_root_task(mAgent->getRoot()), 0);
	su_timer_set_interval(mDecisionTimer, &ForkBasicContext::sOnDecisionTimer, this, (su_duration_t)20000);
}

ForkBasicContext::~ForkBasicContext() {
	if (mDecisionTimer)
		su_timer_destroy(mDecisionTimer);
	LOGD("Destroy ForkBasicContext %p", this);
}

void ForkBasicContext::onResponse(const shared_ptr<BranchInfo> &br, const shared_ptr<ResponseSipEvent> &event) {
	int code=br->getStatus();
	if (code>=200){
		if (code<300){
			forwardResponse(br);
			if (mDecisionTimer){
				su_timer_destroy(mDecisionTimer);
				mDecisionTimer=NULL;
			}
		}else{
			if (allBranchesAnswered()){
				finishIncomingTransaction();
			}
		}
	}
}

void ForkBasicContext::finishIncomingTransaction(){
	if (mDecisionTimer){
		su_timer_destroy(mDecisionTimer);
		mDecisionTimer=NULL;
	}
	shared_ptr<BranchInfo> best=findBestBranch(sUrgentCodes);
	if (best==NULL) {
		// Create response
		shared_ptr<MsgSip> msgsip(mIncoming->createResponse(SIP_408_REQUEST_TIMEOUT));
		if (msgsip){
			shared_ptr<ResponseSipEvent> ev(new ResponseSipEvent(dynamic_pointer_cast<OutgoingAgent>(mAgent->shared_from_this()), msgsip));
			forwardResponse(ev);
		}
	}else{
		forwardResponse(best);
	}
}

void ForkBasicContext::onDecisionTimer(){
	LOGD("ForkBasicContext::onDecisionTimer()");
	finishIncomingTransaction();
}

void ForkBasicContext::sOnDecisionTimer(su_root_magic_t* magic, su_timer_t* t, su_timer_arg_t* arg){
	static_cast<ForkBasicContext*>(arg)->onDecisionTimer();
}

bool ForkBasicContext::onNewRegister(const url_t *url, const string &uid){
	return false;
}

{'fraction_non_alphanumeric': 0.10443366816764808, 'fraction_numerical': 0.006927606511950121, 'mean_word_length': 6.384910485933504, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7451865', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1848, 'n_words': 475}