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