/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. package org.mobicents.protocols.mgcp.stack.test.transactionretransmisson;
  23. import jain.protocol.ip.mgcp.JainMgcpCommandEvent;
  24. import jain.protocol.ip.mgcp.JainMgcpEvent;
  25. import jain.protocol.ip.mgcp.JainMgcpResponseEvent;
  26. import jain.protocol.ip.mgcp.message.Constants;
  27. import jain.protocol.ip.mgcp.message.CreateConnection;
  28. import jain.protocol.ip.mgcp.message.DeleteConnection;
  29. import jain.protocol.ip.mgcp.message.ModifyConnection;
  30. import jain.protocol.ip.mgcp.message.NotificationRequest;
  31. import jain.protocol.ip.mgcp.message.Notify;
  32. import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
  33. import jain.protocol.ip.mgcp.message.parms.ConnectionDescriptor;
  34. import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
  35. import jain.protocol.ip.mgcp.message.parms.ConnectionMode;
  36. import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
  37. import jain.protocol.ip.mgcp.message.parms.EventName;
  38. import jain.protocol.ip.mgcp.message.parms.NotifiedEntity;
  39. import jain.protocol.ip.mgcp.message.parms.ReturnCode;
  40. import org.apache.log4j.Logger;
  41. import org.mobicents.protocols.mgcp.stack.JainMgcpExtendedListener;
  42. import org.mobicents.protocols.mgcp.stack.JainMgcpStackProviderImpl;
  43. import org.mobicents.protocols.mgcp.stack.test.createconnection.CreateConnectionTest;
  44. import org.mobicents.protocols.mgcp.stack.test.modifyconnection.ModifyConnectionTest;
  45. import org.mobicents.protocols.mgcp.stack.test.notificationrequest.NotificationRequestTest;
  46. import org.mobicents.protocols.mgcp.stack.test.notify.NotifyTest;
  47. public class CA implements JainMgcpExtendedListener {
  48. private static Logger logger = Logger.getLogger(CA.class);
  49. private JainMgcpStackProviderImpl caProvider;
  50. private int mgStack = 0;
  51. private boolean finalResponseReceived = false;
  52. // The calling application/listener will not receive the provisional response. Hence
  53. // we are setting this to true
  54. private boolean provisionalResponseReceived = false;
  55. private String command;
  56. public CA(JainMgcpStackProviderImpl caProvider, JainMgcpStackProviderImpl mgwProvider) {
  57. this.caProvider = caProvider;
  58. mgStack = mgwProvider.getJainMgcpStack().getPort();
  59. }
  60. public void sendReTransmissionCreateConnection() {
  61. try {
  62. caProvider.addJainMgcpListener(this);
  63. CallIdentifier callID = caProvider.getUniqueCallIdentifier();
  64. EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/$", "127.0.0.1:" + mgStack);
  65. CreateConnection createConnection = new CreateConnection(this, callID, endpointID, ConnectionMode.SendRecv);
  66. String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020" + " IN IP4 127.0.0.1\r\n"
  67. + "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"
  68. + "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n"
  69. + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
  70. createConnection.setRemoteConnectionDescriptor(new ConnectionDescriptor(sdpData));
  71. createConnection.setTransactionHandle(caProvider.getUniqueTransactionHandler());
  72. caProvider.sendMgcpEvents(new JainMgcpEvent[] { createConnection });
  73. logger.debug(" CreateConnection command sent for TxId " + createConnection.getTransactionHandle()
  74. + " and CallId " + callID);
  75. } catch (Exception e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. TxRetransmissionTest.fail("Unexpected Exception");
  79. }
  80. }
  81. public void sendReTransmissionDeleteConnection() {
  82. try {
  83. caProvider.addJainMgcpListener(this);
  84. EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/", "127.0.0.1:" + mgStack);
  85. ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier((caProvider.getUniqueCallIdentifier())
  86. .toString());
  87. DeleteConnection deleteConnection = new DeleteConnection(this, endpointID);
  88. deleteConnection.setConnectionIdentifier(connectionIdentifier);
  89. deleteConnection.setTransactionHandle(caProvider.getUniqueTransactionHandler());
  90. caProvider.sendMgcpEvents(new JainMgcpEvent[] { deleteConnection });
  91. logger.debug(" DeleteConnection command sent for TxId " + deleteConnection.getTransactionHandle()
  92. + " and ConnectionIdentifier " + connectionIdentifier);
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. CreateConnectionTest.fail("Unexpected Exception");
  96. }
  97. }
  98. public void sendReTransmissionModifyConnection() {
  99. try {
  100. caProvider.addJainMgcpListener(this);
  101. CallIdentifier callID = caProvider.getUniqueCallIdentifier();
  102. EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/", "127.0.0.1:" + mgStack);
  103. String identifier = ((CallIdentifier) caProvider.getUniqueCallIdentifier()).toString();
  104. ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier(identifier);
  105. ModifyConnection modifyConnection = new ModifyConnection(this, callID, endpointID, connectionIdentifier);
  106. String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020" + " IN IP4 127.0.0.1\r\n"
  107. + "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"
  108. + "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n"
  109. + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
  110. modifyConnection.setRemoteConnectionDescriptor(new ConnectionDescriptor(sdpData));
  111. modifyConnection.setTransactionHandle(caProvider.getUniqueTransactionHandler());
  112. caProvider.sendMgcpEvents(new JainMgcpEvent[] { modifyConnection });
  113. logger.debug(" ModifyConnection command sent for TxId " + modifyConnection.getTransactionHandle()
  114. + " and CallId " + callID);
  115. } catch (Exception e) {
  116. // TODO Auto-generated catch block
  117. e.printStackTrace();
  118. ModifyConnectionTest.fail("Unexpected Exception");
  119. }
  120. }
  121. public void sendReTransmissionNotificationRequest() {
  122. try {
  123. caProvider.addJainMgcpListener(this);
  124. EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/", "127.0.0.1:" + mgStack);
  125. NotificationRequest notificationRequest = new NotificationRequest(this, endpointID, caProvider
  126. .getUniqueRequestIdentifier());
  127. notificationRequest.setTransactionHandle(caProvider.getUniqueTransactionHandler());
  128. caProvider.sendMgcpEvents(new JainMgcpEvent[] { notificationRequest });
  129. logger.debug(" NotificationRequest command sent for TxId " + notificationRequest.getTransactionHandle());
  130. } catch (Exception e) {
  131. // TODO Auto-generated catch block
  132. e.printStackTrace();
  133. NotificationRequestTest.fail("Unexpected Exception");
  134. }
  135. }
  136. public void sendReTransmissionNotify() {
  137. try {
  138. caProvider.addJainMgcpListener(this);
  139. EndpointIdentifier endpointID = new EndpointIdentifier("media/trunk/Announcement/", "127.0.0.1:" + mgStack);
  140. Notify notify = new Notify(this, endpointID, caProvider.getUniqueRequestIdentifier(), new EventName[] {});
  141. notify.setTransactionHandle(caProvider.getUniqueTransactionHandler());
  142. // TODO We are forced to set the NotifiedEntity, but this should
  143. // happen automatically. Fix this in MGCP Stack
  144. NotifiedEntity notifiedEntity = new NotifiedEntity("127.0.0.1", "127.0.0.1", mgStack);
  145. notify.setNotifiedEntity(notifiedEntity);
  146. caProvider.sendMgcpEvents(new JainMgcpEvent[] { notify });
  147. logger.debug(" Notify command sent for TxId " + notify.getTransactionHandle());
  148. } catch (Exception e) {
  149. // TODO Auto-generated catch block
  150. e.printStackTrace();
  151. NotifyTest.fail("Unexpected Exception");
  152. }
  153. }
  154. public void checkState() {
  155. TxRetransmissionTest.assertTrue("Expect to receive " + command + " Provisional Response",
  156. provisionalResponseReceived);
  157. TxRetransmissionTest.assertTrue("Expect to receive " + command + " Final Response", finalResponseReceived);
  158. }
  159. public void transactionEnded(int handle) {
  160. logger.info("transactionEnded " + handle);
  161. }
  162. public void transactionRxTimedOut(JainMgcpCommandEvent jainMgcpCommandEvent) {
  163. logger.info("transactionRxTimedOut " + jainMgcpCommandEvent);
  164. }
  165. public void transactionTxTimedOut(JainMgcpCommandEvent jainMgcpCommandEvent) {
  166. logger.info("transactionTxTimedOut " + jainMgcpCommandEvent);
  167. }
  168. public void processMgcpCommandEvent(JainMgcpCommandEvent jainmgcpcommandevent) {
  169. logger.info("processMgcpCommandEvent " + jainmgcpcommandevent);
  170. }
  171. public void processMgcpResponseEvent(JainMgcpResponseEvent jainmgcpresponseevent) {
  172. logger.debug("processMgcpResponseEvent = " + jainmgcpresponseevent);
  173. switch (jainmgcpresponseevent.getObjectIdentifier()) {
  174. case Constants.RESP_NOTIFY:
  175. case Constants.RESP_NOTIFICATION_REQUEST:
  176. case Constants.RESP_MODIFY_CONNECTION:
  177. case Constants.RESP_DELETE_CONNECTION:
  178. case Constants.RESP_CREATE_CONNECTION:
  179. if (isProvisional(jainmgcpresponseevent.getReturnCode())) {
  180. provisionalResponseReceived = true;
  181. } else {
  182. finalResponseReceived = true;
  183. }
  184. break;
  185. default:
  186. logger.warn("This RESPONSE is unexpected " + jainmgcpresponseevent);
  187. break;
  188. }
  189. }
  190. private static boolean isProvisional(ReturnCode rc) {
  191. final int rval = rc.getValue();
  192. return ((99 < rval) && (rval < 200));
  193. }
  194. public String getCommand() {
  195. return command;
  196. }
  197. public void setCommand(String command) {
  198. this.command = command;
  199. }
  200. }