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

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