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

http://mobicents.googlecode.com/ · Java · 297 lines · 163 code · 41 blank · 93 comment · 39 complexity · c51bf74d068d85257cb177f77df22f91 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 : 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.ModifyConnection;
  41. import jain.protocol.ip.mgcp.message.ModifyConnectionResponse;
  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.EndpointIdentifier;
  46. import jain.protocol.ip.mgcp.message.parms.NotificationRequestParms;
  47. import jain.protocol.ip.mgcp.message.parms.NotifiedEntity;
  48. import jain.protocol.ip.mgcp.message.parms.RequestIdentifier;
  49. import jain.protocol.ip.mgcp.message.parms.ReturnCode;
  50. import java.io.IOException;
  51. import java.net.InetAddress;
  52. import java.text.ParseException;
  53. import org.apache.log4j.Logger;
  54. import org.mobicents.protocols.mgcp.parser.MgcpContentHandler;
  55. import org.mobicents.protocols.mgcp.parser.MgcpMessageParser;
  56. import org.mobicents.protocols.mgcp.parser.Utils;
  57. /**
  58. *
  59. * @author Oleg Kulikov
  60. * @author Pavel Mitrenko
  61. * @author amit bhayani
  62. */
  63. public class ModifyConnectionHandler extends TransactionHandler {
  64. private ModifyConnection command = null;
  65. private ModifyConnectionResponse response = null;
  66. private static final Logger logger = Logger.getLogger(ModifyConnectionHandler.class);
  67. /** Creates a new instance of ModifyConnectionHandler */
  68. public ModifyConnectionHandler(JainMgcpStackImpl stack) {
  69. super(stack);
  70. }
  71. public ModifyConnectionHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
  72. super(stack, address, port);
  73. }
  74. public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
  75. Utils utils = utilsFactory.allocate();
  76. MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
  77. try {
  78. parser.parse(message);
  79. } catch (IOException e) {
  80. logger.error("Decoding of MDCX command failed", e);
  81. } finally {
  82. utilsFactory.deallocate(utils);
  83. }
  84. NotifiedEntity notifiedEntity = command.getNotifiedEntity();
  85. if (command.getNotifiedEntity() != null) {
  86. this.stack.provider.setNotifiedEntity(notifiedEntity);
  87. }
  88. return command;
  89. }
  90. public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
  91. Utils utils = utilsFactory.allocate();
  92. MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
  93. try {
  94. parser.parse(message);
  95. } catch (IOException e) {
  96. logger.error("Decoding of MDCX Response failed", e);
  97. } finally {
  98. utilsFactory.deallocate(utils);
  99. }
  100. return response;
  101. }
  102. public String encode(JainMgcpCommandEvent event) {
  103. // encode message header
  104. Utils utils = utilsFactory.allocate();
  105. ModifyConnection evt = (ModifyConnection) event;
  106. StringBuffer s = new StringBuffer();
  107. s.append("MDCX ").append(evt.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
  108. evt.getEndpointIdentifier()).append(SINGLE_CHAR_SPACE).append(MGCP_VERSION).append(NEW_LINE);
  109. // encode mandatory parameters
  110. s.append("C:").append(evt.getCallIdentifier()).append(NEW_LINE);
  111. s.append("I:").append(evt.getConnectionIdentifier()).append(NEW_LINE);
  112. // encode optional parameters
  113. if (evt.getBearerInformation() != null) {
  114. s.append("B:e:").append(evt.getBearerInformation()).append(NEW_LINE);
  115. }
  116. if (evt.getLocalConnectionOptions() != null) {
  117. s.append("L:").append(utils.encodeLocalOptionValueList(evt.getLocalConnectionOptions())).append(NEW_LINE);
  118. }
  119. if (evt.getMode() != null) {
  120. s.append("M:").append(evt.getMode()).append(NEW_LINE);
  121. }
  122. if (evt.getNotificationRequestParms() != null) {
  123. s.append(utils.encodeNotificationRequestParms(evt.getNotificationRequestParms())).append(NEW_LINE);
  124. }
  125. if (evt.getNotifiedEntity() != null) {
  126. s.append("N:").append(evt.getNotifiedEntity()).append(NEW_LINE);
  127. }
  128. if (evt.getRemoteConnectionDescriptor() != null) {
  129. s.append(NEW_LINE).append(evt.getRemoteConnectionDescriptor());
  130. }
  131. utilsFactory.deallocate(utils);
  132. return s.toString();
  133. }
  134. public String encode(JainMgcpResponseEvent event) {
  135. ModifyConnectionResponse response = (ModifyConnectionResponse) event;
  136. ReturnCode returnCode = response.getReturnCode();
  137. StringBuffer s = new StringBuffer();
  138. s.append(returnCode.getValue()).append(SINGLE_CHAR_SPACE).append(response.getTransactionHandle()).append(
  139. SINGLE_CHAR_SPACE).append(returnCode.getComment()).append(NEW_LINE);
  140. if (response.getLocalConnectionDescriptor() != null) {
  141. s.append(NEW_LINE).append(response.getLocalConnectionDescriptor());
  142. }
  143. return s.toString();
  144. }
  145. private class CommandContentHandle implements MgcpContentHandler {
  146. private Utils utils = null;
  147. public CommandContentHandle(Utils utils) {
  148. this.utils = utils;
  149. }
  150. /**
  151. * Receive notification of the header of a message. Parser will call
  152. * this method to report about header reading.
  153. *
  154. * @param header
  155. * the header from the message.
  156. */
  157. public void header(String header) throws ParseException {
  158. command = new ModifyConnection(source != null ? source : stack, new CallIdentifier("00"), endpoint,
  159. new ConnectionIdentifier("00"));
  160. command.setTransactionHandle(remoteTID);
  161. }
  162. /**
  163. * Receive notification of the parameter of a message. Parser will call
  164. * this method to report about parameter reading.
  165. *
  166. * @param name
  167. * the name of the paremeter
  168. * @param value
  169. * the value of the parameter.
  170. */
  171. public void param(String name, String value) throws ParseException {
  172. if (name.equalsIgnoreCase("B")) {
  173. command.setBearerInformation(utils.decodeBearerInformation(value));
  174. } else if (name.equalsIgnoreCase("c")) {
  175. command.setCallIdentifier(new CallIdentifier(value));
  176. } else if (name.equalsIgnoreCase("I")) {
  177. command.setConnectionIdentifier(new ConnectionIdentifier(value));
  178. } else if (name.equalsIgnoreCase("m")) {
  179. command.setMode(utils.decodeConnectionMode(value));
  180. } else if (name.equalsIgnoreCase("L")) {
  181. command.setLocalConnectionOptions(utils.decodeLocalOptionValueList(value));
  182. } else if (name.equalsIgnoreCase("N")) {
  183. command.setNotifiedEntity(utils.decodeNotifiedEntity(value, true));
  184. } else if (name.equalsIgnoreCase("X")) {
  185. command.setNotificationRequestParms(new NotificationRequestParms(new RequestIdentifier(value)));
  186. } else if (name.equalsIgnoreCase("R")) {
  187. command.getNotificationRequestParms().setRequestedEvents(utils.decodeRequestedEventList(value));
  188. } else if (name.equalsIgnoreCase("S")) {
  189. command.getNotificationRequestParms().setSignalRequests(utils.decodeEventNames(value));
  190. } else if (name.equalsIgnoreCase("T")) {
  191. command.getNotificationRequestParms().setDetectEvents(utils.decodeEventNames(value));
  192. }
  193. }
  194. /**
  195. * Receive notification of the session description. Parser will call
  196. * this method to report about session descriptor reading.
  197. *
  198. * @param sd
  199. * the session description from message.
  200. */
  201. public void sessionDescription(String sd) throws ParseException {
  202. command.setRemoteConnectionDescriptor(new ConnectionDescriptor(sd));
  203. }
  204. }
  205. private class ResponseContentHandle implements MgcpContentHandler {
  206. private Utils utils;
  207. public ResponseContentHandle(Utils utils) {
  208. this.utils = utils;
  209. }
  210. /**
  211. * Receive notification of the header of a message. Parser will call
  212. * this method to report about header reading.
  213. *
  214. * @param header
  215. * the header from the message.
  216. */
  217. public void header(String header) throws ParseException {
  218. String[] tokens = utils.splitStringBySpace(header);
  219. int tid = Integer.parseInt(tokens[1]);
  220. response = new ModifyConnectionResponse(source != null ? source : stack, utils.decodeReturnCode(Integer
  221. .parseInt(tokens[0])));
  222. response.setTransactionHandle(tid);
  223. }
  224. /**
  225. * Receive notification of the parameter of a message. Parser will call
  226. * this method to report about parameter reading.
  227. *
  228. * @param name
  229. * the name of the paremeter
  230. * @param value
  231. * the value of the parameter.
  232. */
  233. public void param(String name, String value) throws ParseException {
  234. // hasn't parameters
  235. }
  236. /**
  237. * Receive notification of the session description. Parser will call
  238. * this method to report about session descriptor reading.
  239. *
  240. * @param sd
  241. * the session description from message.
  242. */
  243. public void sessionDescription(String sd) throws ParseException {
  244. response.setLocalConnectionDescriptor(new ConnectionDescriptor(sd));
  245. }
  246. }
  247. @Override
  248. public JainMgcpResponseEvent getProvisionalResponse() {
  249. ModifyConnectionResponse provisionalresponse = null;
  250. if (!sent) {
  251. provisionalresponse = new ModifyConnectionResponse(source != null ? source : stack,
  252. ReturnCode.Transaction_Being_Executed);
  253. }
  254. return provisionalresponse;
  255. }
  256. }