/*******************************************************************************
 *  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.cservices.withoutdb.testcases.defaultscp;



import cdac.medinfo.sdk.dcm30_04.enums.EnumLoggingLevel;

import cdac.medinfo.sdk.dcm30_04.services.association.DefaultAssociation;
import cdac.medinfo.sdk.dcm30_04.services.dicomnetcommons.EnumServiceType;
import cdac.medinfo.sdk.dcm30_04.services.scp.commons.ProcessHandler;
import cdac.medinfo.sdk.dcm30_04.services.scp.commons.SCPSession;
import cdac.medinfo.sdk.dcm30_04.utils.DicomConfig;

/**
 * This class is to Test C-Echo SCP in Asynchronous Mode. In Asynchronous Mode SCU sends multiple service request
 * to SCP without waiting for the Response. The SCP processes the request and sends the response.
 * This is the default implementation of the test code for C-Echo Services.
 * This sample codes is to show the handling of connection, association and service request 
 * by the default Process Handler Implementation of the SDK.
 * The Sample Test code should be extended from the ProcessHandler class of SDK for DICOM.
 * Then all the connection , Association and service requests are handled by the default implementation
 * of the SDK.
 */
public class CEcho_SCP_Async_Def_ProcHandler 
{
	public static void main(String[] args) throws Exception
	{
		DicomConfig objDicomConfig = DicomConfig.createInstance();

		//enable logging,specify the path of the directory where log files are created.
		//User can specify any directory in place of 'c:\DCM_Logs'.
		//If no directory is defined by user to create logs then it is created in User Temp DIR.
		objDicomConfig.enableLogging(EnumLoggingLevel.INFO, "C:\\DCM_Logs");

		/*
		 * Creates TestCEchoSCP.
		 */
		AsyncCEchoSCPHandler scp = new AsyncCEchoSCPHandler();

		scp.doCEcho();
	}
}

/**
 * This class is to Test C-Echo SCP in Asynchronous Mode. In Asynchronous Mode SCU sends multiple service request
 * to SCP without waiting for the Response. The SCP processes the request and sends the response.
 * This class inherits all the lifecycle methods involved in a communication with client.
 * This class is extended from the ProcessHandler class of the SDK for DICOM.
 * ProcessHandler class is the default processor of SDK for DICOM which is capable
 * of handling connection , association and service requests. It process all the request , 
 * generates response and sends it to SCU.
 * Once the required parameters are set on ProcessHandler class of SDK for DICOM
 * then it will handle all the Association and Service requests.
 */
class AsyncCEchoSCPHandler extends ProcessHandler
{
	public void doCEcho()
	{
		try
		{
			/*Create SCP Session*/ 
			SCPSession session = new SCPSession();

			/*
			 * Initializing selected services on SCPSession
			 */
			EnumServiceType[] arrenumServiceType = new EnumServiceType[1];
			arrenumServiceType[0] = EnumServiceType.CECHO;
			DefaultAssociation objDefaultAssociation = new DefaultAssociation("SDK");
			objDefaultAssociation.loadAssociation(arrenumServiceType);
			objDefaultAssociation.getRQUserInfo().setMaxNumOpsInvoked((short)0);
			objDefaultAssociation.getRQUserInfo().setMaxNumOpsPerformed((short)0);

			session.setDefaultAssociation(objDefaultAssociation);

			//Set Process Handler as the SCP TestCode
			session.setIProcessHandler(this);

			/*
			 * Setting time out as 5000 milli second. It will wait 5000 mili seconds for read and write operation.
			 * If in the time specified (5000 ms) read or write operation is not performed then 
			 * session is time out.
			 */ 
			//session.setTimeOut(5000);
			
			//Start the Session
			session.start("SDK",9000);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
