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

http://mobicents.googlecode.com/ · Java · 232 lines · 108 code · 32 blank · 92 comment · 5 complexity · f61e2891165739c5e860069fc18a8385 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. * File Name : EndpointConfigurationHandle.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.EndpointConfiguration;
  41. import jain.protocol.ip.mgcp.message.EndpointConfigurationResponse;
  42. import jain.protocol.ip.mgcp.message.parms.BearerInformation;
  43. import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
  44. import jain.protocol.ip.mgcp.message.parms.ReturnCode;
  45. import java.net.InetAddress;
  46. import java.text.ParseException;
  47. import org.apache.log4j.Logger;
  48. import org.mobicents.protocols.mgcp.parser.MgcpContentHandler;
  49. import org.mobicents.protocols.mgcp.parser.MgcpMessageParser;
  50. import org.mobicents.protocols.mgcp.parser.Utils;
  51. /**
  52. *
  53. * @author Oleg Kulikov
  54. * @author Pavel Mitrenko
  55. */
  56. public class EndpointConfigurationHandler extends TransactionHandler {
  57. private EndpointConfiguration command;
  58. private EndpointConfigurationResponse response;
  59. private static final Logger logger = Logger.getLogger(EndpointConfigurationHandler.class);
  60. /** Creates a new instance of EndpointConfigurationHandle */
  61. public EndpointConfigurationHandler(JainMgcpStackImpl stack) {
  62. super(stack);
  63. }
  64. public EndpointConfigurationHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
  65. super(stack, address, port);
  66. }
  67. public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
  68. Utils utils = utilsFactory.allocate();
  69. MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
  70. try {
  71. parser.parse(message);
  72. } catch (Exception e) {
  73. // should never happen
  74. logger.error("Parsing of EndpointConfiguration command failed", e);
  75. } finally {
  76. utilsFactory.deallocate(utils);
  77. }
  78. return command;
  79. }
  80. public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
  81. Utils utils = utilsFactory.allocate();
  82. MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
  83. try {
  84. parser.parse(message);
  85. } catch (Exception e) {
  86. // should never happen
  87. logger.error("Parsing of EndpointConfiguration Response failed", e);
  88. } finally {
  89. utilsFactory.deallocate(utils);
  90. }
  91. return response;
  92. }
  93. public String encode(JainMgcpCommandEvent event) {
  94. // encode message header
  95. EndpointConfiguration evt = (EndpointConfiguration) event;
  96. StringBuffer s = new StringBuffer();
  97. s.append("EPCF ").append(evt.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
  98. evt.getEndpointIdentifier()).append(MGCP_VERSION).append(NEW_LINE);
  99. // encode mandatory parameters
  100. s.append("B:e:").append(evt.getBearerInformation()).append(NEW_LINE);
  101. return s.toString();
  102. }
  103. public String encode(JainMgcpResponseEvent event) {
  104. EndpointConfigurationResponse response = (EndpointConfigurationResponse) event;
  105. ReturnCode returnCode = response.getReturnCode();
  106. StringBuffer s = new StringBuffer();
  107. s.append(returnCode.getValue()).append(SINGLE_CHAR_SPACE).append(response.getTransactionHandle()).append(
  108. SINGLE_CHAR_SPACE).append(returnCode.getComment()).append(NEW_LINE);
  109. return s.toString();
  110. }
  111. private class CommandContentHandle implements MgcpContentHandler {
  112. private Utils utils = null;
  113. public CommandContentHandle(Utils utils) {
  114. this.utils = utils;
  115. }
  116. /**
  117. * Receive notification of the header of a message. Parser will call
  118. * this method to report about header reading.
  119. *
  120. * @param header
  121. * the header from the message.
  122. */
  123. public void header(String header) throws ParseException {
  124. command = new EndpointConfiguration(source != null ? source : stack, endpoint, BearerInformation.EncMethod_A_Law);
  125. command.setTransactionHandle(remoteTID);
  126. }
  127. /**
  128. * Receive notification of the parameter of a message. Parser will call
  129. * this method to report about parameter reading.
  130. *
  131. * @param name
  132. * the name of the paremeter
  133. * @param value
  134. * the value of the parameter.
  135. */
  136. public void param(String name, String value) throws ParseException {
  137. if (name.equalsIgnoreCase("B")) {
  138. command.setBearerInformation(utils.decodeBearerInformation(value));
  139. }
  140. }
  141. /**
  142. * Receive notification of the session description. Parser will call
  143. * this method to report about session descriptor reading.
  144. *
  145. * @param sd
  146. * the session description from message.
  147. */
  148. public void sessionDescription(String sd) throws ParseException {
  149. }
  150. }
  151. private class ResponseContentHandle implements MgcpContentHandler {
  152. private Utils utils = null;
  153. public ResponseContentHandle(Utils utils) {
  154. this.utils = utils;
  155. }
  156. /**
  157. * Receive notification of the header of a message. Parser will call
  158. * this method to report about header reading.
  159. *
  160. * @param header
  161. * the header from the message.
  162. */
  163. public void header(String header) throws ParseException {
  164. String[] tokens = utils.splitStringBySpace(header);
  165. int tid = Integer.parseInt(tokens[1]);
  166. response = new EndpointConfigurationResponse(source != null ? source : stack, utils
  167. .decodeReturnCode(Integer.parseInt(tokens[0])));
  168. response.setTransactionHandle(tid);
  169. }
  170. /**
  171. * Receive notification of the parameter of a message. Parser will call
  172. * this method to report about parameter reading.
  173. *
  174. * @param name
  175. * the name of the paremeter
  176. * @param value
  177. * the value of the parameter.
  178. */
  179. public void param(String name, String value) throws ParseException {
  180. }
  181. /**
  182. * Receive notification of the session description. Parser will call
  183. * this method to report about session descriptor reading.
  184. *
  185. * @param sd
  186. * the session description from message.
  187. */
  188. public void sessionDescription(String sd) throws ParseException {
  189. }
  190. }
  191. @Override
  192. public JainMgcpResponseEvent getProvisionalResponse() {
  193. EndpointConfigurationResponse provisionalresponse = null;
  194. if (!sent) {
  195. provisionalresponse = new EndpointConfigurationResponse(source != null ? source : stack,
  196. ReturnCode.Transaction_Being_Executed);
  197. }
  198. return provisionalresponse;
  199. }
  200. }