PageRenderTime 60ms CodeModel.GetById 32ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 137 lines | 83 code | 32 blank | 22 comment | 3 complexity | 3a857a84a68e6f538c01df6e03fabf4f 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.createconnection;
 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.parms.CallIdentifier;
 32import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
 33import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
 34import jain.protocol.ip.mgcp.message.parms.ReturnCode;
 35
 36import java.util.TooManyListenersException;
 37
 38import org.apache.log4j.Logger;
 39import org.mobicents.protocols.mgcp.stack.JainMgcpExtendedListener;
 40import org.mobicents.protocols.mgcp.stack.JainMgcpStackProviderImpl;
 41
 42public class MGW implements JainMgcpExtendedListener {
 43
 44	private static Logger logger = Logger.getLogger(MGW.class);
 45	private boolean responseSent = false;
 46
 47	private boolean sendFailedResponse = false;
 48
 49	JainMgcpStackProviderImpl mgwProvider;
 50
 51	public MGW(JainMgcpStackProviderImpl mgwProvider, boolean sendFailedResponse) {
 52		this(mgwProvider);
 53		this.sendFailedResponse = sendFailedResponse;		
 54	}
 55
 56	public MGW(JainMgcpStackProviderImpl mgwProvider) {
 57		this.mgwProvider = mgwProvider;
 58		try {
 59			this.mgwProvider.addJainMgcpListener(this);
 60		} catch (TooManyListenersException e) {
 61			e.printStackTrace();
 62			CreateConnectionTest.fail("Unexpected Exception");
 63		}
 64	}
 65
 66	public void checkState() {
 67		CreateConnectionTest.assertTrue("Expect to sent CRCX Response", responseSent);
 68	}
 69
 70	public void transactionEnded(int handle) {
 71		logger.info("transactionEnded " + handle);
 72
 73	}
 74
 75	public void transactionRxTimedOut(JainMgcpCommandEvent command) {
 76		logger.info("transactionRxTimedOut " + command);
 77
 78	}
 79
 80	public void transactionTxTimedOut(JainMgcpCommandEvent command) {
 81		logger.info("transactionTxTimedOut " + command);
 82
 83	}
 84
 85	public void processMgcpCommandEvent(JainMgcpCommandEvent jainmgcpcommandevent) {
 86		logger.info("processMgcpCommandEvent " + jainmgcpcommandevent);
 87
 88		switch (jainmgcpcommandevent.getObjectIdentifier()) {
 89		case Constants.CMD_CREATE_CONNECTION:
 90			
 91			CreateConnectionResponse response = null;
 92
 93			if (this.sendFailedResponse) {
 94				response = new CreateConnectionResponse(jainmgcpcommandevent.getSource(),
 95						ReturnCode.Endpoint_Unknown, new ConnectionIdentifier("0"));
 96				response.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle());
 97			} else {
 98				String identifier = ((CallIdentifier) mgwProvider.getUniqueCallIdentifier()).toString();
 99				ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier(identifier);
100
101				response = new CreateConnectionResponse(jainmgcpcommandevent.getSource(),
102						ReturnCode.Transaction_Executed_Normally, connectionIdentifier);
103
104				response.setTransactionHandle(jainmgcpcommandevent.getTransactionHandle());
105				try {
106					// FIXME: we asume there is wildcard - "any of"
107					CreateConnection cc = (CreateConnection) jainmgcpcommandevent;
108					EndpointIdentifier wildcard = cc.getEndpointIdentifier();
109					EndpointIdentifier specific = new EndpointIdentifier(wildcard.getLocalEndpointName().replace("$",
110							"")
111							+ "test-1", wildcard.getDomainName());
112					response.setSpecificEndpointIdentifier(specific);
113				} catch (Exception e) {
114					e.printStackTrace();
115				}
116
117			}
118
119			mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { response });
120
121			responseSent = true;
122
123			break;
124		default:
125			logger.warn("This REQUEST is unexpected " + jainmgcpcommandevent);
126			break;
127
128		}
129
130	}
131
132	public void processMgcpResponseEvent(JainMgcpResponseEvent jainmgcpresponseevent) {
133		logger.info("processMgcpResponseEvent " + jainmgcpresponseevent);
134
135	}
136
137}