/*******************************************************************************
 *  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 java.io.IOException;


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

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;
import cdac.medinfo.sdk.dcm30_04.utils.ISingleCollection;
import cdac.medinfo.sdk.dcm30_04.utils.SingleCollection;

/**
 * This is the default implementation of the sample test code for C-Store and 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 for multiple services.
 * This class is capable of handling both C-Echo and C-Store request.
 * 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 CServices_SCP_WithoutDB_Def_ProcHandler
{
	/**
	 * The main method.
	 * @param args
	 * @throws IOException Signals if IO Exception occurs.
	 */
	public static void main(String args[])
	{
		try
		{
			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 CServices_SCP.
			 */
			CServices_SCP_Handler objCServices_SCP = new CServices_SCP_Handler();

			/*
			 * Set EnumCommandFiled on ProcessHandler. The procesHandler will process the services whose
			 * command field is set in ProcessHandler. For the request of other services the process handler
			 * will generate Abort.
			 */
			ISingleCollection<EnumCommandField> objEnumCommandField = new SingleCollection<EnumCommandField>();
			objEnumCommandField.add(EnumCommandField.C_STORE_RQ);
			objEnumCommandField.add(EnumCommandField.C_ECHO_RQ);
			objCServices_SCP.setEnumCommandField(objEnumCommandField);

			/*
			 * Start the Server. This can process service requests of all types.
			 */
			objCServices_SCP.start();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}


/**
 * This is master test case for SCP which works as SCP for All Services.
 * This test case extends ProcessHandler class which is default implementation 
 * of SDK to process all kinds of Association and Service Request.
 * Once the required parameters are given to the ProcessHandler and is started , 
 * it is capable of process all Association and service requests and sending their response 
 */
class CServices_SCP_Handler extends ProcessHandler
{
	/**
	 * Starts SCP server at iPort and local host.
	 * @throws IOException Signals IOException if IO operation fails.
	 */
	public void start() throws IOException
	{
		/*
		 * Create SCP session.
		 */
		SCPSession objSCPSession = new SCPSession();

		/*
		 * Initializing selected services on SCPSession
		 */
		EnumServiceType[] arrenumServiceType = new EnumServiceType[2];
		arrenumServiceType[0] = EnumServiceType.CECHO;
		arrenumServiceType[1] = EnumServiceType.CSTORE_DEFAULT;
		objSCPSession.selectServices(arrenumServiceType);

		/*The DataSet is serialized at the Storage Path provided on C-Store SCP.
		 * If No Storage Path is provide on C-Store SCP then File is serialized at "%temp%/CDAC-SDK/DCM-J/DicomDump"
		 * in User Temp Folder.
		 */
		setStoragePath("C:\\StoragePath");

		/*
		 * Set Process handler.
		 */
		objSCPSession.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.
		 */ 
		//objSCPSession.setTimeOut(5000);

		/*
		 * Start the server.
		 */
		objSCPSession.start("SDK",9000);
	} 

}
