/*
* Copyright (C) 2012 Doubango Telecom <http://www.doubango.org>
* License: BSD
* This file is part of Open Source sipML5 solution <http://www.sipml5.org>
*/

tsdp_header_B.prototype = Object.create(tsdp_header.prototype);
%%{
	machine tsdp_machine_parser_header_B;

	# Includes
	include tsdp_machine_utils "./tsdp_machine_utils.jrl";
	
	action tag{
		i_tag_start = p;
	}

	action parse_bwtype{
		hdr_B.s_bwtype = tsk_ragel_parser_get_string(s_str, p, i_tag_start);
	}

	action parse_bandwidth{
		hdr_B.i_bandwidth = tsk_ragel_parser_get_int(s_str, p, i_tag_start);
	}
	
	bwtype = token>tag %parse_bwtype;
	bandwidth = DIGIT+>tag %parse_bandwidth;

	B = 'b' SP* "=" SP*<: bwtype ":" bandwidth;
	
	# Entry point
	main := B :>CRLF?;

}%%

%%write data;

function tsdp_header_B(s_bwtype, i_bandwidth){
	tsdp_header.call(this, tsdp_header_type_e.A);
	this.s_bwtype = s_bwtype;
	this.i_bandwidth = i_bandwidth;
}

tsdp_header_B.prototype.toString = function(){
	return tsk_string_format("{0}:{1}", this.s_bwtype, this.i_bandwidth);
}

tsdp_header_B.prototype.Parse = function(s_str){
	var cs = 0;
	var p = 0;
	var pe = s_str.length;
	var eof = pe;
	var data = tsk_buff_str2ib(s_str);
	var i_tag_start;	
	var hdr_B = new tsdp_header_B(null, 0);
	
	%%write init;
	%%write exec;
	
	if( cs < %%{ write first_final; }%% ){
		tsk_utils_log_error("Failed to parse \"b=\" header: " + s_str);
		return null;
	}
	
	return hdr_B;
}