/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/message/QueryLastMsgsResp.java

http://mobicents.googlecode.com/ · Java · 141 lines · 73 code · 16 blank · 52 comment · 6 complexity · 168032f7e98736b0f6dbfb5df0d76372 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.smpp.message;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import org.mobicents.protocols.smpp.util.PacketDecoder;
  27. import org.mobicents.protocols.smpp.util.PacketEncoder;
  28. /**
  29. * SMSC response to a QueryLastMsgs request.
  30. * @version $Id: QueryLastMsgsResp.java 457 2009-01-15 17:37:42Z orank $
  31. */
  32. public class QueryLastMsgsResp extends SMPPPacket {
  33. private static final long serialVersionUID = 2L;
  34. private static final int MAX_SIZE = 255;
  35. /** The table of messages returned */
  36. private List<String> messageTable = new ArrayList<String>();
  37. /**
  38. * Construct a new QueryLastMsgsResp.
  39. */
  40. public QueryLastMsgsResp() {
  41. super(CommandId.QUERY_LAST_MSGS_RESP);
  42. }
  43. /**
  44. * Create a new QueryLastMsgsResp packet in response to a BindReceiver. This
  45. * constructor will set the sequence number to it's expected value.
  46. *
  47. * @param request
  48. * The Request packet the response is to
  49. */
  50. public QueryLastMsgsResp(SMPPPacket request) {
  51. super(request);
  52. }
  53. /**
  54. * Add a message Id to the response packet. A maximum of 255 message IDs
  55. * can be added, since the size specifier for the encoded packet is only
  56. * one byte. If an attempt is made to add more than 255 message IDs, this
  57. * method will fail silently.
  58. * @param id
  59. * The message Id to add to the packet.
  60. * @return The current number of message Ids (including the new one).
  61. */
  62. public int addMessageId(String id) {
  63. if (messageTable.size() < MAX_SIZE) {
  64. messageTable.add(id);
  65. }
  66. return messageTable.size();
  67. }
  68. /** Get the number of message Ids. */
  69. public int getMsgCount() {
  70. return messageTable.size();
  71. }
  72. /**
  73. * Get a String array of the message Ids.
  74. * @return A String array of all the message Ids. Will never return
  75. * <code>null</code>, if the table is empty a zero-length array will be
  76. * returned.
  77. */
  78. public String[] getMessageIds() {
  79. return (String[]) messageTable.toArray(new String[0]);
  80. }
  81. @Override
  82. public boolean equals(Object obj) {
  83. boolean equals = super.equals(obj);
  84. if (equals) {
  85. QueryLastMsgsResp other = (QueryLastMsgsResp) obj;
  86. equals |= safeCompare(messageTable, other.messageTable);
  87. }
  88. return equals;
  89. }
  90. @Override
  91. public int hashCode() {
  92. int hc = super.hashCode();
  93. hc += (messageTable != null) ? messageTable.hashCode() : 71;
  94. return hc;
  95. }
  96. @Override
  97. protected void toString(StringBuilder buffer) {
  98. buffer.append("msgCount=").append(messageTable.size())
  99. .append(",messageIds=").append(messageTable);
  100. }
  101. @Override
  102. protected void readMandatory(PacketDecoder decoder) {
  103. messageTable = new ArrayList<String>();
  104. int count = decoder.readUInt1();
  105. for (int i = 0; i < count; i++) {
  106. messageTable.add(decoder.readCString());
  107. }
  108. }
  109. @Override
  110. protected void writeMandatory(PacketEncoder encoder) throws IOException {
  111. int count = messageTable.size();
  112. encoder.writeUInt1(count);
  113. for (String s : messageTable) {
  114. encoder.writeCString(s);
  115. }
  116. }
  117. @Override
  118. protected int getMandatorySize() {
  119. int length = 1;
  120. for (String s : messageTable) {
  121. length += (1 + sizeOf(s));
  122. }
  123. return length;
  124. }
  125. }