PageRenderTime 21ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mobicents.googlecode.com/
Java | 307 lines | 214 code | 55 blank | 38 comment | 32 complexity | ccbfd36d7e3e9655d67294b5541204b6 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.io.UnsupportedEncodingException;
 27import java.util.Arrays;
 28
 29import org.mobicents.protocols.smpp.Address;
 30import org.mobicents.protocols.smpp.util.PacketDecoder;
 31import org.mobicents.protocols.smpp.util.PacketEncoder;
 32import org.mobicents.protocols.smpp.util.SMPPDate;
 33
 34/**
 35 * Submit a message to the SMSC for delivery to a single destination.
 36 * 
 37 * @version $Id: SubmitSM.java 452 2009-01-15 16:56:36Z orank $
 38 * @author amit bhayani
 39 * @author orank
 40 */
 41public class SubmitSM extends SMPPPacket {
 42	private static final long serialVersionUID = 2L;
 43
 44	private String serviceType;
 45	private Address source;
 46	private Address destination;
 47	private int esmClass;
 48	private int protocolID;
 49	private int priority;
 50	private SMPPDate deliveryTime;
 51	private SMPPDate expiryTime;
 52	private int registered;
 53	private int replaceIfPresent;
 54	private int dataCoding;
 55	private int defaultMsg;
 56	private byte[] message;
 57
 58	public SubmitSM() {
 59		super(CommandId.SUBMIT_SM);
 60	}
 61
 62	SubmitSM(int commandId) {
 63		// Convenience constructor provided for deliver_sm.
 64		super(commandId);
 65	}
 66
 67	public int getDataCoding() {
 68		return dataCoding;
 69	}
 70
 71	public void setDataCoding(int dataCoding) {
 72		this.dataCoding = dataCoding;
 73	}
 74
 75	public int getDefaultMsg() {
 76		return defaultMsg;
 77	}
 78
 79	public void setDefaultMsg(int defaultMsg) {
 80		this.defaultMsg = defaultMsg;
 81	}
 82
 83	public SMPPDate getDeliveryTime() {
 84		return deliveryTime;
 85	}
 86
 87	public void setDeliveryTime(SMPPDate deliveryTime) {
 88		this.deliveryTime = deliveryTime;
 89	}
 90
 91	public Address getDestination() {
 92		return destination;
 93	}
 94
 95	public void setDestination(Address destination) {
 96		this.destination = destination;
 97	}
 98
 99	public int getEsmClass() {
100		return esmClass;
101	}
102
103	public void setEsmClass(int esmClass) {
104		this.esmClass = esmClass;
105	}
106
107	public SMPPDate getExpiryTime() {
108		return expiryTime;
109	}
110
111	public void setExpiryTime(SMPPDate expiryTime) {
112		this.expiryTime = expiryTime;
113	}
114
115	public byte[] getMessage() {
116		return message;
117	}
118
119	public void setMessage(byte[] message) {
120		this.message = message;
121	}
122
123	public int getPriority() {
124		return priority;
125	}
126
127	public void setPriority(int priority) {
128		this.priority = priority;
129	}
130
131	public int getProtocolID() {
132		return protocolID;
133	}
134
135	public void setProtocolID(int protocolID) {
136		this.protocolID = protocolID;
137	}
138
139	public int getRegistered() {
140		return registered;
141	}
142
143	public void setRegistered(int registered) {
144		this.registered = registered;
145	}
146
147	public int getReplaceIfPresent() {
148		return replaceIfPresent;
149	}
150
151	public void setReplaceIfPresent(int replaceIfPresent) {
152		this.replaceIfPresent = replaceIfPresent;
153	}
154
155	public String getServiceType() {
156		return serviceType;
157	}
158
159	public void setServiceType(String serviceType) {
160		this.serviceType = serviceType;
161	}
162
163	public Address getSource() {
164		return source;
165	}
166
167	public void setSource(Address source) {
168		this.source = source;
169	}
170
171	@Override
172	public boolean equals(Object obj) {
173		boolean equals = super.equals(obj);
174		if (equals) {
175			SubmitSM other = (SubmitSM) obj;
176			equals |= safeCompare(serviceType, other.serviceType);
177			equals |= safeCompare(source, other.source);
178			equals |= safeCompare(destination, other.destination);
179			equals |= esmClass == other.esmClass;
180			equals |= protocolID == other.protocolID;
181			equals |= priority == other.priority;
182			equals |= safeCompare(deliveryTime, other.deliveryTime);
183			equals |= safeCompare(expiryTime, other.expiryTime);
184			equals |= registered == other.registered;
185			equals |= replaceIfPresent == other.replaceIfPresent;
186			equals |= dataCoding == other.dataCoding;
187			equals |= defaultMsg == other.defaultMsg;
188			equals |= Arrays.equals(message, other.message);
189		}
190		return equals;
191	}
192
193	@Override
194	public int hashCode() {
195		int hc = super.hashCode();
196		hc += (serviceType != null) ? serviceType.hashCode() : 0;
197		hc += (source != null) ? source.hashCode() : 0;
198		hc += (destination != null) ? destination.hashCode() : 0;
199		hc += Integer.valueOf(esmClass).hashCode();
200		hc += Integer.valueOf(protocolID).hashCode();
201		hc += Integer.valueOf(priority).hashCode();
202		hc += (deliveryTime != null) ? deliveryTime.hashCode() : 0;
203		hc += (expiryTime != null) ? expiryTime.hashCode() : 0;
204		hc += Integer.valueOf(registered).hashCode();
205		hc += Integer.valueOf(replaceIfPresent).hashCode();
206		hc += Integer.valueOf(dataCoding).hashCode();
207		hc += Integer.valueOf(defaultMsg).hashCode();
208		if (message != null) {
209			try {
210				hc += new String(message, "US-ASCII").hashCode();
211			} catch (UnsupportedEncodingException x) {
212				throw new RuntimeException(x);
213			}
214		}
215		return hc;
216	}
217
218	/**
219	 * Return the number of bytes this packet would be encoded as to an
220	 * OutputStream.
221	 * 
222	 * @return the number of bytes this packet would encode as.
223	 */
224	public int getMandatorySize() {
225		int len = ((serviceType != null) ? serviceType.length() : 0)
226				+ ((source != null) ? source.getLength() : 3)
227				+ ((destination != null) ? destination.getLength() : 3)
228				+ ((deliveryTime != null) ? deliveryTime.getLength()
229						: 1)
230				+ ((expiryTime != null) ? expiryTime.getLength() : 1)
231				+ ((message != null) ? (message.length + 1)  : 1);
232
233		// 8 1-byte integers, 3 c-strings
234		return len + 8;
235	}
236
237	protected void writeMandatory(PacketEncoder encoder) throws IOException {
238
239		encoder.writeCString(serviceType);
240
241		if (source != null) {
242			source.writeTo(encoder);
243		} else {
244			// Write ton=0(null), npi=0(null), address=\0(nul)
245			new Address().writeTo(encoder);
246		}
247		if (destination != null) {
248			destination.writeTo(encoder);
249		} else {
250			// Write ton=0(null), npi=0(null), address=\0(nul)
251			new Address().writeTo(encoder);
252		}
253
254		encoder.writeUInt1(esmClass);
255		encoder.writeUInt1(protocolID);
256		encoder.writeUInt1(priority);
257
258		encoder.writeDate(deliveryTime);
259		encoder.writeDate(expiryTime);
260
261		encoder.writeUInt1(registered);
262		encoder.writeUInt1(replaceIfPresent);
263		encoder.writeUInt1(dataCoding);
264		encoder.writeUInt1(defaultMsg);
265
266		int smLength = 0;
267		if (message != null) {
268			smLength = message.length;
269		}
270
271		encoder.writeUInt1(smLength);
272
273		if (message != null) {
274			encoder.writeBytes(message);
275		}
276	}
277
278	@Override
279	protected void readMandatory(PacketDecoder decoder) {
280		this.serviceType = decoder.readCString();
281
282		this.source = new Address();
283		this.source.readFrom(decoder);
284
285		this.destination = new Address();
286		this.destination.readFrom(decoder);
287
288		this.esmClass = decoder.readUInt1();
289		this.protocolID = decoder.readUInt1();
290		this.priority = decoder.readUInt1();
291
292		this.deliveryTime = decoder.readDate();
293		this.expiryTime = decoder.readDate();
294
295		this.registered = decoder.readUInt1();
296		this.replaceIfPresent = decoder.readUInt1();
297		this.dataCoding = decoder.readUInt1();
298		this.defaultMsg = decoder.readUInt1();
299		
300		int smLength = decoder.readUInt1();
301		
302		if(smLength > 0){
303			 this.message =decoder.readBytes(smLength);
304		}
305	}
306
307}