PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/protocols/jain-mgcp/stack/src/main/java/org/mobicents/protocols/mgcp/stack/DeleteConnectionHandler.java

http://mobicents.googlecode.com/
Java | 294 lines | 154 code | 50 blank | 90 comment | 36 complexity | 90f582f393af6b4eb640db111452034c 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. /*
  23. * File Name : CreateConnectionHandle.java
  24. *
  25. * The JAIN MGCP API implementaion.
  26. *
  27. * The source code contained in this file is in in the public domain.
  28. * It can be used in any project or product without prior permission,
  29. * license or royalty payments. There is NO WARRANTY OF ANY KIND,
  30. * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, WITHOUT LIMITATION,
  31. * THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  32. * AND DATA ACCURACY. We do not warrant or make any representations
  33. * regarding the use of the software or the results thereof, including
  34. * but not limited to the correctness, accuracy, reliability or
  35. * usefulness of the software.
  36. */
  37. package org.mobicents.protocols.mgcp.stack;
  38. import jain.protocol.ip.mgcp.JainMgcpCommandEvent;
  39. import jain.protocol.ip.mgcp.JainMgcpResponseEvent;
  40. import jain.protocol.ip.mgcp.message.DeleteConnection;
  41. import jain.protocol.ip.mgcp.message.DeleteConnectionResponse;
  42. import jain.protocol.ip.mgcp.message.parms.CallIdentifier;
  43. import jain.protocol.ip.mgcp.message.parms.ConnectionIdentifier;
  44. import jain.protocol.ip.mgcp.message.parms.NotificationRequestParms;
  45. import jain.protocol.ip.mgcp.message.parms.RequestIdentifier;
  46. import jain.protocol.ip.mgcp.message.parms.ReturnCode;
  47. import java.io.IOException;
  48. import java.net.InetAddress;
  49. import java.text.ParseException;
  50. import org.apache.log4j.Logger;
  51. import org.mobicents.protocols.mgcp.parser.MgcpContentHandler;
  52. import org.mobicents.protocols.mgcp.parser.MgcpMessageParser;
  53. import org.mobicents.protocols.mgcp.parser.Utils;
  54. /**
  55. *
  56. * @author Oleg Kulikov
  57. * @author Pavel Mitrenko
  58. */
  59. public class DeleteConnectionHandler extends TransactionHandler {
  60. private DeleteConnection command;
  61. private DeleteConnectionResponse response;
  62. private static final Logger logger = Logger.getLogger(DeleteConnectionHandler.class);
  63. /** Creates a new instance of CreateConnectionHandle */
  64. public DeleteConnectionHandler(JainMgcpStackImpl stack) {
  65. super(stack);
  66. }
  67. public DeleteConnectionHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
  68. super(stack, address, port);
  69. }
  70. public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
  71. Utils utils = utilsFactory.allocate();
  72. MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
  73. try {
  74. parser.parse(message);
  75. } catch (IOException e) {
  76. logger.error("Decode of DLCX command failed", e);
  77. } finally {
  78. utilsFactory.deallocate(utils);
  79. }
  80. return command;
  81. }
  82. public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
  83. Utils utils = utilsFactory.allocate();
  84. MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
  85. try {
  86. parser.parse(message);
  87. } catch (Exception e) {
  88. logger.error("Decode of DLCX Response failed", e);
  89. } finally {
  90. utilsFactory.deallocate(utils);
  91. }
  92. return response;
  93. }
  94. public String encode(JainMgcpCommandEvent event) {
  95. // encode message header
  96. Utils utils = utilsFactory.allocate();
  97. DeleteConnection evt = (DeleteConnection) event;
  98. StringBuffer s = new StringBuffer();
  99. s.append("DLCX ").append(evt.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
  100. evt.getEndpointIdentifier()).append(SINGLE_CHAR_SPACE).append(MGCP_VERSION).append(NEW_LINE);
  101. // encode optional parameters
  102. if (evt.getBearerInformation() != null) {
  103. s.append("B:e:").append(evt.getBearerInformation()).append(NEW_LINE);
  104. }
  105. if (evt.getCallIdentifier() != null) {
  106. s.append("C:").append(evt.getCallIdentifier()).append(NEW_LINE);
  107. }
  108. if (evt.getConnectionIdentifier() != null) {
  109. s.append("I:").append(evt.getConnectionIdentifier()).append(NEW_LINE);
  110. }
  111. if (evt.getConnectionParms() != null) {
  112. s.append("P:").append(utils.encodeConnectionParms(evt.getConnectionParms())).append(NEW_LINE);
  113. }
  114. if (evt.getNotificationRequestParms() != null) {
  115. s.append(utils.encodeNotificationRequestParms(evt.getNotificationRequestParms())).append(NEW_LINE);
  116. }
  117. if (evt.getReasonCode() != null) {
  118. s.append("E:").append(evt.getReasonCode());
  119. }
  120. utilsFactory.deallocate(utils);
  121. return s.toString();
  122. }
  123. public String encode(JainMgcpResponseEvent event) {
  124. DeleteConnectionResponse response = (DeleteConnectionResponse) event;
  125. Utils utils = utilsFactory.allocate();
  126. ReturnCode returnCode = response.getReturnCode();
  127. StringBuffer s = new StringBuffer();
  128. s.append(returnCode.getValue()).append(SINGLE_CHAR_SPACE).append(response.getTransactionHandle()).append(
  129. SINGLE_CHAR_SPACE).append(returnCode.getComment()).append(NEW_LINE);
  130. if (response.getConnectionParms() != null) {
  131. s.append("P:").append(utils.encodeConnectionParms(response.getConnectionParms())).append(NEW_LINE);
  132. }
  133. return s.toString();
  134. }
  135. public class CommandContentHandle implements MgcpContentHandler {
  136. private Utils utils = null;
  137. public CommandContentHandle(Utils utils) {
  138. this.utils = utils;
  139. }
  140. /**
  141. * Receive notification of the header of a message. Parser will call this method to report about header reading.
  142. *
  143. * @param header
  144. * the header from the message.
  145. */
  146. public void header(String header) throws ParseException {
  147. command = new DeleteConnection(source != null ? source : stack, endpoint);
  148. command.setTransactionHandle(remoteTID);
  149. }
  150. /**
  151. * Receive notification of the parameter of a message. Parser will call this method to report about parameter
  152. * reading.
  153. *
  154. * @param name
  155. * the name of the paremeter
  156. * @param value
  157. * the value of the parameter.
  158. */
  159. public void param(String name, String value) throws ParseException {
  160. if (name.equalsIgnoreCase("B")) {
  161. command.setBearerInformation(utils.decodeBearerInformation(value));
  162. } else if (name.equalsIgnoreCase("c")) {
  163. command.setCallIdentifier(new CallIdentifier(value));
  164. } else if (name.equalsIgnoreCase("I")) {
  165. command.setConnectionIdentifier(new ConnectionIdentifier(value));
  166. } else if (name.equalsIgnoreCase("X")) {
  167. command.setNotificationRequestParms(new NotificationRequestParms(new RequestIdentifier(value)));
  168. } else if (name.equalsIgnoreCase("R")) {
  169. command.getNotificationRequestParms().setRequestedEvents(utils.decodeRequestedEventList(value));
  170. } else if (name.equalsIgnoreCase("S")) {
  171. command.getNotificationRequestParms().setSignalRequests(utils.decodeEventNames(value));
  172. } else if (name.equalsIgnoreCase("T")) {
  173. command.getNotificationRequestParms().setDetectEvents(utils.decodeEventNames(value));
  174. } else if (name.equalsIgnoreCase("P")) {
  175. command.setConnectionParms(utils.decodeConnectionParms(value));
  176. } else if (name.equalsIgnoreCase("E")) {
  177. command.setReasonCode(utils.decodeReasonCode(value));
  178. }
  179. }
  180. /**
  181. * Receive notification of the session description. Parser will call this method to report about session
  182. * descriptor reading.
  183. *
  184. * @param sd
  185. * the session description from message.
  186. */
  187. public void sessionDescription(String sd) throws ParseException {
  188. // do nothing
  189. }
  190. }
  191. private class ResponseContentHandle implements MgcpContentHandler {
  192. private Utils utils = null;
  193. public ResponseContentHandle(Utils utils) {
  194. this.utils = utils;
  195. }
  196. /**
  197. * Receive notification of the header of a message. Parser will call this method to report about header reading.
  198. *
  199. * @param header
  200. * the header from the message.
  201. */
  202. public void header(String header) throws ParseException {
  203. String[] tokens = utils.splitStringBySpace(header);
  204. int tid = Integer.parseInt(tokens[1]);
  205. response = new DeleteConnectionResponse(source != null ? source : stack, utils.decodeReturnCode(Integer
  206. .parseInt(tokens[0])));
  207. response.setTransactionHandle(tid);
  208. }
  209. /**
  210. * Receive notification of the parameter of a message. Parser will call this method to report about parameter
  211. * reading.
  212. *
  213. * @param name
  214. * the name of the paremeter
  215. * @param value
  216. * the value of the parameter.
  217. */
  218. public void param(String name, String value) throws ParseException {
  219. if (name.equalsIgnoreCase("P")) {
  220. response.setConnectionParms(utils.decodeConnectionParms(value));
  221. }
  222. }
  223. /**
  224. * Receive notification of the session description. Parser will call this method to report about session
  225. * descriptor reading.
  226. *
  227. * @param sd
  228. * the session description from message.
  229. */
  230. public void sessionDescription(String sd) throws ParseException {
  231. // not used
  232. }
  233. }
  234. @Override
  235. public JainMgcpResponseEvent getProvisionalResponse() {
  236. DeleteConnectionResponse provisionalResponse = null;
  237. if (!sent) {
  238. provisionalResponse = new DeleteConnectionResponse(source != null ? source : stack,
  239. ReturnCode.Transaction_Being_Executed);
  240. provisionalResponse.setTransactionHandle(commandEvent.getTransactionHandle());
  241. }
  242. return provisionalResponse;
  243. }
  244. }