Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.yondu.smppmockclient;
- import static java.lang.invoke.MethodHandles.lookup;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Random;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledThreadPoolExecutor;
- import java.util.concurrent.ThreadFactory;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.atomic.AtomicInteger;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import com.cloudhopper.commons.charset.Charset;
- /*
- * #%L
- * ch-smpp
- * %%
- * Copyright (C) 2009 - 2015 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 com.cloudhopper.commons.charset.CharsetUtil;
- import com.cloudhopper.smpp.SmppBindType;
- import com.cloudhopper.smpp.SmppConstants;
- import com.cloudhopper.smpp.SmppSession;
- import com.cloudhopper.smpp.SmppSessionConfiguration;
- import com.cloudhopper.smpp.impl.DefaultSmppClient;
- import com.cloudhopper.smpp.impl.DefaultSmppSessionHandler;
- import com.cloudhopper.smpp.pdu.DeliverSm;
- import com.cloudhopper.smpp.pdu.PduRequest;
- import com.cloudhopper.smpp.pdu.PduResponse;
- import com.cloudhopper.smpp.pdu.SubmitSm;
- import com.cloudhopper.smpp.type.Address;
- import com.cloudhopper.smpp.type.SmppInvalidArgumentException;
- import com.yondu.smpp.custom.charset.ASCIICharset;
- import com.yondu.smpp.custom.charset.ISO2022JPCharset;
- import com.yondu.smpp.custom.charset.ISO88595Charset;
- import com.yondu.smpp.custom.charset.ISO88598Charset;
- import com.yondu.smpp.custom.charset.JISX0208Charset;
- import com.yondu.smpp.custom.charset.JISX0212Charset;
- import com.yondu.smpp.custom.charset.KSC5601Charset;
- /**
- *
- * @author joelauer (twitter: @jjlauer or
- * <a href="http://twitter.com/jjlauer" target=window>http://twitter.com
- * /jjlauer</a>)
- */
- public class ClientMain {
- private static final Logger logger = LoggerFactory.getLogger(lookup().lookupClass());
- private final static Random splitMessagesReferenceGenerator = new Random();
- private static final List<Byte> DCS_WITH_140B_LIMIT = Arrays.asList(
- SmppConstants.DATA_CODING_UCS2
- );
- private static final List<Byte> DCS_WITH_70_CHARACTER_LIMIT = Arrays.asList(
- SmppConstants.DATA_CODING_JIS,
- SmppConstants.DATA_CODING_EXKANJI,
- SmppConstants.DATA_CODING_MUSIC,
- SmppConstants.DATA_CODING_CYRLLIC,
- SmppConstants.DATA_CODING_KSC5601,
- SmppConstants.DATA_CODING_HEBREW
- );
- private static final Map<Byte, Charset> CHARSET_MAP;
- static {
- CHARSET_MAP = new HashMap<>();
- CHARSET_MAP.put(SmppConstants.DATA_CODING_DEFAULT, CharsetUtil.CHARSET_GSM);
- CHARSET_MAP.put(SmppConstants.DATA_CODING_IA5, new ASCIICharset());
- CHARSET_MAP.put(SmppConstants.DATA_CODING_LATIN1, CharsetUtil.CHARSET_ISO_8859_1);
- CHARSET_MAP.put(SmppConstants.DATA_CODING_UCS2, CharsetUtil.CHARSET_UCS_2);
- CHARSET_MAP.put(SmppConstants.DATA_CODING_JIS, new JISX0208Charset());
- CHARSET_MAP.put(SmppConstants.DATA_CODING_CYRLLIC, new ISO88595Charset());
- CHARSET_MAP.put(SmppConstants.DATA_CODING_HEBREW, new ISO88598Charset());
- CHARSET_MAP.put(SmppConstants.DATA_CODING_MUSIC, new ISO2022JPCharset());
- CHARSET_MAP.put(SmppConstants.DATA_CODING_EXKANJI, new JISX0212Charset());
- CHARSET_MAP.put(SmppConstants.DATA_CODING_KSC5601, new KSC5601Charset());
- }
- static public void main(String[] args) throws Exception {
- Map<String, String> parameters = getParametersFromConfigFile(args[0]);
- String mobileNumbersStr = parameters.get("mobilenumber");
- String[] mobileNumbersArray = mobileNumbersStr.split(",");
- String message = parameters.get("message");
- int messageCount = Integer.parseInt(parameters.get("messagecount"));
- String shortCode = parameters.get("senderid");
- byte dcs = Byte.parseByte(parameters.get("dcs"));
- String username = parameters.get("username");
- String password = parameters.get("password");
- String bindType = parameters.get("bindtype");
- int udhReferenceNumberType = Integer.parseInt(parameters.get("udhReferenceNumberType"));
- boolean isSSL = Boolean.parseBoolean(parameters.get("isSSL"));
- SmppBindType smppBindType = null;
- switch (bindType) {
- case "receiver":
- smppBindType = SmppBindType.RECEIVER;
- break;
- case "transmitter":
- smppBindType = SmppBindType.TRANSMITTER;
- break;
- default:
- smppBindType = SmppBindType.TRANSCEIVER;
- }
- String smpphost = parameters.get("smpphost");
- int smppport = Integer.parseInt(parameters.get("smppport"));
- boolean concatenate = Boolean.parseBoolean(parameters.get("concatenate"));
- int messageLength = DCS_WITH_140B_LIMIT.contains(dcs) ? Integer.parseInt(parameters.get("messagelength")) * 2
- : Integer.parseInt(parameters.get("messagelength"));
- ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool();
- ScheduledThreadPoolExecutor monitorExecutor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(1,
- new ThreadFactory() {
- private AtomicInteger sequence = new AtomicInteger(0);
- @Override
- public Thread newThread(Runnable r) {
- Thread t = new Thread(r);
- t.setName("SmppClientSessionWindowMonitorPool-" + sequence.getAndIncrement());
- return t;
- }
- });
- DefaultSmppClient clientBootstrap = new DefaultSmppClient(executor, 1, monitorExecutor);
- DefaultSmppSessionHandler sessionHandler = new ClientSmppSessionHandler();
- SmppSessionConfiguration config0 = new SmppSessionConfiguration();
- config0.setWindowSize(100);
- config0.setName("Tester.Session.0");
- config0.setType(smppBindType);
- config0.setHost(smpphost);
- config0.setPort(smppport);
- config0.setConnectTimeout(10000);
- config0.setSystemId(username);
- config0.setPassword(password);
- config0.getLoggingOptions().setLogBytes(true);
- config0.setRequestExpiryTimeout(30000);
- config0.setWindowMonitorInterval(15000);
- config0.setCountersEnabled(true);
- config0.setUseSsl(isSSL);
- SmppSession session0 = null;
- try {
- // create session a session by having the bootstrap connect a
- // socket, send the bind request, and wait for a bind response
- session0 = clientBootstrap.bind(config0, sessionHandler);
- String text = message;
- Charset charset = CHARSET_MAP.get(dcs);
- byte[] textBytes = CharsetUtil.encode(text, charset);
- for (int i = 0; i < messageCount; i++) {
- int mobileNumberIndex = i % mobileNumbersArray.length;
- String mobileNumber = mobileNumbersArray[mobileNumberIndex];
- List<SubmitSm> submitSm = createSubmitSm(textBytes, mobileNumber, shortCode, dcs, concatenate,
- messageLength, udhReferenceNumberType);
- /* session0.submit(submitSm, 30000); */
- for (SubmitSm submit : submitSm) {
- session0.submit(submit, 30000);
- }
- }
- } catch (Exception e) {
- logger.error("Exception sending the transaction!", e);
- }
- }
- private static List<SubmitSm> createSubmitSm(byte[] message, String mobileNumber, String shortcode, byte dcs,
- boolean concatenate, int messageLength, int udhReferenceNumberType) throws SmppInvalidArgumentException {
- List<SubmitSm> submitSmList = new ArrayList<>();
- if (concatenate) {
- byte[] referenceNumber = udhReferenceNumberType == 8 ? new byte[2] : new byte[1];
- splitMessagesReferenceGenerator.nextBytes(referenceNumber);
- byte[][] parts = createConcatenatedBinaryShortMessages(message, referenceNumber, messageLength, dcs);
- if (parts == null) {
- SubmitSm submitSm = createSubmitSm(message, shortcode, mobileNumber, dcs);
- submitSmList.add(submitSm);
- } else {
- for (byte[] part : parts) {
- SubmitSm submitSm = createSubmitSm(part, shortcode, mobileNumber, dcs);
- submitSm.setEsmClass(SmppConstants.ESM_CLASS_UDHI_MASK);
- submitSmList.add(submitSm);
- }
- }
- } else {
- SubmitSm submitSm = createSubmitSm(message, shortcode, mobileNumber, dcs);
- submitSmList.add(submitSm);
- }
- return submitSmList;
- }
- /**
- * Could either implement SmppSessionHandler or only override select methods
- * by extending a DefaultSmppSessionHandler.
- */
- public static class ClientSmppSessionHandler extends DefaultSmppSessionHandler {
- public ClientSmppSessionHandler() {
- super(logger);
- }
- @Override
- public PduResponse firePduRequestReceived(@SuppressWarnings("rawtypes") PduRequest pduRequest) {
- if (pduRequest instanceof DeliverSm) {
- DeliverSm deliverSm = (DeliverSm) pduRequest;
- String message = CharsetUtil.decode(deliverSm.getShortMessage(), CharsetUtil.CHARSET_GSM);
- logger.info("DLR: {}", message);
- }
- PduResponse response = pduRequest.createResponse();
- return response;
- }
- }
- public static Map<String, String> getParametersFromConfigFile(String configfile) {
- Map<String, String> parameters = new HashMap<>();
- InputStreamReader isr = null;
- BufferedReader br = null;
- try {
- isr = new InputStreamReader(new FileInputStream(configfile), java.nio.charset.Charset.forName("UTF-8"));
- br = new BufferedReader(isr);
- String line;
- while ((line = br.readLine()) != null) {
- line = line.trim();
- String[] array = line.split("=");
- if (array.length >= 2) {
- String value = "";
- for (int i = 1; i < array.length; i++) {
- value += array[i];
- }
- parameters.put(array[0].trim(), value.trim());
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return parameters;
- }
- static public byte[][] createConcatenatedBinaryShortMessages(byte[] binaryShortMessage, byte[] referenceNumber,
- int length, int dcs) throws IllegalArgumentException {
- if (binaryShortMessage == null) {
- return null;
- }
- if (DCS_WITH_70_CHARACTER_LIMIT.contains((byte) dcs)) {
- return createConcatenatedBinaryShortMessagesByCharacter(binaryShortMessage, referenceNumber, length,
- (byte) dcs);
- }
- // if the short message does not need to be concatenated
- if (binaryShortMessage.length <= length) {
- return null;
- }
- int messagePartLength = length - (!DCS_WITH_140B_LIMIT.contains(dcs) ? 7 : 6);
- // since the UDH will be 6 bytes, we'll split the data into chunks of
- // 134
- int numParts = (int) (binaryShortMessage.length / messagePartLength)
- + (binaryShortMessage.length % messagePartLength != 0 ? 1 : 0);
- // logger.debug("numParts=" + numParts);
- byte[][] shortMessageParts = new byte[numParts][];
- for (int i = 0; i < numParts; i++) {
- // default this part length to max of 134
- int shortMessagePartLength = messagePartLength;
- if ((i + 1) == numParts) {
- // last part (only need to add remainder)
- shortMessagePartLength = binaryShortMessage.length - (i * messagePartLength);
- }
- // logger.debug("part " + i + " len: " + shortMessagePartLength);
- // part will be UDH (6 bytes) + length of part
- int udhLength = referenceNumber.length == 1 ? 6 : 7;
- byte[] shortMessagePart = new byte[udhLength + shortMessagePartLength];
- setUdh(numParts, i + 1, referenceNumber, shortMessagePart);
- // copy this part's user data onto the end
- System.arraycopy(binaryShortMessage, (i * messagePartLength), shortMessagePart, udhLength,
- shortMessagePartLength);
- shortMessageParts[i] = shortMessagePart;
- }
- return shortMessageParts;
- }
- private static SubmitSm createSubmitSm(byte[] message, String senderId, String mobileNumber, byte dcs)
- throws SmppInvalidArgumentException {
- SubmitSm submit0 = new SubmitSm();
- // add delivery receipt
- // submit0.setRegisteredDelivery(SmppConstants.REGISTERED_DELIVERY_SMSC_RECEIPT_REQUESTED);
- submit0.setSourceAddress(new Address((byte) 0x03, (byte) 0x00, senderId));
- submit0.setDestAddress(new Address((byte) 0x01, (byte) 0x01, mobileNumber));
- submit0.setShortMessage(message);
- submit0.setDataCoding(dcs);
- return submit0;
- }
- static public byte[][] createConcatenatedBinaryShortMessagesByCharacter(byte[] binaryShortMessage,
- byte[] referenceNumber, int length, byte dcs) {
- Charset charset = CHARSET_MAP.get(dcs);
- String message = charset.decode(binaryShortMessage);
- if (message.length() <= length) {
- return null;
- }
- int messagePartLength = length - 3;
- int numParts = (int) (message.length() / messagePartLength)
- + (message.length() % messagePartLength != 0 ? 1 : 0);
- byte[][] shortMessageParts = new byte[numParts][];
- int messagePartStartIndex = 0;
- for (int i = 0; i < numParts; i++) {
- int messagePartEndIndex = (i == numParts - 1) ? message.length()
- : ((i * messagePartLength) + messagePartLength);
- String messagePart = message.substring(messagePartStartIndex, messagePartEndIndex);
- byte[] messagePartInByteArray = charset.encode(messagePart);
- int udhLength = referenceNumber.length == 1 ? 6 : 7;
- byte[] shortMessagePart = new byte[udhLength + messagePartInByteArray.length];
- setUdh(numParts, i + 1, referenceNumber, shortMessagePart);
- System.arraycopy(messagePartInByteArray, 0, shortMessagePart, udhLength, messagePartInByteArray.length);
- shortMessageParts[i] = shortMessagePart;
- messagePartStartIndex += messagePartLength;
- }
- return shortMessageParts;
- }
- private static void setUdh(int numberOfParts, int partNumber, byte[] referenceNumber, byte[] shortMessagePart) {
- // if 8bit reference number
- if (referenceNumber.length == 1) {
- shortMessagePart[0] = (byte) 0x05;
- shortMessagePart[1] = (byte) 0x00;
- shortMessagePart[2] = (byte) 0x03;
- shortMessagePart[3] = referenceNumber[0];
- shortMessagePart[4] = (byte) numberOfParts;
- shortMessagePart[5] = (byte) partNumber;
- // else if 16 bit reference number
- } else if (referenceNumber.length == 2) {
- shortMessagePart[0] = (byte) 0x06;
- shortMessagePart[1] = (byte) 0x08;
- shortMessagePart[2] = (byte) 0x04;
- shortMessagePart[3] = referenceNumber[0];
- shortMessagePart[4] = referenceNumber[1];
- shortMessagePart[5] = (byte) numberOfParts;
- shortMessagePart[6] = (byte) partNumber;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement