PageRenderTime 362ms CodeModel.GetById 344ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 217 lines | 147 code | 44 blank | 26 comment | 3 complexity | f06e58e24dd6bb14bce91248f858cba7 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  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.CreateConnectionResponse;
  29. import jain.protocol.ip.mgcp.message.DeleteConnectionResponse;
  30. import jain.protocol.ip.mgcp.message.ModifyConnectionResponse;
  31. import jain.protocol.ip.mgcp.message.NotificationRequestResponse;
  32. import jain.protocol.ip.mgcp.message.NotifyResponse;
  33. import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
  34. import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
  35. import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
  36. import jain.protocol.ip.mgcp.message.parms.ReturnCode;
  37. import java.util.TooManyListenersException;
  38. import org.apache.log4j.Logger;
  39. import org.mobicents.protocols.mgcp.stack.JainMgcpExtendedListener;
  40. import org.mobicents.protocols.mgcp.stack.JainMgcpStackProviderImpl;
  41. public class MGW implements JainMgcpExtendedListener {
  42. private static Logger logger = Logger.getLogger(MGW.class);
  43. private boolean finalResponseSent = false;
  44. private boolean provisionalResponseSent = false;
  45. private String command;
  46. JainMgcpStackProviderImpl mgwProvider;
  47. public MGW(JainMgcpStackProviderImpl mgwProvider) {
  48. this.mgwProvider = mgwProvider;
  49. try {
  50. this.mgwProvider.addJainMgcpListener(this);
  51. } catch (TooManyListenersException e) {
  52. e.printStackTrace();
  53. TxRetransmissionTest.fail("Unexpected Exception");
  54. }
  55. }
  56. public void checkState() {
  57. TxRetransmissionTest.assertTrue("Expect to sent Provisional " + command
  58. + " Response", provisionalResponseSent);
  59. TxRetransmissionTest.assertTrue("Expect to sent Final " + command
  60. + " Response", finalResponseSent);
  61. }
  62. public void transactionEnded(int handle) {
  63. logger.info("transactionEnded " + handle);
  64. }
  65. public void transactionRxTimedOut(JainMgcpCommandEvent jainMgcpCommandEvent) {
  66. logger.info("transactionRxTimedOut " + jainMgcpCommandEvent);
  67. }
  68. public void transactionTxTimedOut(JainMgcpCommandEvent jainMgcpCommandEvent) {
  69. logger.info("transactionTxTimedOut " + jainMgcpCommandEvent);
  70. }
  71. public void processMgcpCommandEvent(
  72. JainMgcpCommandEvent jainmgcpcommandevent) {
  73. logger.info("processMgcpCommandEvent \n" + jainmgcpcommandevent);
  74. switch (jainmgcpcommandevent.getObjectIdentifier()) {
  75. case Constants.CMD_CREATE_CONNECTION:
  76. String identifier = ((CallIdentifier) mgwProvider
  77. .getUniqueCallIdentifier()).toString();
  78. ConnectionIdentifier connectionIdentifier = new ConnectionIdentifier(
  79. identifier);
  80. CreateConnectionResponse createConnectionResponse = new CreateConnectionResponse(
  81. jainmgcpcommandevent.getSource(),
  82. ReturnCode.Transaction_Executed_Normally,
  83. connectionIdentifier);
  84. createConnectionResponse.setTransactionHandle(jainmgcpcommandevent
  85. .getTransactionHandle());
  86. CreateConnection cc = (CreateConnection) jainmgcpcommandevent;
  87. EndpointIdentifier wildcard = cc.getEndpointIdentifier();
  88. String endpointName = wildcard.getLocalEndpointName();
  89. if (endpointName.indexOf("$") != -1) {
  90. endpointName = endpointName.substring(0, endpointName
  91. .indexOf("$"));
  92. }
  93. EndpointIdentifier specific = new EndpointIdentifier(endpointName
  94. + "test-1", wildcard.getDomainName());
  95. createConnectionResponse.setSpecificEndpointIdentifier(specific);
  96. // Let us sleep for 4.5 Sec which will fire the CRCX command again
  97. // from CA.
  98. sleep();
  99. mgwProvider
  100. .sendMgcpEvents(new JainMgcpEvent[] { createConnectionResponse });
  101. finalResponseSent = true;
  102. break;
  103. case Constants.CMD_DELETE_CONNECTION:
  104. DeleteConnectionResponse deleteConnectionResponse = new DeleteConnectionResponse(
  105. jainmgcpcommandevent.getSource(),
  106. ReturnCode.Transaction_Executed_Normally);
  107. deleteConnectionResponse.setTransactionHandle(jainmgcpcommandevent
  108. .getTransactionHandle());
  109. sleep();
  110. mgwProvider
  111. .sendMgcpEvents(new JainMgcpEvent[] { deleteConnectionResponse });
  112. finalResponseSent = true;
  113. break;
  114. case Constants.CMD_MODIFY_CONNECTION:
  115. ModifyConnectionResponse modifyConnectionResponse = new ModifyConnectionResponse(
  116. jainmgcpcommandevent.getSource(),
  117. ReturnCode.Transaction_Executed_Normally);
  118. modifyConnectionResponse.setTransactionHandle(jainmgcpcommandevent
  119. .getTransactionHandle());
  120. sleep();
  121. mgwProvider
  122. .sendMgcpEvents(new JainMgcpEvent[] { modifyConnectionResponse });
  123. finalResponseSent = true;
  124. break;
  125. case Constants.CMD_NOTIFICATION_REQUEST:
  126. NotificationRequestResponse notificationRequestResponse = new NotificationRequestResponse(
  127. jainmgcpcommandevent.getSource(),
  128. ReturnCode.Transaction_Executed_Normally);
  129. notificationRequestResponse
  130. .setTransactionHandle(jainmgcpcommandevent
  131. .getTransactionHandle());
  132. sleep();
  133. mgwProvider
  134. .sendMgcpEvents(new JainMgcpEvent[] { notificationRequestResponse });
  135. finalResponseSent = true;
  136. break;
  137. case Constants.CMD_NOTIFY:
  138. NotifyResponse notifyResponse = new NotifyResponse(
  139. jainmgcpcommandevent.getSource(),
  140. ReturnCode.Transaction_Executed_Normally);
  141. notifyResponse.setTransactionHandle(jainmgcpcommandevent
  142. .getTransactionHandle());
  143. sleep();
  144. mgwProvider.sendMgcpEvents(new JainMgcpEvent[] { notifyResponse });
  145. finalResponseSent = true;
  146. break;
  147. default:
  148. logger.warn("This REQUEST is unexpected " + jainmgcpcommandevent);
  149. break;
  150. }
  151. }
  152. public void processMgcpResponseEvent(
  153. JainMgcpResponseEvent jainmgcpresponseevent) {
  154. logger.info("processMgcpResponseEvent " + jainmgcpresponseevent);
  155. }
  156. public String getCommand() {
  157. return command;
  158. }
  159. public void setCommand(String command) {
  160. this.command = command;
  161. }
  162. private void sleep() {
  163. try {
  164. Thread.sleep(4500);
  165. // Assuming that stack must have sent provisional response. The
  166. // listener is never called for re-transmission of command
  167. provisionalResponseSent = true;
  168. } catch (InterruptedException e) {
  169. // TODO Auto-generated catch block
  170. e.printStackTrace();
  171. }
  172. }
  173. }