package org.mobicents.protocols.ss7.tcap;

import javax.naming.NamingException;

import org.mobicents.protocols.ss7.indicator.RoutingIndicator;
import org.mobicents.protocols.ss7.sccp.SccpProvider;
import org.mobicents.protocols.ss7.sccp.parameter.SccpAddress;
import org.mobicents.protocols.ss7.tcap.api.ComponentPrimitiveFactory;
import org.mobicents.protocols.ss7.tcap.api.TCAPException;
import org.mobicents.protocols.ss7.tcap.api.TCAPProvider;
import org.mobicents.protocols.ss7.tcap.api.TCAPSendException;
import org.mobicents.protocols.ss7.tcap.api.TCAPStack;
import org.mobicents.protocols.ss7.tcap.api.TCListener;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.Dialog;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCBeginIndication;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCBeginRequest;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCContinueIndication;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCEndIndication;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCEndRequest;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCNoticeIndication;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCPAbortIndication;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCUniIndication;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TCUserAbortIndication;
import org.mobicents.protocols.ss7.tcap.api.tc.dialog.events.TerminationType;
import org.mobicents.protocols.ss7.tcap.asn.ApplicationContextName;
import org.mobicents.protocols.ss7.tcap.asn.comp.Invoke;
import org.mobicents.protocols.ss7.tcap.asn.comp.OperationCode;

public class ClientTest implements TCListener {
	// encoded Application Context Name
	public static final long[] _ACN_ = new long[] { 0, 4, 0, 0, 1, 0, 19, 2 };
	private TCAPStack stack;
	private SccpAddress thisAddress;
	private SccpAddress remoteAddress;
	private TCAPProvider tcapProvider;
	private Dialog clientDialog;

	ClientTest(SccpProvider sccpPprovider, SccpAddress thisAddress, SccpAddress remoteAddress) {
		super();
		
		// pass address, so stack can register in SCCP
		this.stack = new TCAPStackImpl(sccpPprovider, 8); 
		this.thisAddress = thisAddress;
		this.remoteAddress = remoteAddress;
		this.tcapProvider = this.stack.getProvider();
		this.tcapProvider.addTCListener(this);
	}

	private static SccpProvider getSccpProvider() throws NamingException {
		// ......
		// ......
		// we need to add code here for getting a active SccpProvider
		return null;  
	}

	public void start() throws TCAPException, TCAPSendException {
		clientDialog = this.tcapProvider.getNewDialog(thisAddress, remoteAddress);
		ComponentPrimitiveFactory cpFactory = this.tcapProvider.getComponentPrimitiveFactory();

		// create some INVOKE
		Invoke invoke = cpFactory.createTCInvokeRequest();
		invoke.setInvokeId(this.clientDialog.getNewInvokeId());
		OperationCode oc = cpFactory.createOperationCode();
		oc.setLocalOperationCode(12L);
		invoke.setOperationCode(oc);
		// no parameter
		this.clientDialog.sendComponent(invoke);
		ApplicationContextName acn = this.tcapProvider.getDialogPrimitiveFactory().createApplicationContextName(_ACN_);
		// UI is optional!
		TCBeginRequest tcbr = this.tcapProvider.getDialogPrimitiveFactory().createBegin(this.clientDialog);
		tcbr.setApplicationContextName(acn);
		this.clientDialog.send(tcbr);
	}

	public void onDialogReleased(Dialog d) {
	}

	public void onInvokeTimeout(Invoke tcInvokeRequest) {
	}

	public void onDialogTimeout(Dialog d) {
		d.keepAlive();
	}

	public void onTCBegin(TCBeginIndication ind) {
	}

	public void onTCContinue(TCContinueIndication ind) {
		// send end
		TCEndRequest end = this.tcapProvider.getDialogPrimitiveFactory().createEnd(ind.getDialog());
		end.setTermination(TerminationType.Basic);
		try {
			ind.getDialog().send(end);
		} catch (TCAPSendException e) {
			throw new RuntimeException(e);
		}
	}

	public void onTCEnd(TCEndIndication ind) {
		// should not happen, in this scenario, we send data.
	}

	public void onTCUni(TCUniIndication ind) {
		// not going to happen
	}

	public void onTCPAbort(TCPAbortIndication ind) {
		// TODO Auto-generated method stub
	}

	public void onTCUserAbort(TCUserAbortIndication ind) {
		// TODO Auto-generated method stub
	}

	public void onTCNotice(TCNoticeIndication ind) {
		// TODO Auto-generated method stub
		
	}

	public static void main(String[] args) {
		SccpAddress localAddress = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_DPC_AND_SSN, 1, null, 8);
		SccpAddress remoteAddress = new SccpAddress(RoutingIndicator.ROUTING_BASED_ON_DPC_AND_SSN, 2, null, 8);
		try {
			ClientTest c = new ClientTest(getSccpProvider(), localAddress, remoteAddress);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

@@ -3160,24 +3160,28 @@
 iveFactory()
+%0A%09%09%09
 .createAppli
@@ -4541,16 +4541,21 @@
 SSN, 1, 
+%0A%09%09%09%09
 null, 8)
@@ -4652,16 +4652,21 @@
 SSN, 2, 
+%0A%09%09%09%09
 null, 8)

Update a TCAP test for a manual