/*******************************************************************************
 *  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.scp.commons;



import cdac.medinfo.sdk.dcm30_04.exceptions.DicomException;
import cdac.medinfo.sdk.dcm30_04.services.association.AAbort;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumAbortReason;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumAbortSource;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumErrorCode;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.DIMSEResponse;
import cdac.medinfo.sdk.dcm30_04.services.dimsecommons.DicomMessage;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;
import cdac.medinfo.sdk.dcm30_04.utils.DicomLogger;


/**
 * This abstract class in inherited by all SCP.
 */
public abstract class SCP implements IProcessHandler 
{

	/** The object of client session. SCP sends response to this client session. */
	public ClientSession objClientSession = null;

	/**
	 * Instantiates a new SCP.
	 * @param objClientSession the object of client session
	 */
	protected SCP(ClientSession objClientSession)
	{
		this.objClientSession = objClientSession;
	}

	/**
	 * Instantiates a new SCP.
	 */
	protected SCP()
	{
	}

	/**
	 * This method process the Request.
	 * @param objDicomMessage instance of Dicom Message
	 * @return DIMSEResponse instance of Dimse Response.
	 * @throws Exception Signals that an exception has occurred.
	 */
	public abstract DIMSEResponse processRequest(DicomMessage objDicomMessage);
	
	/**
	 * @param objClientSession
	 * @param ex
	 */
	@Override
	public void onException(ClientSession objClientSession,DicomException ex,EnumErrorCode ErrorCode)
	{
		if(DicomConfig.createInstance().isLoggingOn())
		{
			DicomLogger.getLogger().log(ex.getMessage() + " " +ErrorCode.toString());
		}
		switch(ErrorCode)
		{
		case REMOTE_CONNECTION_CLOSED:
		case PDUREADER_CLOSED:
		case SOCKET_TIMEOUT:
			objClientSession.close();
			break;
		case INVALID_COMMANDSET:
			if(objClientSession.getAssociationStatus())
			{
				AAbort objAAbort = new AAbort();
				objAAbort.setReason(EnumAbortReason.INVALID_PDU_PARAM_VALUE);
				objAAbort.setSource(EnumAbortSource.SERVICE_PROVIDER);
				objClientSession.sendAssociationMessage(objAAbort);
			}
			break;
		case INVALID_PDUTYPE:
			if(objClientSession.getAssociationStatus())
			{
				AAbort objAAbort = new AAbort();
				objAAbort.setReason(EnumAbortReason.INVALID_PDU_PARAM_VALUE);
				objAAbort.setSource(EnumAbortSource.SERVICE_PROVIDER);
				objClientSession.sendAssociationMessage(objAAbort);
			}
			break;
		case INVALID_ASSOCIATION_PRIMITIVE:
			if(objClientSession.getAssociationStatus())
			{
				AAbort objAAbort = new AAbort();
				objAAbort.setReason(EnumAbortReason.INVALID_PDU_PARAM_VALUE);
				objAAbort.setSource(EnumAbortSource.SERVICE_PROVIDER);
				objClientSession.sendAssociationMessage(objAAbort);
			}
			break;
		case AGRRED_PDU_LENGTH_VIOLATED:
			if(objClientSession.getAssociationStatus())
			{
				AAbort objAAbort = new AAbort();
				objAAbort.setReason(EnumAbortReason.INVALID_PDU_PARAM_VALUE);
				objAAbort.setSource(EnumAbortSource.SERVICE_PROVIDER);
				objClientSession.sendAssociationMessage(objAAbort);
			}
			break;
		case ASSOCIATION_NOT_EXISTS:
		break;
		case UNEXPECTED_DIMSE_ASSOCIATION:
			break;
		case UNKNOWN:
			if(objClientSession.getAssociationStatus())
			{
				AAbort objAAbort = new AAbort();
				objAAbort.setReason(EnumAbortReason.INVALID_PDU_PARAM_VALUE);
				objAAbort.setSource(EnumAbortSource.SERVICE_PROVIDER);
				objClientSession.sendAssociationMessage(objAAbort);
			}
			objClientSession.close();
			break;
		case INVALID_DICOM_MESSAGE:
			if(objClientSession.getAssociationStatus())
			{
				AAbort objAAbort = new AAbort();
				objAAbort.setReason(EnumAbortReason.INVALID_PDU_PARAM_VALUE);
				objAAbort.setSource(EnumAbortSource.SERVICE_PROVIDER);
				objClientSession.sendAssociationMessage(objAAbort);
			}
			break; 
		}
	}

	

}
