/*******************************************************************************
 *  Copyright 2017-2018 Centre for Development of Advanced Computing(C-DAC), Pune
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package cdac.medinfo.sdk.dcm30_04.services.pdu;

import java.io.IOException;

import cdac.medinfo.sdk.dcm30_04.commoninterface.IDicomStream;
import cdac.medinfo.sdk.dcm30_04.exceptions.ParseException;
import cdac.medinfo.sdk.dcm30_04.exceptions.SerializeException;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumAbortReason;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumAbortSource;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConstants;




/**
 * This class represents A-Abort PDU. 
 * When the A-ABORT service is used, the association shall be released abnormally and
 * simultaneous with the abnormal release of the underlying connection.
 */
public class AAbortPDU extends PDU 
{

	
	/** byte value representing abort source. */
	byte bSource;
	
	/** byte value representing abort reason. */
	byte bReason;
	
	/**
	 * Gets the abort reason.
	 * @return byte - byte value containing the abort reason.
	 */
	public byte getReason() 
	{
		return bReason;
	}
	
	/**
	 * Sets an integer value encoded as an unsigned binary number.
	 * If the Source field has the value (2) DICOM UL service-provider, it shall take one of the following:
	 * 0 - reason-not-specified,
	 * 1 - unrecognized-PDU,
	 * 2 - unexpected-PDU,
	 * 3 - reserved,
	 * 4 - unrecognized-PDU parameter,
	 * 5 - unexpected-PDU parameter,
	 * 6 - invalid-PDU-parameter value
	 * @param enumReason enum value of abort reason.
	 */
	public void setReason(EnumAbortReason enumReason) 
	{
		this.bReason = enumReason.getValue();
	}
	
	/**
	 * Gets the source.
	 * @return byte - Source field containing an integer value encoded as an unsigned binary number.
	 */
	public byte getSource() 
	{
		return bSource;
	}
	
	/**
	 * Sets Source field shall contain an integer value encoded as an unsigned binary number.
	 * One of the following values shall be used:
	 * 0 - DICOM UL service-user (initiated abort),
	 * 1 - reserved,
	 * 2 - DICOM UL service-provider (initiated abort)
	 * @param enumSource enum value of source
	 */
	public void setSource(EnumAbortSource enumSource) 
	{
		this.bSource = enumSource.getValue();
	}
	
	/**
	 * This function returns the length of Abort PDU>
	 * @return int - integer value of Abort PDU.
	 */
	public int getLength() 
	{
		return EnumLength.ABORT_LENGTH.getIntValue();
	}
	
	/**
	 * This function parse the Abort PDU.
	 * @param objIDicomStream dicom stream containing Abort PDU .
	 * @throws IOException Signals that an IO Exception has occurred.
	 * @throws ParseException Signals that an Parse Exception has occurred.
	 */
	public void parse(IDicomStream objIDicomStream) throws IOException, ParseException 
	{
		super.parse(objIDicomStream);
		objIDicomStream.readByte();
		objIDicomStream.readByte();
		bSource = objIDicomStream.readByte();
		bReason = objIDicomStream.readByte();
	}
	
	/**
	 * This function serialize the Abort PDU.
	 * @param objIDicomStream dicom stream to which Abort PDU has to be serialized.
	 * @throws IOException Signals that an IO Exception has occurred.
	 * @throws SerializeException Signals that an Serialize Exception has occurred.
	 */
	public void serialize(IDicomStream objIDicomStream) throws IOException, SerializeException 
	{
		super.serialize(objIDicomStream);
		objIDicomStream.writeByte(DicomConstants.RESERVE_BYTE);
		objIDicomStream.writeByte(DicomConstants.RESERVE_BYTE);
		objIDicomStream.writeByte(bSource);
		objIDicomStream.writeByte(bReason);
	}
}
