PageRenderTime 48ms CodeModel.GetById 14ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 1ms

/protocols/jain-mgcp/stack/src/test/java/org/mobicents/protocols/mgcp/stack/test/concurrency/MGW.java

http://mobicents.googlecode.com/
Java | 177 lines | 98 code | 47 blank | 32 comment | 3 complexity | 8fb756232881df1a805c1ae9cc49c08d MD5 | raw file
  1/*
  2 * JBoss, Home of Professional Open Source
  3 * Copyright 2011, Red Hat, Inc. and individual contributors
  4 * by the @authors tag. See the copyright.txt in the distribution for a
  5 * full listing of individual contributors.
  6 *
  7 * This is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU Lesser General Public License as
  9 * published by the Free Software Foundation; either version 2.1 of
 10 * the License, or (at your option) any later version.
 11 *
 12 * This software is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15 * Lesser General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU Lesser General Public
 18 * License along with this software; if not, write to the Free
 19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21 */
 22
 23package org.mobicents.protocols.mgcp.stack.test.concurrency;
 24
 25import jain.protocol.ip.mgcp.CreateProviderException;
 26import jain.protocol.ip.mgcp.JainMgcpCommandEvent;
 27import jain.protocol.ip.mgcp.JainMgcpEvent;
 28import jain.protocol.ip.mgcp.JainMgcpListener;
 29import jain.protocol.ip.mgcp.JainMgcpResponseEvent;
 30import jain.protocol.ip.mgcp.message.Constants;
 31import jain.protocol.ip.mgcp.message.CreateConnectionResponse;
 32import jain.protocol.ip.mgcp.message.DeleteConnectionResponse;
 33import jain.protocol.ip.mgcp.message.NotificationRequestResponse;
 34import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
 35import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
 36import jain.protocol.ip.mgcp.message.parms.ReturnCode;
 37
 38import java.net.InetAddress;
 39import java.net.UnknownHostException;
 40import java.util.TooManyListenersException;
 41
 42import org.mobicents.protocols.mgcp.stack.JainMgcpExtendedListener;
 43import org.mobicents.protocols.mgcp.stack.JainMgcpStackImpl;
 44import org.mobicents.protocols.mgcp.stack.JainMgcpStackProviderImpl;
 45import org.mobicents.protocols.mgcp.stack.test.TestHarness;
 46
 47public class MGW extends TestHarness implements JainMgcpExtendedListener {
 48
 49	protected static final String CLIENT_ADDRESS = "127.0.0.1";
 50
 51	protected static final String SERVER_ADDRESS = "127.0.0.1";
 52
 53	protected static final int CA_PORT = 2724;
 54
 55	protected static final int MGW_PORT = 2727;
 56	static int NDIALOGS = 1000;
 57
 58	private int terminationCount = 0;
 59	private int timedOut = 0;
 60
 61	protected InetAddress mgwIPAddress = null;
 62
 63	protected JainMgcpStackImpl mgwStack = null;
 64
 65	private JainMgcpStackProviderImpl mgwProvider = null;
 66
 67	long start;
 68
 69
 70	// //////////////////////////////////
 71	// //// Listeners Method start /////
 72	// /////////////////////////////////
 73
 74	public void transactionEnded(int handle) {
 75		// TODO Auto-generated method stub
 76
 77	}
 78
 79	public void transactionRxTimedOut(JainMgcpCommandEvent command) {
 80		this.timedOut ++;
 81		System.out.println("Timed Out Transaction "+timedOut);
 82	}
 83
 84	public void transactionTxTimedOut(JainMgcpCommandEvent command) {
 85		this.timedOut ++;
 86		System.out.println("Timed Out Transaction "+timedOut);
 87
 88	}
 89
 90	public void processMgcpCommandEvent(JainMgcpCommandEvent jainmgcpcommandevent) {
 91		switch (jainmgcpcommandevent.getObjectIdentifier()) {
 92		case Constants.CMD_CREATE_CONNECTION:
 93
 94			String identifier = ((CallIdentifier) mgwProvider.getUniqueCallIdentifier()).toString();
 95			ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier(identifier);
 96
 97			CreateConnectionResponse response = new CreateConnectionResponse(jainmgcpcommandevent.getSource(),
 98					ReturnCode.Transaction_Executed_Normally, connectionIdentifier);
 99
100			response.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle());
101			response.setSpecificEndpointIdentifier(jainmgcpcommandevent.getEndpointIdentifier());
102			mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { response });
103
104			break;
105
106		case Constants.CMD_NOTIFICATION_REQUEST:
107
108			NotificationRequestResponse ntrqResponse = new NotificationRequestResponse(
109					jainmgcpcommandevent.getSource(), ReturnCode.Transaction_Executed_Normally);
110			ntrqResponse.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle());
111			mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { ntrqResponse });
112
113			break;
114
115		case Constants.CMD_DELETE_CONNECTION:
116
117			DeleteConnectionResponse dlcxResponse = new DeleteConnectionResponse(jainmgcpcommandevent.getSource(),
118					ReturnCode.Transaction_Executed_Normally);
119
120			dlcxResponse.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle());
121
122			mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { dlcxResponse });
123
124			terminationCount++;
125			if(terminationCount % 100 == 0) System.out.println("MGCP cycle termination count = " + terminationCount);
126			
127//			if(terminationCount == 10100){
128//				stop();
129//			}
130			break;
131
132		default:
133			System.out.println("This COMMAND is unexpected " + jainmgcpcommandevent);
134			break;
135
136		}
137
138	}
139
140	public void processMgcpResponseEvent(JainMgcpResponseEvent jainmgcpresponseevent) {
141		// TODO Auto-generated method stub
142
143	}
144	
145	private void stop(){
146		this.mgwStack.close();
147	}
148
149	// //////////////////////////////////
150	// //// Listeners Method over //////
151	// /////////////////////////////////
152
153	public void createMgcpStack(JainMgcpListener listener) throws UnknownHostException, CreateProviderException,
154			TooManyListenersException {
155		mgwIPAddress = InetAddress.getByName(SERVER_ADDRESS);
156		mgwStack = new JainMgcpStackImpl(mgwIPAddress, MGW_PORT);
157		mgwProvider = (JainMgcpStackProviderImpl) mgwStack.createProvider();
158		mgwProvider.addJainMgcpListener(listener);
159	}
160
161	public static void main(String args[]) {
162		final MGW mgw = new MGW();
163		try {
164			mgw.createMgcpStack(mgw);
165			mgw.start = System.currentTimeMillis();
166
167		} catch (UnknownHostException e) {
168			e.printStackTrace();
169		} catch (CreateProviderException e) {
170			e.printStackTrace();
171		} catch (TooManyListenersException e) {
172			e.printStackTrace();
173		}
174
175	}
176
177}