/protocols/ss7/map/map-impl/src/main/java/org/mobicents/protocols/ss7/map/service/lsm/ResponseTimeImpl.java

http://mobicents.googlecode.com/ · Java · 249 lines · 132 code · 25 blank · 92 comment · 20 complexity · 18b47357cd750f6ee53d02bc8e63cde8 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.ss7.map.service.lsm;
  23. import java.io.IOException;
  24. import org.mobicents.protocols.asn.AsnException;
  25. import org.mobicents.protocols.asn.AsnInputStream;
  26. import org.mobicents.protocols.asn.AsnOutputStream;
  27. import org.mobicents.protocols.asn.Tag;
  28. import org.mobicents.protocols.ss7.map.api.MAPException;
  29. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentException;
  30. import org.mobicents.protocols.ss7.map.api.MAPParsingComponentExceptionReason;
  31. import org.mobicents.protocols.ss7.map.api.service.lsm.ResponseTime;
  32. import org.mobicents.protocols.ss7.map.api.service.lsm.ResponseTimeCategory;
  33. import org.mobicents.protocols.ss7.map.primitives.MAPAsnPrimitive;
  34. /**
  35. * @author amit bhayani
  36. *
  37. */
  38. public class ResponseTimeImpl implements ResponseTime, MAPAsnPrimitive {
  39. private ResponseTimeCategory responseTimeCategory = null;
  40. /**
  41. *
  42. */
  43. public ResponseTimeImpl() {
  44. super();
  45. }
  46. /**
  47. * @param responseTimeCategory
  48. */
  49. public ResponseTimeImpl(ResponseTimeCategory responseTimeCategory) {
  50. super();
  51. this.responseTimeCategory = responseTimeCategory;
  52. }
  53. /*
  54. * (non-Javadoc)
  55. *
  56. * @see org.mobicents.protocols.ss7.map.api.service.lsm.ResponseTime#
  57. * getResponseTimeCategory()
  58. */
  59. @Override
  60. public ResponseTimeCategory getResponseTimeCategory() {
  61. return this.responseTimeCategory;
  62. }
  63. /*
  64. * (non-Javadoc)
  65. *
  66. * @see
  67. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTag()
  68. */
  69. @Override
  70. public int getTag() throws MAPException {
  71. return Tag.SEQUENCE;
  72. }
  73. /*
  74. * (non-Javadoc)
  75. *
  76. * @see
  77. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getTagClass
  78. * ()
  79. */
  80. @Override
  81. public int getTagClass() {
  82. return Tag.CLASS_UNIVERSAL;
  83. }
  84. /*
  85. * (non-Javadoc)
  86. *
  87. * @see
  88. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#getIsPrimitive
  89. * ()
  90. */
  91. @Override
  92. public boolean getIsPrimitive() {
  93. return false;
  94. }
  95. /*
  96. * (non-Javadoc)
  97. *
  98. * @see
  99. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeAll
  100. * (org.mobicents.protocols.asn.AsnInputStream)
  101. */
  102. @Override
  103. public void decodeAll(AsnInputStream ansIS) throws MAPParsingComponentException {
  104. try {
  105. int length = ansIS.readLength();
  106. this._decode(ansIS, length);
  107. } catch (IOException e) {
  108. throw new MAPParsingComponentException("IOException when decoding SM_RP_DA: " + e.getMessage(), e,
  109. MAPParsingComponentExceptionReason.MistypedParameter);
  110. } catch (AsnException e) {
  111. throw new MAPParsingComponentException("AsnException when decoding SM_RP_DA: " + e.getMessage(), e,
  112. MAPParsingComponentExceptionReason.MistypedParameter);
  113. }
  114. }
  115. /*
  116. * (non-Javadoc)
  117. *
  118. * @see
  119. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#decodeData
  120. * (org.mobicents.protocols.asn.AsnInputStream, int)
  121. */
  122. @Override
  123. public void decodeData(AsnInputStream ansIS, int length) throws MAPParsingComponentException {
  124. try {
  125. this._decode(ansIS, length);
  126. } catch (IOException e) {
  127. throw new MAPParsingComponentException("IOException when decoding SM_RP_DA: " + e.getMessage(), e,
  128. MAPParsingComponentExceptionReason.MistypedParameter);
  129. } catch (AsnException e) {
  130. throw new MAPParsingComponentException("AsnException when decoding SM_RP_DA: " + e.getMessage(), e,
  131. MAPParsingComponentExceptionReason.MistypedParameter);
  132. }
  133. }
  134. private void _decode(AsnInputStream asnIS, int length) throws MAPParsingComponentException, IOException, AsnException {
  135. AsnInputStream ais = asnIS.readSequenceStreamData(length);
  136. int tag = ais.readTag();
  137. if (ais.getTagClass() != Tag.CLASS_UNIVERSAL || !ais.isTagPrimitive() || tag != Tag.ENUMERATED) {
  138. throw new MAPParsingComponentException(
  139. "Error while decoding ResponseTime: Parameter 0[ResponseTimeCategory ::= ENUMERATED] bad tag class, tag or not primitive",
  140. MAPParsingComponentExceptionReason.MistypedParameter);
  141. }
  142. int length1 = ais.readLength();
  143. int category = (int) ais.readIntegerData(length1);
  144. this.responseTimeCategory = ResponseTimeCategory.getResponseTimeCategory(category);
  145. while (true) {
  146. if (ais.available() == 0)
  147. break;
  148. switch (ais.readTag()) {
  149. default:
  150. ais.advanceElement();
  151. break;
  152. }
  153. }
  154. }
  155. /*
  156. * (non-Javadoc)
  157. *
  158. * @see
  159. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
  160. * (org.mobicents.protocols.asn.AsnOutputStream)
  161. */
  162. @Override
  163. public void encodeAll(AsnOutputStream asnOs) throws MAPException {
  164. this.encodeAll(asnOs, Tag.CLASS_UNIVERSAL, Tag.SEQUENCE);
  165. }
  166. /*
  167. * (non-Javadoc)
  168. *
  169. * @see
  170. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeAll
  171. * (org.mobicents.protocols.asn.AsnOutputStream, int, int)
  172. */
  173. @Override
  174. public void encodeAll(AsnOutputStream asnOs, int tagClass, int tag) throws MAPException {
  175. try {
  176. asnOs.writeTag(tagClass, false, tag);
  177. int pos = asnOs.StartContentDefiniteLength();
  178. this.encodeData(asnOs);
  179. asnOs.FinalizeContent(pos);
  180. } catch (AsnException e) {
  181. throw new MAPException("AsnException when encoding InformServiceCentreRequest: " + e.getMessage(), e);
  182. }
  183. }
  184. /*
  185. * (non-Javadoc)
  186. *
  187. * @see
  188. * org.mobicents.protocols.ss7.map.api.primitives.MAPAsnPrimitive#encodeData
  189. * (org.mobicents.protocols.asn.AsnOutputStream)
  190. */
  191. @Override
  192. public void encodeData(AsnOutputStream asnOs) throws MAPException {
  193. if (this.responseTimeCategory == null) {
  194. throw new MAPException("Error while encoding ResponseTime the mandatory parameter responseTimeCategory is not defined");
  195. }
  196. try {
  197. asnOs.writeInteger(Tag.CLASS_UNIVERSAL, Tag.ENUMERATED, this.responseTimeCategory.getCategory());
  198. } catch (IOException e) {
  199. throw new MAPException("IOException when encoding responseTimeCategory: ", e);
  200. } catch (AsnException e) {
  201. throw new MAPException("AsnException when encoding responseTimeCategory: ", e);
  202. }
  203. }
  204. @Override
  205. public int hashCode() {
  206. final int prime = 31;
  207. int result = 1;
  208. result = prime * result + ((responseTimeCategory == null) ? 0 : responseTimeCategory.hashCode());
  209. return result;
  210. }
  211. @Override
  212. public boolean equals(Object obj) {
  213. if (this == obj)
  214. return true;
  215. if (obj == null)
  216. return false;
  217. if (getClass() != obj.getClass())
  218. return false;
  219. ResponseTimeImpl other = (ResponseTimeImpl) obj;
  220. if (responseTimeCategory != other.responseTimeCategory)
  221. return false;
  222. return true;
  223. }
  224. }