PageRenderTime 34ms CodeModel.GetById 12ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 238 lines | 150 code | 46 blank | 42 comment | 13 complexity | 5be0fcd3d205c06263903bc4b17ca02b 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.endpointhandler;
 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.CreateConnection;
 29import jain.protocol.ip.mgcp.message.CreateConnectionResponse;
 30import jain.protocol.ip.mgcp.message.DeleteConnection;
 31import jain.protocol.ip.mgcp.message.DeleteConnectionResponse;
 32import jain.protocol.ip.mgcp.message.NotificationRequest;
 33import jain.protocol.ip.mgcp.message.NotificationRequestResponse;
 34import jain.protocol.ip.mgcp.message.Notify;
 35import jain.protocol.ip.mgcp.message.NotifyResponse;
 36import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
 37import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
 38import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
 39import jain.protocol.ip.mgcp.message.parms.EventName;
 40import jain.protocol.ip.mgcp.message.parms.NotifiedEntity;
 41import jain.protocol.ip.mgcp.message.parms.ReturnCode;
 42
 43import java.net.InetAddress;
 44import java.util.TooManyListenersException;
 45
 46import org.apache.log4j.Logger;
 47import org.mobicents.protocols.mgcp.stack.JainMgcpExtendedListener;
 48import org.mobicents.protocols.mgcp.stack.JainMgcpStackProviderImpl;
 49
 50
 51public class MGW implements JainMgcpExtendedListener {
 52
 53	
 54	private static Logger logger = Logger.getLogger(MGW.class);
 55	private boolean responseSent = false;
 56
 57	JainMgcpStackProviderImpl mgwProvider;
 58
 59	private InetAddress localAddress = null;
 60	private int localPort = -1;
 61	private int caPort=-1;
 62
 63	protected EndpointIdentifier specificEndpointId = null;
 64	protected ConnectionIdentifier specificConnectionId = null;
 65	private boolean receivedCCR;
 66	private boolean sentCCResponse;
 67	private boolean receivedNotificationRequest;
 68	private boolean sentNotification;
 69	private boolean sentNotificationRequestResponse;
 70	private boolean receivedNotificatioAnswer;
 71	private boolean sentDLCXA;
 72	private boolean receivedDLCX;
 73	
 74	
 75	public MGW(JainMgcpStackProviderImpl mgwProvider, InetAddress localAddress,int localPort, int caPort) {
 76		this.mgwProvider = mgwProvider;
 77		try {
 78			this.mgwProvider.addJainMgcpListener(this);
 79			this.localAddress = localAddress;
 80			this.localPort = localPort;
 81			this.caPort=caPort;
 82			
 83			
 84			
 85			
 86		} catch (TooManyListenersException e) {
 87			e.printStackTrace();
 88			SimpleFlowTest.fail("Unexpected Exception");
 89		}
 90	}
 91	
 92	/*
 93	 * (non-Javadoc)
 94	 * 
 95	 * @see
 96	 * org.mobicents.mgcp.stack.JainMgcpExtendedListener#transactionEnded(int)
 97	 */
 98	public void transactionEnded(int handle) {
 99		System.err.println("Transaction ended out on = " + localAddress + ":"
100				+ localPort);
101
102	}
103
104	/*
105	 * (non-Javadoc)
106	 * 
107	 * @see
108	 * org.mobicents.mgcp.stack.JainMgcpExtendedListener#transactionRxTimedOut
109	 * (jain.protocol.ip.mgcp.JainMgcpCommandEvent)
110	 */
111	public void transactionRxTimedOut(JainMgcpCommandEvent command) {
112		System.err.println("Transaction Rx timed out on = " + localAddress + ":"
113				+ localPort);
114
115	}
116
117	/*
118	 * (non-Javadoc)
119	 * 
120	 * @see
121	 * org.mobicents.mgcp.stack.JainMgcpExtendedListener#transactionTxTimedOut
122	 * (jain.protocol.ip.mgcp.JainMgcpCommandEvent)
123	 */
124	public void transactionTxTimedOut(JainMgcpCommandEvent command) {
125		System.err.println("Transaction Tx timed out on = " + localAddress + ":"
126				+ localPort);
127
128	}
129
130	public void processMgcpCommandEvent(JainMgcpCommandEvent command) {
131	
132		
133		if(command instanceof CreateConnection)
134		{
135			receivedCCR=true;
136			System.err.println(" - "+localAddress+":"+localPort+" RECEIVE CRCX");
137			String identifier = ((CallIdentifier) mgwProvider.getUniqueCallIdentifier()).toString();
138			ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier(identifier);
139
140			CreateConnectionResponse response = new CreateConnectionResponse(command.getSource(),
141					ReturnCode.Transaction_Executed_Normally, connectionIdentifier);
142
143			response.setTransactionHandle(command.getTransactionHandle());
144			try{
145				CreateConnection cc=(CreateConnection) command;
146				EndpointIdentifier wildcard=cc.getEndpointIdentifier();
147				EndpointIdentifier specific=new EndpointIdentifier(wildcard.getLocalEndpointName().replace("$", "")+"test-1",wildcard.getDomainName());
148				response.setSpecificEndpointIdentifier(specific);
149			}catch(Exception e)
150			{
151				e.printStackTrace();
152			}
153			
154			this.specificConnectionId=connectionIdentifier;
155			this.specificEndpointId=response.getSpecificEndpointIdentifier();
156			System.err.println(" - "+localAddress+":"+localPort+" SENDING CCRespose");
157			mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { response });
158			sentCCResponse=true;
159			
160			
161		}else if(command instanceof NotificationRequest)
162		{
163			
164			System.err.println(" - "+localAddress+":"+localPort+" RECEIVE NotificationRequest");
165			receivedNotificationRequest=true;
166			NotificationRequestResponse response = new NotificationRequestResponse(command.getSource(),
167					ReturnCode.Transaction_Executed_Normally);
168			response.setTransactionHandle(command.getTransactionHandle());
169			System.err.println(" - "+localAddress+":"+localPort+" Sending NotificationreqeustResponse");
170			mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { response });
171			sentNotificationRequestResponse=true;
172			
173			Notify notify = new Notify(this, specificEndpointId, mgwProvider.getUniqueRequestIdentifier(), new EventName[] {});
174			notify.setTransactionHandle(mgwProvider.getUniqueTransactionHandler());
175
176			//TODO We are forced to set the NotifiedEntity, but this should happen automatically. Fix this in MGCP Stack
177			NotifiedEntity notifiedEntity = new NotifiedEntity("127.0.0.1", "127.0.0.1", caPort);
178			notify.setNotifiedEntity(notifiedEntity);
179			System.err.println(" - "+localAddress+":"+localPort+" Sending NOTIFY");
180			mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { notify });
181			sentNotification=true;
182			
183		}else if(command instanceof DeleteConnection)
184		{
185			System.err.println(" - "+localAddress+":"+localPort+" RECEIVE DLCX");
186			receivedDLCX=true;
187			DeleteConnectionResponse response = new DeleteConnectionResponse(command
188					.getSource(), ReturnCode.Transaction_Executed_Normally);
189
190			response.setTransactionHandle(command.getTransactionHandle());
191			System.err.println(" - "+localAddress+":"+localPort+" Sending DLCXresponse");
192			mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { response });
193			sentDLCXA=true;
194		}
195			
196		
197		
198	}
199
200	public void processMgcpResponseEvent(JainMgcpResponseEvent resp) {
201		if(resp instanceof NotifyResponse)
202		{
203			System.err.println(" - "+localAddress+":"+localPort+" RECEIVE NOTIFY Response");
204			receivedNotificatioAnswer=true;
205		}
206
207	}
208
209	public void checkState() {
210		if (receivedCCR && sentCCResponse && receivedNotificationRequest
211				&& sentNotificationRequestResponse && sentNotification
212				&& receivedNotificatioAnswer && receivedDLCX && sentDLCXA) {
213			
214		} else {
215			
216			System.err.println("Receival receivedCCR[" + receivedCCR + "] sentCCResponse["
217					+ sentCCResponse + "] receivedNotificationRequest["
218					+ receivedNotificationRequest
219					+ "] sentNotificationRequestResponse["
220					+ sentNotificationRequestResponse
221					+ "] sentNotification[" + sentNotification
222					+ "] receivedNotificatioAnswer[" + receivedNotificatioAnswer
223					+ "] receivedDLCX[" + receivedDLCX + "] sentDLCXA["
224					+ sentDLCXA + "]");
225			
226			SimpleFlowTest.fail("Receival receivedCCR[" + receivedCCR + "] sentCCResponse["
227					+ sentCCResponse + "] receivedNotificationRequest["
228					+ receivedNotificationRequest
229					+ "] sentNotificationRequestResponse["
230					+ sentNotificationRequestResponse
231					+ "] sentNotification[" + sentNotification
232					+ "] receivedNotificatioAnswer[" + receivedNotificatioAnswer
233					+ "] receivedDLCX[" + receivedDLCX + "] sentDLCXA["
234					+ sentDLCXA + "]");
235		}
236	}
237	
238}