/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/MyExample.java
Java | 163 lines | 95 code | 33 blank | 35 comment | 0 complexity | d635e24c1b99088c185799036020ec1a 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; 24 25import java.io.IOException; 26import java.net.UnknownHostException; 27 28import org.mobicents.protocols.smpp.Address; 29import org.mobicents.protocols.smpp.Session; 30import org.mobicents.protocols.smpp.encoding.AlphabetEncoding; 31import org.mobicents.protocols.smpp.encoding.DefaultAlphabetEncoding; 32import org.mobicents.protocols.smpp.event.SMPPEvent; 33import org.mobicents.protocols.smpp.event.SessionObserver; 34import org.mobicents.protocols.smpp.message.Bind; 35import org.mobicents.protocols.smpp.message.BindTransmitter; 36import org.mobicents.protocols.smpp.message.DataSM; 37import org.mobicents.protocols.smpp.message.SMPPPacket; 38import org.mobicents.protocols.smpp.message.SubmitSM; 39import org.mobicents.protocols.smpp.message.tlv.Tag; 40 41public class MyExample implements SessionObserver { 42 43 // private static final Logger STATIC_LOG = 44 // LoggerFactory.getLogger(MyExample.class); 45 46 // java.util.logging.Logger logger; 47 private String hostname = "192.168.0.105"; 48 private int port = 2775; 49 50 /** 51 * @param args 52 */ 53 public static void main(String[] args) { 54 // TODO Auto-generated method stub 55 56 MyExample example = new MyExample(); 57 example.test(); 58 59 try { 60 Thread.sleep(1000 * 60 * 5); 61 } catch (InterruptedException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 66 } 67 68 public void test() { 69 try { 70 71 // java.util.logging.LogManager logManager = 72 // java.util.logging.LogManager.getLogManager(); 73 // java.util.logging.Logger logger = logManager.getLogger(""); 74 // logger.setLevel(java.util.logging.Level.ALL); 75 // 76 // logger.fine("Starting"); 77 78 Session session = createSession(); 79 session.addObserver(this); 80 81 bind(session); 82 83 Thread.sleep(1000); 84 85 Address destination = new Address(); 86 destination.setAddress("919960639901"); 87 destination.setTON(1); 88 destination.setNPI(0); 89 90 sendSubmitSMMessage(session, destination, "Hello World"); 91 92 Thread.sleep(1000); 93 94 sendDataSMMessage(session, destination); 95 96 Thread.sleep(1000 * 60); 97 98 } catch (Exception e) { 99 e.printStackTrace(); 100 } 101 } 102 103 protected Session createSession() throws UnknownHostException { 104 return new Session(hostname, port); 105 } 106 107 private void bind(Session session) throws IOException { 108 BindTransmitter bt = new BindTransmitter(); 109 initBindReq(bt); 110 session.bind(bt); 111 } 112 113 protected Bind initBindReq(Bind bindRequest) { 114 bindRequest.setAddressTon(1); 115 bindRequest.setAddressNpi(0); 116 bindRequest.setAddressRange("50"); 117 bindRequest.setSystemType("ESME"); 118 bindRequest.setSystemId("smppclient1"); 119 bindRequest.setPassword("password"); 120 return bindRequest; 121 } 122 123 private void sendSubmitSMMessage(Session session, Address destination, 124 String message) throws IOException { 125 sendSubmitSMMessage(session, destination, message, 126 new DefaultAlphabetEncoding()); 127 } 128 129 private void sendSubmitSMMessage(Session session, Address destination, 130 String message, AlphabetEncoding encoding) throws IOException { 131 SubmitSM submitSm = new SubmitSM(); 132 submitSm.setDestination(destination); 133 submitSm.setDataCoding(encoding.getDataCoding()); 134 submitSm.setMessage(encoding.encode(message)); 135 session.sendPacket(submitSm); 136 } 137 138 private void sendDataSMMessage(Session session, Address destination) 139 throws IOException { 140 sendDataSMMessage(session, destination, new DefaultAlphabetEncoding()); 141 } 142 143 private void sendDataSMMessage(Session session, Address destination, 144 AlphabetEncoding encoding) throws IOException { 145 DataSM dataSm = new DataSM(); 146 dataSm.setDestination(destination); 147 dataSm.setDataCoding(encoding.getDataCoding()); 148 dataSm.setTLV(Tag.MESSAGE_PAYLOAD, 149 "Hello world?".getBytes()); 150 151 session.sendPacket(dataSm); 152 } 153 154 public void packetReceived(Session arg0, SMPPPacket arg1) { 155 System.out.println("Packet Received " + arg1); 156 157 } 158 159 public void update(Session arg0, SMPPEvent arg1) { 160 System.out.println("Update called " + arg1.toString()); 161 } 162 163}