/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/message/M3UAMessageImpl.java

http://mobicents.googlecode.com/ · Java · 127 lines · 71 code · 26 blank · 30 comment · 4 complexity · 39e40df9a363378ad6a90009932f056a 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.m3ua.impl.message;
  23. import java.nio.ByteBuffer;
  24. import javolution.text.TextBuilder;
  25. import javolution.util.FastMap;
  26. import org.mobicents.protocols.ss7.m3ua.impl.parameter.ParameterFactoryImpl;
  27. import org.mobicents.protocols.ss7.m3ua.message.M3UAMessage;
  28. import org.mobicents.protocols.ss7.m3ua.parameter.Parameter;
  29. /**
  30. * @author amit bhayani
  31. * @author kulikov
  32. */
  33. public abstract class M3UAMessageImpl implements M3UAMessage {
  34. // header part
  35. private int messageClass;
  36. private int messageType;
  37. private String message;
  38. protected FastMap<Short, Parameter> parameters = new FastMap<Short, Parameter>();
  39. private ParameterFactoryImpl factory = new ParameterFactoryImpl();
  40. int initialPosition = 0;
  41. public M3UAMessageImpl(String message) {
  42. this.message = message;
  43. }
  44. protected M3UAMessageImpl(int messageClass, int messageType, String message) {
  45. this(message);
  46. this.messageClass = messageClass;
  47. this.messageType = messageType;
  48. }
  49. protected abstract void encodeParams(ByteBuffer buffer);
  50. public void encode(ByteBuffer buffer) {
  51. initialPosition = buffer.position();
  52. buffer.position(initialPosition + 8);
  53. encodeParams(buffer);
  54. int length = buffer.position() - initialPosition;
  55. // buffer.rewind();
  56. buffer.put(initialPosition++, (byte) 1);
  57. buffer.put(initialPosition++, (byte) 0);
  58. buffer.put(initialPosition++, (byte) messageClass);
  59. buffer.put(initialPosition++, (byte) messageType);
  60. buffer.putInt(initialPosition++, length);
  61. // buffer.position(length);
  62. }
  63. protected void decode(byte[] data) {
  64. this.decode(data, 0);
  65. }
  66. protected void decode(byte[] data, int initialPos) {
  67. int pos = initialPos;
  68. while (pos < data.length) {
  69. short tag = (short) ((data[pos] & 0xff) << 8 | (data[pos + 1] & 0xff));
  70. short len = (short) ((data[pos + 2] & 0xff) << 8 | (data[pos + 3] & 0xff));
  71. byte[] value = new byte[len - 4];
  72. System.arraycopy(data, pos + 4, value, 0, value.length);
  73. pos += len;
  74. parameters.put(tag, factory.createParameter(tag, value));
  75. // The Parameter Length does not include any padding octets. We have
  76. // to consider padding here
  77. int padding = 4 - (pos % 4);
  78. if (padding < 4) {
  79. pos += padding;
  80. }
  81. }
  82. }
  83. public int getMessageClass() {
  84. return messageClass;
  85. }
  86. public int getMessageType() {
  87. return messageType;
  88. }
  89. @Override
  90. public String toString() {
  91. TextBuilder tb = new TextBuilder();
  92. tb.append(this.message).append(" Params(");
  93. for (FastMap.Entry<Short, Parameter> e = parameters.head(), end = parameters.tail(); (e = e.getNext()) != end;) {
  94. Parameter value = e.getValue();
  95. tb.append(value.toString());
  96. tb.append(", ");
  97. }
  98. tb.append(")");
  99. return tb.toString();
  100. }
  101. }