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

http://mobicents.googlecode.com/ · Java · 183 lines · 125 code · 34 blank · 24 comment · 11 complexity · a21ec05216e55abf7073fdeb27d60aa2 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.Notify;
  26. import jain.protocol.ip.mgcp.message.NotifyResponse;
  27. import jain.protocol.ip.mgcp.message.parms.EndpointIdentifier;
  28. import jain.protocol.ip.mgcp.message.parms.EventName;
  29. import jain.protocol.ip.mgcp.message.parms.RequestIdentifier;
  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. public class NotifyHandler extends TransactionHandler {
  39. private Notify command;
  40. private NotifyResponse response;
  41. private static final Logger logger = Logger.getLogger(NotifyHandler.class);
  42. public NotifyHandler(JainMgcpStackImpl stack) {
  43. super(stack);
  44. }
  45. public NotifyHandler(JainMgcpStackImpl stack, InetAddress address, int port) {
  46. super(stack, address, port);
  47. }
  48. @Override
  49. public JainMgcpCommandEvent decodeCommand(String message) throws ParseException {
  50. Utils utils = utilsFactory.allocate();
  51. MgcpMessageParser parser = new MgcpMessageParser(new CommandContentHandle(utils));
  52. try {
  53. parser.parse(message);
  54. } catch (Exception e) {
  55. throw new ParseException(e.getMessage(), -1);
  56. } finally {
  57. utilsFactory.deallocate(utils);
  58. }
  59. return command;
  60. }
  61. @Override
  62. public JainMgcpResponseEvent decodeResponse(String message) throws ParseException {
  63. Utils utils = utilsFactory.allocate();
  64. MgcpMessageParser parser = new MgcpMessageParser(new ResponseContentHandle(utils));
  65. try {
  66. parser.parse(message);
  67. } catch (IOException e) {
  68. logger.error("Something wrong while parsing the NOTIFY Response received", e);
  69. } finally {
  70. utilsFactory.deallocate(utils);
  71. }
  72. return response;
  73. }
  74. @Override
  75. public String encode(JainMgcpCommandEvent event) {
  76. Notify notify = (Notify) event;
  77. StringBuffer message = new StringBuffer();
  78. message.append("NTFY ").append(event.getTransactionHandle()).append(SINGLE_CHAR_SPACE).append(
  79. notify.getEndpointIdentifier()).append(MGCP_VERSION).append(NEW_LINE);
  80. if (notify.getNotifiedEntity() != null) {
  81. message.append("N: ").append(notify.getNotifiedEntity()).append(NEW_LINE);
  82. }
  83. message.append("X: ").append(notify.getRequestIdentifier()).append(NEW_LINE);
  84. Utils utils = utilsFactory.allocate();
  85. message.append("O: ").append(utils.encodeEventNames(notify.getObservedEvents())).append(NEW_LINE);
  86. utilsFactory.deallocate(utils);
  87. return message.toString();
  88. }
  89. @Override
  90. public String encode(JainMgcpResponseEvent event) {
  91. StringBuffer s = new StringBuffer();
  92. s.append(event.getReturnCode().getValue()).append(SINGLE_CHAR_SPACE).append(event.getTransactionHandle())
  93. .append(SINGLE_CHAR_SPACE).append(event.getReturnCode().getComment()).append(NEW_LINE);
  94. return s.toString();
  95. // return event.getReturnCode().getValue() + " " +
  96. // event.getTransactionHandle() + " "
  97. // + event.getReturnCode().getComment() + "\n";
  98. }
  99. private class CommandContentHandle implements MgcpContentHandler {
  100. private Utils utils = null;
  101. public CommandContentHandle(Utils utils) {
  102. this.utils = utils;
  103. }
  104. public void header(String header) throws ParseException {
  105. command = new Notify(source != null ? source : stack, endpoint, new RequestIdentifier("0"), new EventName[] {});
  106. command.setTransactionHandle(remoteTID);
  107. }
  108. public void param(String name, String value) throws ParseException {
  109. if (name.equalsIgnoreCase("N")) {
  110. command.setNotifiedEntity(utils.decodeNotifiedEntity(value, false));
  111. } else if (name.equalsIgnoreCase("X")) {
  112. command.setRequestIdentifier(new RequestIdentifier(value));
  113. } else if (name.equalsIgnoreCase("O")) {
  114. command.setObservedEvents(utils.decodeEventNames(value));
  115. }
  116. }
  117. public void sessionDescription(String sd) throws ParseException {
  118. throw new UnsupportedOperationException("Not supported yet.");
  119. }
  120. }
  121. private class ResponseContentHandle implements MgcpContentHandler {
  122. private Utils utils;
  123. public ResponseContentHandle(Utils utils) {
  124. this.utils = utils;
  125. }
  126. public void header(String header) throws ParseException {
  127. String[] tokens = utils.splitStringBySpace(header);
  128. int tid = Integer.parseInt(tokens[1]);
  129. response = new NotifyResponse(source != null ? source : stack, utils.decodeReturnCode(Integer
  130. .parseInt(tokens[0])));
  131. response.setTransactionHandle(tid);
  132. }
  133. public void param(String name, String value) throws ParseException {
  134. throw new UnsupportedOperationException("Not supported yet.");
  135. }
  136. public void sessionDescription(String sd) throws ParseException {
  137. throw new UnsupportedOperationException("Not supported yet.");
  138. }
  139. }
  140. @Override
  141. public JainMgcpResponseEvent getProvisionalResponse() {
  142. NotifyResponse provisionalresponse = null;
  143. if (!sent) {
  144. provisionalresponse = new NotifyResponse(source != null ? source : stack,
  145. ReturnCode.Transaction_Being_Executed);
  146. }
  147. return provisionalresponse;
  148. }
  149. }