PageRenderTime 38ms CodeModel.GetById 14ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 1ms

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