/protocols/jain-mgcp/stack/src/test/java/org/mobicents/protocols/mgcp/stack/test/endpointhandler/CA.java
Java | 331 lines | 209 code | 50 blank | 72 comment | 18 complexity | d6c6b2d26f1d460c91943f364687ac4c 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 23/** 24 * Start time:12:54:42 2008-11-24<br> 25 * Project: mobicents-media-server-controllers<br> 26 * 27 * @author <a href="mailto:baranowb@gmail.com">baranowb - Bartosz Baranowski 28 * </a> 29 * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a> 30 */ 31package org.mobicents.protocols.mgcp.stack.test.endpointhandler; 32 33import jain.protocol.ip.mgcp.JainMgcpCommandEvent; 34import jain.protocol.ip.mgcp.JainMgcpEvent; 35import jain.protocol.ip.mgcp.JainMgcpResponseEvent; 36import jain.protocol.ip.mgcp.message.CreateConnection; 37import jain.protocol.ip.mgcp.message.CreateConnectionResponse; 38import jain.protocol.ip.mgcp.message.DeleteConnection; 39import jain.protocol.ip.mgcp.message.DeleteConnectionResponse; 40import jain.protocol.ip.mgcp.message.NotificationRequest; 41import jain.protocol.ip.mgcp.message.NotificationRequestResponse; 42import jain.protocol.ip.mgcp.message.Notify; 43import jain.protocol.ip.mgcp.message.NotifyResponse; 44import jain.protocol.ip.mgcp.message.parms.CallIdentifier; 45import jain.protocol.ip.mgcp.message.parms.ConnectionDescriptor; 46import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier; 47import jain.protocol.ip.mgcp.message.parms.ConnectionMode; 48import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier; 49import jain.protocol.ip.mgcp.message.parms.EventName; 50import jain.protocol.ip.mgcp.message.parms.NotificationRequestParms; 51import jain.protocol.ip.mgcp.message.parms.NotifiedEntity; 52import jain.protocol.ip.mgcp.message.parms.RequestIdentifier; 53import jain.protocol.ip.mgcp.message.parms.RequestedAction; 54import jain.protocol.ip.mgcp.message.parms.RequestedEvent; 55import jain.protocol.ip.mgcp.message.parms.ReturnCode; 56import jain.protocol.ip.mgcp.pkg.MgcpEvent; 57import jain.protocol.ip.mgcp.pkg.PackageName; 58 59import java.net.InetAddress; 60 61import org.apache.log4j.Logger; 62import org.mobicents.protocols.mgcp.stack.JainMgcpExtendedListener; 63import org.mobicents.protocols.mgcp.stack.JainMgcpStackProviderImpl; 64import org.mobicents.protocols.mgcp.stack.MgcpResponseType; 65 66/** 67 * Start time:12:54:42 2008-11-24<br> 68 * Project: mobicents-media-server-controllers<br> 69 * 70 * @author <a href="mailto:baranowb@gmail.com">baranowb - Bartosz Baranowski 71 * </a> 72 */ 73public class CA implements JainMgcpExtendedListener { 74 75 private static Logger logger = Logger.getLogger(CA.class); 76 77 private JainMgcpStackProviderImpl caProvider; 78 private int mgStack = 0; 79 private InetAddress localAddress = null; 80 private int localPort = -1; 81 82 protected boolean sentCCR, receivedCCResponse, sentNotificationRequest, 83 receiveNotificationRequestResponse, receivedNotification, 84 sentNotificatioAnswer, sentDLCX, receivedDLCXA; 85 86 protected EndpointIdentifier specificEndpointId = null; 87 protected ConnectionIdentifier specificConnectionId = null; 88 89 public CA(JainMgcpStackProviderImpl caProvider, 90 JainMgcpStackProviderImpl mgwProvider, InetAddress localAddress, 91 int localPort) { 92 this.caProvider = caProvider; 93 mgStack = mgwProvider.getJainMgcpStack().getPort(); 94 this.localAddress = localAddress; 95 this.localPort = localPort; 96 } 97 98 public void sendCRCX() { 99 100 try { 101 caProvider.addJainMgcpListener(this); 102 103 CallIdentifier callID = caProvider.getUniqueCallIdentifier(); 104 105 EndpointIdentifier endpointID = new EndpointIdentifier( 106 "media/trunk/Announcement/$", "127.0.0.1:" + mgStack); 107 108 CreateConnection createConnection = new CreateConnection(this, 109 callID, endpointID, ConnectionMode.SendRecv); 110 111 String sdpData = "v=0\r\n" 112 + "o=4855 13760799956958020 13760799956958020" 113 + " IN IP4 127.0.0.1\r\n" + "s=mysession session\r\n" 114 + "p=+46 8 52018010\r\n" + "c=IN IP4 127.0.0.1\r\n" 115 + "t=0 0\r\n" + "m=audio 6022 RTP/AVP 0 4 18\r\n" 116 + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n" 117 + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n"; 118 119 createConnection 120 .setRemoteConnectionDescriptor(new ConnectionDescriptor( 121 sdpData)); 122 123 createConnection.setTransactionHandle(caProvider 124 .getUniqueTransactionHandler()); 125 126 System.err.println(" - "+localAddress+":"+localPort+" SENDING CRCX"); 127 128 caProvider.sendMgcpEvents(new JainMgcpEvent[] { createConnection }); 129 130 logger.debug(" CreateConnection command sent for TxId " 131 + createConnection.getTransactionHandle() + " and CallId " 132 + callID); 133 sentCCR = true; 134 } catch (Exception e) { 135 e.printStackTrace(); 136 SimpleFlowTest.fail("Unexpected error: " + e); 137 } 138 } 139 140 /* 141 * (non-Javadoc) 142 * 143 * @see 144 * org.mobicents.mgcp.stack.JainMgcpExtendedListener#transactionEnded(int) 145 */ 146 public void transactionEnded(int handle) { 147 System.err.println("Transaction ended out on = " + localAddress + ":" 148 + localPort); 149 150 } 151 152 /* 153 * (non-Javadoc) 154 * 155 * @see 156 * org.mobicents.mgcp.stack.JainMgcpExtendedListener#transactionRxTimedOut 157 * (jain.protocol.ip.mgcp.JainMgcpCommandEvent) 158 */ 159 public void transactionRxTimedOut(JainMgcpCommandEvent command) { 160 System.err.println("Transaction Rx timed out on = " + localAddress + ":" 161 + localPort); 162 163 } 164 165 /* 166 * (non-Javadoc) 167 * 168 * @see 169 * org.mobicents.mgcp.stack.JainMgcpExtendedListener#transactionTxTimedOut 170 * (jain.protocol.ip.mgcp.JainMgcpCommandEvent) 171 */ 172 public void transactionTxTimedOut(JainMgcpCommandEvent command) { 173 System.err.println("Transaction Tx timed out on = " + localAddress + ":" 174 + localPort); 175 176 } 177 178 /* 179 * (non-Javadoc) 180 * 181 * @see 182 * jain.protocol.ip.mgcp.JainMgcpListener#processMgcpCommandEvent(jain.protocol 183 * .ip.mgcp.JainMgcpCommandEvent) 184 */ 185 public void processMgcpCommandEvent(JainMgcpCommandEvent command) { 186 if (command instanceof Notify) { 187 receivedNotification = true; 188 System.err.println(" - "+localAddress+":"+localPort+" RECEIVE NOTIFY"); 189 NotifyResponse response = new NotifyResponse(command.getSource(), 190 ReturnCode.Transaction_Executed_Normally); 191 response.setTransactionHandle(command.getTransactionHandle()); 192 caProvider.sendMgcpEvents(new JainMgcpEvent[] { response }); 193 sentNotificatioAnswer = true; 194 195 DeleteConnection deleteConnection = new DeleteConnection(this, 196 this.specificEndpointId); 197 198 deleteConnection.setConnectionIdentifier(this.specificConnectionId); 199 200 deleteConnection.setTransactionHandle(caProvider 201 .getUniqueTransactionHandler()); 202 203 //Lets add NotificationParms 204 NotificationRequestParms parms=new NotificationRequestParms(new RequestIdentifier("1")); 205 deleteConnection.setNotificationRequestParms(parms); 206 207 System.err.println(" - "+localAddress+":"+localPort+" SEND DLCX"); 208 caProvider.sendMgcpEvents(new JainMgcpEvent[] { deleteConnection }); 209 sentDLCX = true; 210 211 } 212 } 213 214 /* 215 * (non-Javadoc) 216 * 217 * @see 218 * jain.protocol.ip.mgcp.JainMgcpListener#processMgcpResponseEvent(jain. 219 * protocol.ip.mgcp.JainMgcpResponseEvent) 220 */ 221 public void processMgcpResponseEvent(JainMgcpResponseEvent response) { 222 223 MgcpResponseType type = MgcpResponseType 224 .getResponseTypeFromCode(response.getReturnCode().getValue()); 225 226 if (response instanceof CreateConnectionResponse) { 227 receivedCCResponse = true; 228 System.err.println(" - "+localAddress+":"+localPort+" RECEIVE CRCXResponse"); 229 switch (type) { 230 case SuccessResponse: 231 // Tx executed properly 232 CreateConnectionResponse event = (CreateConnectionResponse) response; 233 ConnectionIdentifier connectionIdentifier = event 234 .getConnectionIdentifier(); 235 this.specificEndpointId = event.getSpecificEndpointIdentifier(); 236 NotificationRequest notificationRequest = new NotificationRequest( 237 this, specificEndpointId, this.caProvider 238 .getUniqueRequestIdentifier()); 239 240 this.specificConnectionId=connectionIdentifier; 241 this.specificEndpointId=event.getSpecificEndpointIdentifier(); 242 EventName[] signalRequests = { new EventName( 243 PackageName.Announcement, MgcpEvent.ann 244 .withParm("http://tests.ip:8080/test.wav"), 245 connectionIdentifier) }; 246 notificationRequest.setSignalRequests(signalRequests); 247 248 RequestedAction[] actions = new RequestedAction[] { RequestedAction.NotifyImmediately }; 249 250 RequestedEvent[] requestedEvents = { 251 new RequestedEvent(new EventName(PackageName.Dtmf, 252 MgcpEvent.dtmf0, connectionIdentifier), actions), 253 new RequestedEvent(new EventName( 254 PackageName.Announcement, MgcpEvent.of, 255 connectionIdentifier), actions) }; 256 257 notificationRequest.setRequestedEvents(requestedEvents); 258 notificationRequest.setTransactionHandle(caProvider 259 .getUniqueTransactionHandler()); 260 261 NotifiedEntity notifiedEntity = new NotifiedEntity( 262 this.localAddress.toString(), localAddress.toString(), 263 this.localPort); 264 notificationRequest.setNotifiedEntity(notifiedEntity); 265 266 System.err.println(" - "+localAddress+":"+localPort+" SEND NR"); 267 caProvider 268 .sendMgcpEvents(new JainMgcpEvent[] { notificationRequest }); 269 sentNotificationRequest = true; 270 break; 271 case ProvisionalResponse: 272 break; 273 default: 274 SimpleFlowTest.fail("Bad message: " + response); 275 } 276 } else if (response instanceof NotificationRequestResponse) { 277 receiveNotificationRequestResponse = true; 278 System.err.println(" - "+localAddress+":"+localPort+" Receive NRResponse"); 279 switch (type) { 280 case SuccessResponse: 281 282 break; 283 case ProvisionalResponse: 284 break; 285 default: 286 SimpleFlowTest.fail("Bad message: " + response); 287 } 288 } else if (response instanceof DeleteConnectionResponse) { 289 receivedDLCXA = true; 290 switch (type) { 291 case SuccessResponse: 292 293 break; 294 case ProvisionalResponse: 295 break; 296 default: 297 SimpleFlowTest.fail("Bad message: " + response); 298 } 299 } 300 301 } 302 303 public void checkState() { 304 if (sentCCR && receivedCCResponse && sentNotificationRequest 305 && receiveNotificationRequestResponse && receivedNotification 306 && sentNotificatioAnswer && sentDLCX && receivedDLCXA) { 307 308 } else { 309 310 System.err.println("Receival sentCCR[" + sentCCR + "] receivedCCResponse[" 311 + receivedCCResponse + "] sentNotificationRequest[" 312 + sentNotificationRequest 313 + "] receiveNotificationRequestResponse[" 314 + receiveNotificationRequestResponse 315 + "] receivedNotification[" + receivedNotification 316 + "] sentNotificatioAnswer[" + sentNotificatioAnswer 317 + "] sentDLCX[" + sentDLCX + "] receivedDLCXA[" 318 + receivedDLCXA + "]"); 319 SimpleFlowTest.fail("Receival sentCCR[" + sentCCR + "] receivedCCResponse[" 320 + receivedCCResponse + "] sentNotificationRequest[" 321 + sentNotificationRequest 322 + "] receiveNotificationRequestResponse[" 323 + receiveNotificationRequestResponse 324 + "] receivedNotification[" + receivedNotification 325 + "] sentNotificatioAnswer[" + sentNotificatioAnswer 326 + "] sentDLCX[" + sentDLCX + "] receivedDLCXA[" 327 + receivedDLCXA + "]"); 328 } 329 } 330 331}