PageRenderTime 38ms CodeModel.GetById 18ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 268 lines | 171 code | 68 blank | 29 comment | 4 complexity | c6fff6a784ada2d438bf66ea97b2ef68 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.DeleteConnection;
 31import jain.protocol.ip.mgcp.message.ModifyConnection;
 32import jain.protocol.ip.mgcp.message.NotificationRequest;
 33import jain.protocol.ip.mgcp.message.Notify;
 34import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
 35import jain.protocol.ip.mgcp.message.parms.ConnectionDescriptor;
 36import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
 37import jain.protocol.ip.mgcp.message.parms.ConnectionMode;
 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 org.apache.log4j.Logger;
 44import org.mobicents.protocols.mgcp.stack.JainMgcpExtendedListener;
 45import org.mobicents.protocols.mgcp.stack.JainMgcpStackProviderImpl;
 46import org.mobicents.protocols.mgcp.stack.test.createconnection.CreateConnectionTest;
 47import org.mobicents.protocols.mgcp.stack.test.modifyconnection.ModifyConnectionTest;
 48import org.mobicents.protocols.mgcp.stack.test.notificationrequest.NotificationRequestTest;
 49import org.mobicents.protocols.mgcp.stack.test.notify.NotifyTest;
 50
 51public class CA implements JainMgcpExtendedListener {
 52
 53	private static Logger logger = Logger.getLogger(CA.class);
 54
 55	private JainMgcpStackProviderImpl caProvider;
 56	private int mgStack = 0;
 57	private boolean finalResponseReceived = false;
 58
 59	// The calling application/listener will not receive the provisional response. Hence
 60	// we are setting this to true
 61	private boolean provisionalResponseReceived = false;
 62	private String command;
 63
 64	public CA(JainMgcpStackProviderImpl caProvider, JainMgcpStackProviderImpl mgwProvider) {
 65		this.caProvider = caProvider;
 66		mgStack = mgwProvider.getJainMgcpStack().getPort();
 67	}
 68
 69	public void sendReTransmissionCreateConnection() {
 70
 71		try {
 72			caProvider.addJainMgcpListener(this);
 73
 74			CallIdentifier callID = caProvider.getUniqueCallIdentifier();
 75
 76			EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/$", "127.0.0.1:" + mgStack);
 77
 78			CreateConnection createConnection = new CreateConnection(this, callID, endpointID, ConnectionMode.SendRecv);
 79
 80			String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020" + " IN IP4  127.0.0.1\r\n"
 81					+ "s=mysession session\r\n" + "p=+46 8 52018010\r\n" + "c=IN IP4  127.0.0.1\r\n" + "t=0 0\r\n"
 82					+ "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n"
 83					+ "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
 84
 85			createConnection.setRemoteConnectionDescriptor(new ConnectionDescriptor(sdpData));
 86
 87			createConnection.setTransactionHandle(caProvider.getUniqueTransactionHandler());
 88
 89			caProvider.sendMgcpEvents(new JainMgcpEvent[] { createConnection });
 90
 91			logger.debug(" CreateConnection command sent for TxId " + createConnection.getTransactionHandle()
 92					+ " and CallId " + callID);
 93		} catch (Exception e) {
 94			// TODO Auto-generated catch block
 95			e.printStackTrace();
 96			TxRetransmissionTest.fail("Unexpected Exception");
 97		}
 98	}
 99
100	public void sendReTransmissionDeleteConnection() {
101
102		try {
103			caProvider.addJainMgcpListener(this);
104
105			EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/", "127.0.0.1:" + mgStack);
106
107			ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier((caProvider.getUniqueCallIdentifier())
108					.toString());
109
110			DeleteConnection deleteConnection = new DeleteConnection(this, endpointID);
111			deleteConnection.setConnectionIdentifier(connectionIdentifier);
112			deleteConnection.setTransactionHandle(caProvider.getUniqueTransactionHandler());
113
114			caProvider.sendMgcpEvents(new JainMgcpEvent[] { deleteConnection });
115
116			logger.debug(" DeleteConnection command sent for TxId " + deleteConnection.getTransactionHandle()
117					+ " and ConnectionIdentifier " + connectionIdentifier);
118		} catch (Exception e) {
119			e.printStackTrace();
120			CreateConnectionTest.fail("Unexpected Exception");
121		}
122	}
123
124	public void sendReTransmissionModifyConnection() {
125
126		try {
127			caProvider.addJainMgcpListener(this);
128
129			CallIdentifier callID = caProvider.getUniqueCallIdentifier();
130
131			EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/", "127.0.0.1:" + mgStack);
132
133			String identifier = ((CallIdentifier) caProvider.getUniqueCallIdentifier()).toString();
134			ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier(identifier);
135
136			ModifyConnection modifyConnection = new ModifyConnection(this, callID, endpointID, connectionIdentifier);
137
138			String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020" + " IN IP4  127.0.0.1\r\n"
139					+ "s=mysession session\r\n" + "p=+46 8 52018010\r\n" + "c=IN IP4  127.0.0.1\r\n" + "t=0 0\r\n"
140					+ "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n"
141					+ "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
142
143			modifyConnection.setRemoteConnectionDescriptor(new ConnectionDescriptor(sdpData));
144
145			modifyConnection.setTransactionHandle(caProvider.getUniqueTransactionHandler());
146
147			caProvider.sendMgcpEvents(new JainMgcpEvent[] { modifyConnection });
148
149			logger.debug(" ModifyConnection command sent for TxId " + modifyConnection.getTransactionHandle()
150					+ " and CallId " + callID);
151		} catch (Exception e) {
152			// TODO Auto-generated catch block
153			e.printStackTrace();
154			ModifyConnectionTest.fail("Unexpected Exception");
155		}
156	}
157
158	public void sendReTransmissionNotificationRequest() {
159
160		try {
161			caProvider.addJainMgcpListener(this);
162
163			EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/", "127.0.0.1:" + mgStack);
164
165			NotificationRequest notificationRequest = new NotificationRequest(this, endpointID, caProvider
166					.getUniqueRequestIdentifier());
167			notificationRequest.setTransactionHandle(caProvider.getUniqueTransactionHandler());
168
169			caProvider.sendMgcpEvents(new JainMgcpEvent[] { notificationRequest });
170
171			logger.debug(" NotificationRequest command sent for TxId " + notificationRequest.getTransactionHandle());
172		} catch (Exception e) {
173			// TODO Auto-generated catch block
174			e.printStackTrace();
175			NotificationRequestTest.fail("Unexpected Exception");
176		}
177	}
178
179	public void sendReTransmissionNotify() {
180
181		try {
182			caProvider.addJainMgcpListener(this);
183
184			EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/", "127.0.0.1:" + mgStack);
185
186			Notify notify = new Notify(this, endpointID, caProvider.getUniqueRequestIdentifier(), new EventName[] {});
187			notify.setTransactionHandle(caProvider.getUniqueTransactionHandler());
188
189			// TODO We are forced to set the NotifiedEntity, but this should
190			// happen automatically. Fix this in MGCP Stack
191			NotifiedEntity notifiedEntity = new NotifiedEntity("127.0.0.1", "127.0.0.1", mgStack);
192			notify.setNotifiedEntity(notifiedEntity);
193
194			caProvider.sendMgcpEvents(new JainMgcpEvent[] { notify });
195
196			logger.debug(" Notify command sent for TxId " + notify.getTransactionHandle());
197		} catch (Exception e) {
198			// TODO Auto-generated catch block
199			e.printStackTrace();
200			NotifyTest.fail("Unexpected Exception");
201		}
202	}
203
204	public void checkState() {
205		TxRetransmissionTest.assertTrue("Expect to receive " + command + " Provisional Response",
206				provisionalResponseReceived);
207		TxRetransmissionTest.assertTrue("Expect to receive " + command + " Final Response", finalResponseReceived);
208
209	}
210
211	public void transactionEnded(int handle) {
212		logger.info("transactionEnded " + handle);
213
214	}
215
216	public void transactionRxTimedOut(JainMgcpCommandEvent jainMgcpCommandEvent) {
217		logger.info("transactionRxTimedOut " + jainMgcpCommandEvent);
218
219	}
220
221	public void transactionTxTimedOut(JainMgcpCommandEvent jainMgcpCommandEvent) {
222		logger.info("transactionTxTimedOut " + jainMgcpCommandEvent);
223
224	}
225
226	public void processMgcpCommandEvent(JainMgcpCommandEvent jainmgcpcommandevent) {
227		logger.info("processMgcpCommandEvent " + jainmgcpcommandevent);
228	}
229
230	public void processMgcpResponseEvent(JainMgcpResponseEvent jainmgcpresponseevent) {
231		logger.debug("processMgcpResponseEvent = " + jainmgcpresponseevent);
232		switch (jainmgcpresponseevent.getObjectIdentifier()) {
233
234		case Constants.RESP_NOTIFY:
235		case Constants.RESP_NOTIFICATION_REQUEST:
236		case Constants.RESP_MODIFY_CONNECTION:
237		case Constants.RESP_DELETE_CONNECTION:
238		case Constants.RESP_CREATE_CONNECTION:
239
240			if (isProvisional(jainmgcpresponseevent.getReturnCode())) {
241				provisionalResponseReceived = true;
242			} else {
243				finalResponseReceived = true;
244			}
245
246			break;
247		default:
248			logger.warn("This RESPONSE is unexpected " + jainmgcpresponseevent);
249			break;
250
251		}
252
253	}
254
255	private static boolean isProvisional(ReturnCode rc) {
256		final int rval = rc.getValue();
257		return ((99 < rval) && (rval < 200));
258	}
259
260	public String getCommand() {
261		return command;
262	}
263
264	public void setCommand(String command) {
265		this.command = command;
266	}
267
268}