PageRenderTime 33ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/MyExample.java

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