# This file is part of bish-bosh. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/raphaelcohn/bish-bosh/master/COPYRIGHT. No part of bish-bosh, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of bish-bosh. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/raphaelcohn/bish-bosh/master/COPYRIGHT.


core_usesIn bishbosh/connection error

bishbosh_connection_read_controlpacket_CONNACK()
{
	bishbosh_connection_error_ifRemainingLengthNot CONNACK 2
	
	local connectAcknowledgeFlags
	bishbosh_connection_read_byte_blocking connectAcknowledgeFlags
	
	case $connectAcknowledgeFlags in
		
		0)
			bishbosh_connection_sessionPresent=0
		;;
		
		1)
			bishbosh_connection_sessionPresent=1
		;;
		
		*)
			bishbosh_connection_error_protocolReadControlPacket CONNACK "Field 'connectAcknowledgeFlags' was '$connectAcknowledgeFlags', not a value between 0 to 1 inclusive"
		;;
		
	esac
	
	local connectReturnCode
	bishbosh_connection_read_byte_blocking connectReturnCode
	
	case $connectReturnCode in
		
		0)
			# OK
			:
		;;
		
		1)
			bishbosh_connection_error_connectReturnCode $core_commandLine_exitCode_UNAVAILABLE 'Connection Refused, unacceptable protocol version'
		;;
		
		2)
			bishbosh_connection_error_connectReturnCode $core_commandLine_exitCode_NOUSER 'Connection Refused, identifier rejected'
		;;
		
		3)
			bishbosh_connection_error_connectReturnCode $core_commandLine_exitCode_UNAVAILABLE 'Connection Refused, Server unavailable'
		;;
		
		4)
			bishbosh_connection_error_connectReturnCode $core_commandLine_exitCode_NOPERM 'Connection Refused, bad user name or password'
		;;
		
		5)
			bishbosh_connection_error_connectReturnCode $core_commandLine_exitCode_NOPERM 'Connection Refused,  not authorized'
		;;
		
		*)
			bishbosh_connection_error_protocolReadControlPacket CONNACK "Received reserved connectReturnCode code of $connectReturnCode"
		;;
		
	esac
	
	bishbosh_connection_handler_CONNACK
	
	bishbosh_connection_read_controlpacket_CONNACK_retransmit
}

bishbosh_connection_read_controlpacket_CONNACK_retransmit()
{
	pushd "$bishbosh_connection_sessionRetransmissionPath"
		
		local packetIdentifier
		
		set +f
		for paddedTransmissionIdentifier in *
		do
			set -f
			if [ ! -e "$paddedTransmissionIdentifier" ]; then
				continue
			fi
			
			pushd "$paddedTransmissionIdentifier"
			
				packetIdentifier="$(core_compatibility_basename "$(pwd -P)")"
				
				if [ -f 'message-sent' ]; then
					if [ -f 'pubrec-received' ]; then
						bishbosh_connection_write_rePUBREL
					else
						bishbosh_connection_write_rePUBLISH
					fi
				elif [ -f 'message-received' ]; then
					bishbosh_connection_write_rePUBREC
				elif [ -f 'subscribe' ]; then
					bishbosh_connection_write_reSUBSCRIBE
				elif [ -f 'unsubscribe' ]; then
					bishbosh_connection_write_reUNSUBSCRIBE
				else
					core_message WARN "Can not retransmit packet identifier '$packetIdentifier' - unrecognised kind"
				fi
				
			popd
			
		done
		set -f
		
	popd
}
