Class: Smpp::Transceiver

Inherits:
Base
  • Object
show all
Defined in:
lib/smpp/transceiver.rb

Overview

The SMPP Transceiver maintains a bidirectional connection to an SMSC. Provide a config hash with connection options to get started. See the sample_gateway.rb for examples of config values. The transceiver accepts a delegate object that may implement the following (all optional) methods:

mo_received(transceiver, pdu)
delivery_report_received(transceiver, pdu)
message_accepted(transceiver, mt_message_id, pdu)
message_rejected(transceiver, mt_message_id, pdu)
bound(transceiver)
unbound(transceiver)

Constant Summary

Constants included from Smpp

VERSION

Instance Attribute Summary

Attributes inherited from Base

#state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#bound?, #initialize, #logger, logger, logger=, #post_init, #process_pdu, #receive_data, #run_callback, #send_unbind, #start_enquire_link_timer, #unbind, #unbound?

Constructor Details

This class inherits a constructor from Smpp::Base

Class Method Details

.get_message_part_size(options) ⇒ Object

Use data_coding to find out what message part size we can use en.wikipedia.org/wiki/SMS#Message_size



99
100
101
102
103
104
105
106
107
108
# File 'lib/smpp/transceiver.rb', line 99

def self.get_message_part_size options
  return 153 if options[:data_coding].nil?
  return 153 if options[:data_coding] == 0
  return 134 if options[:data_coding] == 3
  return 134 if options[:data_coding] == 5
  return 134 if options[:data_coding] == 6
  return 134 if options[:data_coding] == 7
  return 67  if options[:data_coding] == 8
  return 153
end

Instance Method Details

#send_bindObject

Send BindTransceiverResponse PDU.

Raises:

  • (IOError)


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/smpp/transceiver.rb', line 85

def send_bind
  raise IOError, 'Receiver already bound.' unless unbound?
  pdu = Pdu::BindTransceiver.new(
      @config[:system_id], 
      @config[:password],
      @config[:system_type], 
      @config[:source_ton], 
      @config[:source_npi], 
      @config[:source_address_range])
  write_pdu(pdu)
end

#send_concat_mt(message_id, source_addr, destination_addr, message, options = {}) ⇒ Object

Send a concatenated message with a body of > 160 characters as multiple messages.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/smpp/transceiver.rb', line 33

def send_concat_mt(message_id, source_addr, destination_addr, message, options = {})
  logger.debug "Sending concatenated MT: #{message}"
  if @state == :bound
    # Split the message into parts of 153 characters. (160 - 7 characters for UDH)
    parts = []
    while message.size > 0 do
      parts << message.slice!(0..Smpp::Transceiver.get_message_part_size(options))
    end
    
    0.upto(parts.size-1) do |i|
      udh = sprintf("%c", 5)            # UDH is 5 bytes.
      udh << sprintf("%c%c", 0, 3)      # This is a concatenated message 

      #TODO Figure out why this needs to be an int here, it's a string elsewhere
      udh << sprintf("%c", message_id)  # The ID for the entire concatenated message

      udh << sprintf("%c", parts.size)  # How many parts this message consists of
      udh << sprintf("%c", i+1)         # This is part i+1
      
      options[:esm_class] = 64 # This message contains a UDH header.
      options[:udh] = udh

      pdu = Pdu::SubmitSm.new(source_addr, destination_addr, parts[i], options)
      write_pdu pdu
      
      # This is definately a bit hacky - multiple PDUs are being associated with a single
      # message_id.
      @ack_ids[pdu.sequence_number] = message_id
    end
  else
    raise InvalidStateException, "Transceiver is unbound. Connot send MT messages."
  end
end

#send_mt(message_id, source_addr, destination_addr, short_message, options = {}) ⇒ Object

Send an MT SMS message. Delegate will receive message_accepted callback when SMSC acknowledges, or the message_rejected callback upon error



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/smpp/transceiver.rb', line 18

def send_mt(message_id, source_addr, destination_addr, short_message, options={})
  logger.debug "Sending MT: #{short_message}"
  if @state == :bound
    pdu = Pdu::SubmitSm.new(source_addr, destination_addr, short_message, options)
    write_pdu pdu

    # keep the message ID so we can associate the SMSC message ID with our message
    # when the response arrives.      
    @ack_ids[pdu.sequence_number] = message_id
  else
    raise InvalidStateException, "Transceiver is unbound. Cannot send MT messages."
  end
end

#send_multi_mt(message_id, source_addr, destination_addr_arr, short_message, options = {}) ⇒ Object

Send MT SMS message for multiple dest_address Author: Abhishek Parolkar (abhishekparolkar.com) USAGE: $tx.send_multi_mt(123, “9100000000”, [“9199000000000”,“91990000000001”,“9199000000002”], “Message here”)



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/smpp/transceiver.rb', line 70

def send_multi_mt(message_id, source_addr, destination_addr_arr, short_message, options={})
  logger.debug "Sending Multiple MT: #{short_message}"
  if @state == :bound
    pdu = Pdu::SubmitMulti.new(source_addr, destination_addr_arr, short_message, options)
    write_pdu pdu

    # keep the message ID so we can associate the SMSC message ID with our message
    # when the response arrives.      
    @ack_ids[pdu.sequence_number] = message_id
  else
    raise InvalidStateException, "Transceiver is unbound. Cannot send MT messages."
  end
end