Skip to content

Commit

Permalink
Fixed Issue twitter-archive#33 Add submit_multi request/response support
Browse files Browse the repository at this point in the history
  • Loading branch information
abhayani authored and vetss committed Feb 1, 2016
1 parent 756f0e3 commit 3e56da8
Show file tree
Hide file tree
Showing 8 changed files with 553 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/cloudhopper/smpp/SmppConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public class SmppConstants {
//
// SUBMIT_MULTI destination type flags
//
//public static final int SME_ADDRESS = 1;
//public static final int DISTRIBUTION_LIST_NAME = 2;
public static final int SME_ADDRESS = 1;
public static final int DISTRIBUTION_LIST_NAME = 2;

//
// SMPP Command ID (Requests)
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/cloudhopper/smpp/pdu/BaseSm.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public abstract class BaseSm<R extends PduResponse> extends PduRequest<R> {
protected Address sourceAddress;
protected Address destAddress;
protected byte esmClass;
private byte protocolId; // not present in data_sm
private byte priority; // not present in data_sm
private String scheduleDeliveryTime; // not present in data_sm
private String validityPeriod; // not present in data_sm
protected byte protocolId; // not present in data_sm
protected byte priority; // not present in data_sm
protected String scheduleDeliveryTime; // not present in data_sm
protected String validityPeriod; // not present in data_sm
protected byte registeredDelivery;
private byte replaceIfPresent; // not present in data_sm
protected byte replaceIfPresent; // not present in data_sm
protected byte dataCoding;
private byte defaultMsgId; // not present in data_sm, not used in deliver_sm
private byte[] shortMessage; // not present in data_sm
protected byte defaultMsgId; // not present in data_sm, not used in deliver_sm
protected byte[] shortMessage; // not present in data_sm

public BaseSm(int commandId, String name) {
super(commandId, name);
Expand Down Expand Up @@ -243,4 +243,4 @@ public void appendBodyToString(StringBuilder buffer) {
HexUtil.appendHexString(buffer, this.shortMessage);
buffer.append("])");
}
}
}
205 changes: 205 additions & 0 deletions src/main/java/com/cloudhopper/smpp/pdu/SubmitMulti.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/**
*
*/
package com.cloudhopper.smpp.pdu;

/*
* #%L
* ch-smpp
* %%
* Copyright (C) 2009 - 2013 Cloudhopper by Twitter
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import java.util.ArrayList;
import java.util.List;

import org.jboss.netty.buffer.ChannelBuffer;

import com.cloudhopper.smpp.SmppConstants;
import com.cloudhopper.smpp.type.Address;
import com.cloudhopper.smpp.type.RecoverablePduException;
import com.cloudhopper.smpp.type.SmppInvalidArgumentException;
import com.cloudhopper.smpp.type.UnrecoverablePduException;
import com.cloudhopper.smpp.util.ChannelBufferUtil;
import com.cloudhopper.smpp.util.PduUtil;

/**
* @author Amit Bhayani
*
*/
public class SubmitMulti extends BaseSm<SubmitMultiResp> {

private byte numberOfDest;

private List<Address> destAddresses = new ArrayList<Address>();
private List<String> destDistributionList = new ArrayList<String>();

/**
* @param commandId
* @param name
*/
public SubmitMulti() {
super(SmppConstants.CMD_ID_SUBMIT_MULTI, "submit_multi");
}

/*
* (non-Javadoc)
*
* @see com.cloudhopper.smpp.pdu.PduRequest#createResponse()
*/
@Override
public SubmitMultiResp createResponse() {
SubmitMultiResp resp = new SubmitMultiResp();
resp.setSequenceNumber(this.getSequenceNumber());
return resp;
}

/*
* (non-Javadoc)
*
* @see com.cloudhopper.smpp.pdu.PduRequest#getResponseClass()
*/
@Override
public Class<SubmitMultiResp> getResponseClass() {
return SubmitMultiResp.class;
}

@Override
public Address getDestAddress() {
return null;
}

@Override
public void setDestAddress(Address value) {

}

public void addDestAddresses(Address address)
throws SmppInvalidArgumentException {
this.numberOfDest++;
this.destAddresses.add(address);
}

public void addDestDestributionListName(String name) {
this.numberOfDest++;
this.destDistributionList.add(name);
}

public List<Address> getDestAddresses() {
return this.destAddresses;
}

public List<String> getDestDestributionListName() {
return this.destDistributionList;
}

public int getNumberOfDest(){
return this.numberOfDest;
}

@Override
public void readBody(ChannelBuffer buffer) throws UnrecoverablePduException, RecoverablePduException {
this.serviceType = ChannelBufferUtil.readNullTerminatedString(buffer);
this.sourceAddress = ChannelBufferUtil.readAddress(buffer);

this.numberOfDest = buffer.readByte();

for(int count=0;count<this.numberOfDest; count++){
byte flag = buffer.readByte();
if(flag==SmppConstants.SME_ADDRESS){
this.destAddresses.add(ChannelBufferUtil.readAddress(buffer));
} else if(flag==SmppConstants.DISTRIBUTION_LIST_NAME){
this.destDistributionList.add(ChannelBufferUtil.readNullTerminatedString(buffer));
}
}

this.esmClass = buffer.readByte();
this.protocolId = buffer.readByte();
this.priority = buffer.readByte();
this.scheduleDeliveryTime = ChannelBufferUtil.readNullTerminatedString(buffer);
this.validityPeriod = ChannelBufferUtil.readNullTerminatedString(buffer);
this.registeredDelivery = buffer.readByte();
this.replaceIfPresent = buffer.readByte();
this.dataCoding = buffer.readByte();
this.defaultMsgId = buffer.readByte();
// this is always an unsigned version of the short message length
short shortMessageLength = buffer.readUnsignedByte();
this.shortMessage = new byte[shortMessageLength];
buffer.readBytes(this.shortMessage);
}

@Override
public void writeBody(ChannelBuffer buffer)
throws UnrecoverablePduException, RecoverablePduException {
ChannelBufferUtil.writeNullTerminatedString(buffer, this.serviceType);
ChannelBufferUtil.writeAddress(buffer, this.sourceAddress);

buffer.writeByte(this.numberOfDest);

for(Address adress : this.destAddresses){
buffer.writeByte(SmppConstants.SME_ADDRESS);
ChannelBufferUtil.writeAddress(buffer, adress);
}

for(String s : this.destDistributionList){
buffer.writeByte(SmppConstants.DISTRIBUTION_LIST_NAME);
ChannelBufferUtil.writeNullTerminatedString(buffer, s);

}

buffer.writeByte(this.esmClass);
buffer.writeByte(this.protocolId);
buffer.writeByte(this.priority);
ChannelBufferUtil.writeNullTerminatedString(buffer,
this.scheduleDeliveryTime);
ChannelBufferUtil
.writeNullTerminatedString(buffer, this.validityPeriod);
buffer.writeByte(this.registeredDelivery);
buffer.writeByte(this.replaceIfPresent);
buffer.writeByte(this.dataCoding);
buffer.writeByte(this.defaultMsgId);
buffer.writeByte((byte) getShortMessageLength());
if (this.shortMessage != null) {
buffer.writeBytes(this.shortMessage);
}
}

@Override
public int calculateByteSizeOfBody() {
int bodyLength = 0;
bodyLength += PduUtil.calculateByteSizeOfNullTerminatedString(this.serviceType);
bodyLength += PduUtil.calculateByteSizeOfAddress(this.sourceAddress);

bodyLength +=1; //number_of_dests

for(Address adress : this.destAddresses){
bodyLength += 1;//Flag
bodyLength += PduUtil.calculateByteSizeOfAddress(adress);
}

for(String s : this.destDistributionList){
bodyLength += 1;//Flag
bodyLength += PduUtil.calculateByteSizeOfNullTerminatedString(s);
}

bodyLength += 3; // esmClass, priority, protocolId
bodyLength += PduUtil.calculateByteSizeOfNullTerminatedString(this.scheduleDeliveryTime);
bodyLength += PduUtil.calculateByteSizeOfNullTerminatedString(this.validityPeriod);
bodyLength += 5; // regDelivery, replace, dataCoding, defaultMsgId, messageLength bytes
bodyLength += getShortMessageLength();
return bodyLength;
}
}
119 changes: 119 additions & 0 deletions src/main/java/com/cloudhopper/smpp/pdu/SubmitMultiResp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
*
*/
package com.cloudhopper.smpp.pdu;

/*
* #%L
* ch-smpp
* %%
* Copyright (C) 2009 - 2013 Cloudhopper by Twitter
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import java.util.ArrayList;
import java.util.List;

import org.jboss.netty.buffer.ChannelBuffer;

import com.cloudhopper.smpp.SmppConstants;
import com.cloudhopper.smpp.type.Address;
import com.cloudhopper.smpp.type.RecoverablePduException;
import com.cloudhopper.smpp.type.SmppInvalidArgumentException;
import com.cloudhopper.smpp.type.UnrecoverablePduException;
import com.cloudhopper.smpp.type.UnsucessfulSME;
import com.cloudhopper.smpp.util.ChannelBufferUtil;
import com.cloudhopper.smpp.util.PduUtil;

/**
* @author Amit Bhayani
*
*/
public class SubmitMultiResp extends BaseSmResp {

private byte numberOfUnsucessfulDest;
private List<UnsucessfulSME> unsucessfulSmes = new ArrayList<UnsucessfulSME>();

/**
* @param commandId
* @param name
*/
public SubmitMultiResp() {
super(SmppConstants.CMD_ID_SUBMIT_MULTI_RESP, "submit_multi_resp");
}

public void addUnsucessfulSME(UnsucessfulSME unsucessfulSME)
throws SmppInvalidArgumentException {
this.numberOfUnsucessfulDest++;
this.unsucessfulSmes.add(unsucessfulSME);
}

public byte getNumberOfUnsucessfulDest() {
return numberOfUnsucessfulDest;
}

public List<UnsucessfulSME> getUnsucessfulSmes() {
return unsucessfulSmes;
}

@Override
public void readBody(ChannelBuffer buffer)
throws UnrecoverablePduException, RecoverablePduException {
super.readBody(buffer);

this.numberOfUnsucessfulDest = buffer.readByte();

for (int count = 0; count < this.numberOfUnsucessfulDest; count++) {
Address address = ChannelBufferUtil.readAddress(buffer);
int errorStatusCode = buffer.readInt();

this.unsucessfulSmes.add(new UnsucessfulSME(errorStatusCode,
address));

}
}

@Override
public int calculateByteSizeOfBody() {
int bodyLength = 0;
bodyLength = super.calculateByteSizeOfBody();

bodyLength += 1; // no_unsuccess

for (int count = 0; count < this.numberOfUnsucessfulDest; count++) {
UnsucessfulSME unsucessfulSME = this.unsucessfulSmes.get(count);
bodyLength += PduUtil.calculateByteSizeOfAddress(unsucessfulSME
.getAddress());
bodyLength += 4; // error_status_code
}

return bodyLength;
}

@Override
public void writeBody(ChannelBuffer buffer)
throws UnrecoverablePduException, RecoverablePduException {
super.writeBody(buffer);

buffer.writeByte(this.numberOfUnsucessfulDest);

for (int count = 0; count < this.numberOfUnsucessfulDest; count++) {
UnsucessfulSME unsucessfulSME = this.unsucessfulSmes.get(count);
ChannelBufferUtil.writeAddress(buffer, unsucessfulSME.getAddress());
buffer.writeInt(unsucessfulSME.getErrorStatusCode());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import com.cloudhopper.smpp.pdu.QuerySmResp;
import com.cloudhopper.smpp.pdu.ReplaceSm;
import com.cloudhopper.smpp.pdu.ReplaceSmResp;
import com.cloudhopper.smpp.pdu.SubmitMulti;
import com.cloudhopper.smpp.pdu.SubmitMultiResp;
import com.cloudhopper.smpp.pdu.SubmitSm;
import com.cloudhopper.smpp.pdu.SubmitSmResp;
import com.cloudhopper.smpp.pdu.Unbind;
Expand Down Expand Up @@ -181,6 +183,8 @@ protected Pdu doDecode(int commandLength, ChannelBuffer buffer) throws Unrecover
pdu = new Unbind();
} else if (commandId == SmppConstants.CMD_ID_ALERT_NOTIFICATION) {
pdu = new AlertNotification();
} else if (commandId == SmppConstants.CMD_ID_SUBMIT_MULTI) {
pdu = new SubmitMulti();
} else {
pdu = new PartialPdu(commandId);
}
Expand Down Expand Up @@ -209,6 +213,8 @@ protected Pdu doDecode(int commandLength, ChannelBuffer buffer) throws Unrecover
pdu = new UnbindResp();
} else if (commandId == SmppConstants.CMD_ID_GENERIC_NACK) {
pdu = new GenericNack();
} else if (commandId == SmppConstants.CMD_ID_SUBMIT_MULTI_RESP) {
pdu = new SubmitMultiResp();
} else {
pdu = new PartialPduResp(commandId);
}
Expand Down
Loading

0 comments on commit 3e56da8

Please sign in to comment.