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

http://mobicents.googlecode.com/ · Java · 198 lines · 137 code · 31 blank · 30 comment · 17 complexity · 9876fe677952f8e21a8ec22debe2546b 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;
  23. import jain.protocol.ip.mgcp.JainMgcpCommandEvent;
  24. import jain.protocol.ip.mgcp.JainMgcpResponseEvent;
  25. import jain.protocol.ip.mgcp.message.RestartInProgress;
  26. import jain.protocol.ip.mgcp.message.RestartInProgressResponse;
  27. import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
  28. import jain.protocol.ip.mgcp.message.parms.NotifiedEntity;
  29. import jain.protocol.ip.mgcp.message.parms.RestartMethod;
  30. import jain.protocol.ip.mgcp.message.parms.ReturnCode;
  31. import java.io.IOException;
  32. import java.net.InetAddress;
  33. import java.text.ParseException;
  34. import org.apache.log4j.Logger;
  35. import org.mobicents.protocols.mgcp.parser.MgcpContentHandler;
  36. import org.mobicents.protocols.mgcp.parser.MgcpMessageParser;
  37. import org.mobicents.protocols.mgcp.parser.Utils;
  38. /**
  39. * Parse/encode RSIP commands.
  40. *
  41. * @author Tom Uijldert
  42. *
  43. */
  44. public class RestartInProgressHandler extends TransactionHandler {
  45. private static final Logger logger = Logger.getLogger(RestartInProgressHandler.class);
  46. private RestartInProgress command;
  47. private RestartInProgressResponse response;
  48. public RestartInProgressHandler(JainMgcpStackImpl stack) {
  49. super(stack);
  50. }
  51. public RestartInProgressHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
  52. super(stack, address, port);
  53. }
  54. @Override
  55. public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
  56. Utils utils = utilsFactory.allocate();
  57. MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
  58. try {
  59. parser.parse(message);
  60. } catch (Exception e) {
  61. throw new ParseException(e.getMessage(), -1);
  62. } finally {
  63. utilsFactory.deallocate(utils);
  64. }
  65. return command;
  66. }
  67. @Override
  68. public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
  69. Utils utils = utilsFactory.allocate();
  70. MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
  71. try {
  72. parser.parse(message);
  73. } catch (IOException e) {
  74. // should never happen
  75. } finally {
  76. utilsFactory.deallocate(utils);
  77. }
  78. return response;
  79. }
  80. @Override
  81. public String encode(JainMgcpCommandEvent event) {
  82. RestartInProgress rsip = (RestartInProgress) event;
  83. StringBuffer message = new StringBuffer();
  84. message.append("RSIP ").append(event.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
  85. rsip.getEndpointIdentifier()).append(SINGLE_CHAR_SPACE).append(MGCP_VERSION).append(NEW_LINE);
  86. message.append("RM:").append(rsip.getRestartMethod()).append(NEW_LINE);
  87. if (rsip.getRestartDelay() != 0) {
  88. message.append("RD:").append(rsip.getRestartDelay()).append(NEW_LINE);
  89. }
  90. if (rsip.getReasonCode() != null) {
  91. message.append("E:").append(rsip.getReasonCode()).append(NEW_LINE);
  92. }
  93. return message.toString();
  94. }
  95. @Override
  96. public String encode(JainMgcpResponseEvent event) {
  97. RestartInProgressResponse response = (RestartInProgressResponse) event;
  98. ReturnCode returnCode = response.getReturnCode();
  99. StringBuffer s = new StringBuffer();
  100. s.append(returnCode.getValue()).append(SINGLE_CHAR_SPACE).append(response.getTransactionHandle()).append(
  101. SINGLE_CHAR_SPACE).append(returnCode.getComment()).append(NEW_LINE);
  102. // TODO should utils.encodeNotifiedEntity decide on port?
  103. if (response.getNotifiedEntity() != null) {
  104. Utils utils = utilsFactory.allocate();
  105. s.append("N:").append(utils.encodeNotifiedEntity(response.getNotifiedEntity())).append(NEW_LINE);
  106. utilsFactory.deallocate(utils);
  107. }
  108. return s.toString();
  109. // return msg;
  110. }
  111. private class CommandContentHandle implements MgcpContentHandler {
  112. private Utils utils = null;
  113. public CommandContentHandle(Utils utils) {
  114. this.utils = utils;
  115. }
  116. public void header(String header) throws ParseException {
  117. command = new RestartInProgress(source != null ? source : stack, endpoint, RestartMethod.Restart);
  118. command.setTransactionHandle(remoteTID);
  119. }
  120. public void param(String name, String value) throws ParseException {
  121. if (name.equalsIgnoreCase("RM")) {
  122. command.setRestartMethod(utils.decodeRestartMethod(value));
  123. } else if (name.equalsIgnoreCase("RD")) {
  124. command.setRestartDelay(Integer.parseInt(value));
  125. } else if (name.equalsIgnoreCase("E")) {
  126. command.setReasonCode(utils.decodeReasonCode(value));
  127. }
  128. }
  129. public void sessionDescription(String sd) throws ParseException {
  130. throw new UnsupportedOperationException("Not supported yet.");
  131. }
  132. }
  133. private class ResponseContentHandle implements MgcpContentHandler {
  134. private Utils utils;
  135. public ResponseContentHandle(Utils utils) {
  136. this.utils = utils;
  137. }
  138. public void header(String header) throws ParseException {
  139. String[] tokens = utils.splitStringBySpace(header);
  140. int tid = Integer.parseInt(tokens[1]);
  141. response = new RestartInProgressResponse(source != null ? source : stack, utils.decodeReturnCode(Integer
  142. .parseInt(tokens[0])));
  143. response.setTransactionHandle(tid);
  144. }
  145. public void param(String name, String value) throws ParseException {
  146. if (name.equalsIgnoreCase("N")) {
  147. NotifiedEntity n = utils.decodeNotifiedEntity(value, true);
  148. response.setNotifiedEntity(n);
  149. } else {
  150. logger.warn("Unidentified AUCX Response parameter " + name + " with value = " + value);
  151. }
  152. }
  153. public void sessionDescription(String sd) throws ParseException {
  154. throw new UnsupportedOperationException("Not supported yet.");
  155. }
  156. }
  157. @Override
  158. public JainMgcpResponseEvent getProvisionalResponse() {
  159. RestartInProgressResponse provisionalResponse = null;
  160. if (!sent) {
  161. provisionalResponse = new RestartInProgressResponse(source != null ? source : stack,
  162. ReturnCode.Transaction_Being_Executed);
  163. provisionalResponse.setTransactionHandle(remoteTID);
  164. }
  165. return provisionalResponse;
  166. }
  167. }