PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 217 lines | 147 code | 44 blank | 26 comment | 3 complexity | f06e58e24dd6bb14bce91248f858cba7 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.transactionretransmisson;
 24
 25import jain.protocol.ip.mgcp.JainMgcpCommandEvent;
 26import jain.protocol.ip.mgcp.JainMgcpEvent;
 27import jain.protocol.ip.mgcp.JainMgcpResponseEvent;
 28import jain.protocol.ip.mgcp.message.Constants;
 29import jain.protocol.ip.mgcp.message.CreateConnection;
 30import jain.protocol.ip.mgcp.message.CreateConnectionResponse;
 31import jain.protocol.ip.mgcp.message.DeleteConnectionResponse;
 32import jain.protocol.ip.mgcp.message.ModifyConnectionResponse;
 33import jain.protocol.ip.mgcp.message.NotificationRequestResponse;
 34import jain.protocol.ip.mgcp.message.NotifyResponse;
 35import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
 36import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
 37import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
 38import jain.protocol.ip.mgcp.message.parms.ReturnCode;
 39
 40import java.util.TooManyListenersException;
 41
 42import org.apache.log4j.Logger;
 43import org.mobicents.protocols.mgcp.stack.JainMgcpExtendedListener;
 44import org.mobicents.protocols.mgcp.stack.JainMgcpStackProviderImpl;
 45
 46public class MGW implements JainMgcpExtendedListener {
 47
 48	private static Logger logger = Logger.getLogger(MGW.class);
 49	private boolean finalResponseSent = false;
 50	private boolean provisionalResponseSent = false;
 51	private String command;
 52
 53	JainMgcpStackProviderImpl mgwProvider;
 54
 55	public MGW(JainMgcpStackProviderImpl mgwProvider) {
 56		this.mgwProvider = mgwProvider;
 57
 58		try {
 59			this.mgwProvider.addJainMgcpListener(this);
 60		} catch (TooManyListenersException e) {
 61			e.printStackTrace();
 62			TxRetransmissionTest.fail("Unexpected Exception");
 63		}
 64	}
 65
 66	public void checkState() {
 67		TxRetransmissionTest.assertTrue("Expect to sent Provisional " + command
 68				+ " Response", provisionalResponseSent);
 69		TxRetransmissionTest.assertTrue("Expect to sent Final " + command
 70				+ " Response", finalResponseSent);
 71	}
 72
 73	public void transactionEnded(int handle) {
 74		logger.info("transactionEnded " + handle);
 75
 76	}
 77
 78	public void transactionRxTimedOut(JainMgcpCommandEvent jainMgcpCommandEvent) {
 79		logger.info("transactionRxTimedOut " + jainMgcpCommandEvent);
 80
 81	}
 82
 83	public void transactionTxTimedOut(JainMgcpCommandEvent jainMgcpCommandEvent) {
 84		logger.info("transactionTxTimedOut " + jainMgcpCommandEvent);
 85
 86	}
 87
 88	public void processMgcpCommandEvent(
 89			JainMgcpCommandEvent jainmgcpcommandevent) {
 90		logger.info("processMgcpCommandEvent \n" + jainmgcpcommandevent);
 91
 92		switch (jainmgcpcommandevent.getObjectIdentifier()) {
 93		case Constants.CMD_CREATE_CONNECTION:
 94
 95			String identifier = ((CallIdentifier) mgwProvider
 96					.getUniqueCallIdentifier()).toString();
 97			ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier(
 98					identifier);
 99
100			CreateConnectionResponse createConnectionResponse = new CreateConnectionResponse(
101					jainmgcpcommandevent.getSource(),
102					ReturnCode.Transaction_Executed_Normally,
103					connectionIdentifier);
104
105			createConnectionResponse.setTransactionHandle(jainmgcpcommandevent
106					.getTransactionHandle());
107			CreateConnection cc = (CreateConnection) jainmgcpcommandevent;
108			EndpointIdentifier wildcard = cc.getEndpointIdentifier();
109			String endpointName = wildcard.getLocalEndpointName();
110			if (endpointName.indexOf("$") != -1) {
111				endpointName = endpointName.substring(0, endpointName
112						.indexOf("$"));
113			}
114			EndpointIdentifier specific = new EndpointIdentifier(endpointName
115					+ "test-1", wildcard.getDomainName());
116			createConnectionResponse.setSpecificEndpointIdentifier(specific);
117			// Let us sleep for 4.5 Sec which will fire the CRCX command again
118			// from CA.
119			sleep();
120
121			mgwProvider
122					.sendMgcpEvents(new JainMgcpEvent[] { createConnectionResponse });
123
124			finalResponseSent = true;
125
126			break;
127
128		case Constants.CMD_DELETE_CONNECTION:
129
130			DeleteConnectionResponse deleteConnectionResponse = new DeleteConnectionResponse(
131					jainmgcpcommandevent.getSource(),
132					ReturnCode.Transaction_Executed_Normally);
133
134			deleteConnectionResponse.setTransactionHandle(jainmgcpcommandevent
135					.getTransactionHandle());
136			sleep();
137			mgwProvider
138					.sendMgcpEvents(new JainMgcpEvent[] { deleteConnectionResponse });
139
140			finalResponseSent = true;
141
142			break;
143
144		case Constants.CMD_MODIFY_CONNECTION:
145
146			ModifyConnectionResponse modifyConnectionResponse = new ModifyConnectionResponse(
147					jainmgcpcommandevent.getSource(),
148					ReturnCode.Transaction_Executed_Normally);
149			modifyConnectionResponse.setTransactionHandle(jainmgcpcommandevent
150					.getTransactionHandle());
151			sleep();
152			mgwProvider
153					.sendMgcpEvents(new JainMgcpEvent[] { modifyConnectionResponse });
154			finalResponseSent = true;
155			break;
156
157		case Constants.CMD_NOTIFICATION_REQUEST:
158
159			NotificationRequestResponse notificationRequestResponse = new NotificationRequestResponse(
160					jainmgcpcommandevent.getSource(),
161					ReturnCode.Transaction_Executed_Normally);
162			notificationRequestResponse
163					.setTransactionHandle(jainmgcpcommandevent
164							.getTransactionHandle());
165			sleep();
166			mgwProvider
167					.sendMgcpEvents(new JainMgcpEvent[] { notificationRequestResponse });
168			finalResponseSent = true;
169			break;
170
171		case Constants.CMD_NOTIFY:
172
173			NotifyResponse notifyResponse = new NotifyResponse(
174					jainmgcpcommandevent.getSource(),
175					ReturnCode.Transaction_Executed_Normally);
176			notifyResponse.setTransactionHandle(jainmgcpcommandevent
177					.getTransactionHandle());
178			sleep();
179			mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { notifyResponse });
180			finalResponseSent = true;
181			break;
182
183		default:
184			logger.warn("This REQUEST is unexpected " + jainmgcpcommandevent);
185			break;
186
187		}
188
189	}
190
191	public void processMgcpResponseEvent(
192			JainMgcpResponseEvent jainmgcpresponseevent) {
193		logger.info("processMgcpResponseEvent " + jainmgcpresponseevent);
194
195	}
196
197	public String getCommand() {
198		return command;
199	}
200
201	public void setCommand(String command) {
202		this.command = command;
203	}
204
205	private void sleep() {
206		try {
207			Thread.sleep(4500);
208			// Assuming that stack must have sent provisional response. The
209			// listener is never called for re-transmission of command
210			provisionalResponseSent = true;
211		} catch (InterruptedException e) {
212			// TODO Auto-generated catch block
213			e.printStackTrace();
214		}
215	}
216
217}