package org.mobicents.mgcp.stack.test.concurrency; import jain.protocol.ip.mgcp.CreateProviderException; import jain.protocol.ip.mgcp.JainMgcpCommandEvent; import jain.protocol.ip.mgcp.JainMgcpEvent; import jain.protocol.ip.mgcp.JainMgcpListener; import jain.protocol.ip.mgcp.JainMgcpResponseEvent; import jain.protocol.ip.mgcp.message.Constants; import jain.protocol.ip.mgcp.message.CreateConnectionResponse; import jain.protocol.ip.mgcp.message.DeleteConnectionResponse; import jain.protocol.ip.mgcp.message.NotificationRequestResponse; import jain.protocol.ip.mgcp.message.parms.CallIdentifier; import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier; import jain.protocol.ip.mgcp.message.parms.ReturnCode; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.TooManyListenersException; import org.mobicents.mgcp.stack.JainMgcpExtendedListener; import org.mobicents.mgcp.stack.JainMgcpStackImpl; import org.mobicents.mgcp.stack.JainMgcpStackProviderImpl; import org.mobicents.mgcp.stack.test.TestHarness; public class MGW extends TestHarness implements JainMgcpExtendedListener { protected static final String CLIENT_ADDRESS = "127.0.0.1"; protected static final String SERVER_ADDRESS = "127.0.0.1"; protected static final int CA_PORT = 2724; protected static final int MGW_PORT = 2729; static int NDIALOGS = 1000; private int terminationCount = 0; private int timedOut = 0; protected InetAddress mgwIPAddress = null; protected JainMgcpStackImpl mgwStack = null; private JainMgcpStackProviderImpl mgwProvider = null; long start; // ////////////////////////////////// // //// Listeners Method start ///// // ///////////////////////////////// public void transactionEnded(int handle) { // TODO Auto-generated method stub } public void transactionRxTimedOut(JainMgcpCommandEvent command) { this.timedOut ++; System.out.println("Timed Out Transaction "+timedOut); } public void transactionTxTimedOut(JainMgcpCommandEvent command) { this.timedOut ++; System.out.println("Timed Out Transaction "+timedOut); } public void processMgcpCommandEvent(JainMgcpCommandEvent jainmgcpcommandevent) { switch (jainmgcpcommandevent.getObjectIdentifier()) { case Constants.CMD_CREATE_CONNECTION: String identifier = ((CallIdentifier) mgwProvider.getUniqueCallIdentifier()).toString(); ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier(identifier); CreateConnectionResponse response = new CreateConnectionResponse(jainmgcpcommandevent.getSource(), ReturnCode.Transaction_Executed_Normally, connectionIdentifier); response.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle()); response.setSpecificEndpointIdentifier(jainmgcpcommandevent.getEndpointIdentifier()); mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { response }); break; case Constants.CMD_NOTIFICATION_REQUEST: NotificationRequestResponse ntrqResponse = new NotificationRequestResponse( jainmgcpcommandevent.getSource(), ReturnCode.Transaction_Executed_Normally); ntrqResponse.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle()); mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { ntrqResponse }); break; case Constants.CMD_DELETE_CONNECTION: DeleteConnectionResponse dlcxResponse = new DeleteConnectionResponse(jainmgcpcommandevent.getSource(), ReturnCode.Transaction_Executed_Normally); dlcxResponse.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle()); mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { dlcxResponse }); terminationCount++; if(terminationCount % 100 == 0) System.out.println("MGCP cycle termination count = " + terminationCount); break; default: System.out.println("This COMMAND is unexpected " + jainmgcpcommandevent); break; } } public void processMgcpResponseEvent(JainMgcpResponseEvent jainmgcpresponseevent) { // TODO Auto-generated method stub } // ////////////////////////////////// // //// Listeners Method over ////// // ///////////////////////////////// public void createMgcpStack(JainMgcpListener listener) throws UnknownHostException, CreateProviderException, TooManyListenersException { mgwIPAddress = InetAddress.getByName(SERVER_ADDRESS); mgwStack = new JainMgcpStackImpl(mgwIPAddress, MGW_PORT); mgwProvider = (JainMgcpStackProviderImpl) mgwStack.createProvider(); mgwProvider.addJainMgcpListener(listener); } public static void main(String args[]) { final MGW mgw = new MGW(); try { mgw.createMgcpStack(mgw); mgw.start = System.currentTimeMillis(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (CreateProviderException e) { e.printStackTrace(); } catch (TooManyListenersException e) { e.printStackTrace(); } } }