class Smpp::Pdu::DeliverSm

Received for MO message or delivery notification

Attributes

data_coding[R]
dest_addr_npi[R]
dest_addr_ton[R]
destination_addr[R]
esm_class[R]
message_state[R]
msg_reference[R]
optional_parameters[R]
priority_flag[R]
protocol_id[R]
receipted_message_id[R]
registered_delivery[R]
replace_if_present_flag[R]
schedule_delivery_time[R]
service_type[R]
short_message[R]
sm_default_msg_id[R]
sm_length[R]
source_addr[R]
source_addr_npi[R]
source_addr_ton[R]
stat[R]
udh[R]
validity_period[R]

Public Class Methods

data_encoder=(encoder) click to toggle source

set an encoder that can be called to yield the data_coding and short_message

# File lib/smpp/pdu/deliver_sm.rb, line 147
def self.data_encoder=(encoder)
  @@encoder = encoder
end
from_wire_data(seq, status, body) click to toggle source
# File lib/smpp/pdu/deliver_sm.rb, line 65
def self.from_wire_data(seq, status, body)
  options = {}
  # brutally unpack it
  options[:service_type], 
  options[:source_addr_ton], 
  options[:source_addr_npi], 
  source_addr, 
  options[:dest_addr_ton], 
  options[:dest_addr_npi], 
  destination_addr, 
  options[:esm_class], 
  options[:protocol_id],
  options[:priority_flag], 
  options[:schedule_delivery_time], 
  options[:validity_period], 
  options[:registered_delivery], 
  options[:replace_if_present_flag], 
  options[:data_coding], 
  options[:sm_default_msg_id],
  options[:sm_length], 
  remaining_bytes = body.unpack('Z*CCZ*CCZ*CCCZ*Z*CCCCCa*')    

  short_message = remaining_bytes.slice!(0...options[:sm_length])

  #everything left in remaining_bytes is 3.4 optional parameters
  options[:optional_parameters] = parse_optional_parameters(remaining_bytes)

  #parse the 'standard' optional parameters for delivery receipts
  options[:optional_parameters].each do |tag, tlv|
    if OPTIONAL_MESSAGE_STATE == tag
      value = tlv[:value].unpack('C')
      options[:message_state] = value[0] if value

    elsif OPTIONAL_RECEIPTED_MESSAGE_ID == tag
      value = tlv[:value].unpack('A*')
      options[:receipted_message_id] = value[0] if value
    end
  end

  # Check to see if body has a 5 bit header
  if short_message.unpack("c")[0] == 5
    options[:udh] = short_message.slice!(0..5).unpack("CCCCCC")
  end

  #Note: if the SM is a delivery receipt (esm_class=4) then the short_message _may_ be in this format:
  # "id:Smsc2013 sub:1 dlvrd:1 submit date:0610171515 done date:0610171515 stat:0 err:0 text:blah"
  # or this format:
  # "4790000000SMSAlert^id:1054BC63 sub:0 dlvrd:1 submit date:0610231217 done date:0610231217 stat:DELIVRD err: text:"
  # (according to the SMPP spec, the format is vendor specific)
  # For example, Tele2 (Norway):
  # "<msisdn><shortcode>?id:10ea34755d3d4f7a20900cdb3349e549 sub:001 dlvrd:001 submit date:0611011228 done date:0611011230 stat:DELIVRD err:000 Text:abc'!10ea34755d3d4f7a20900cdb3349e549"
  if options[:esm_class] == 4
    # id is in the mandatory part
    msg_ref_match = short_message.match(/id:([^ ]*)/)
    # id is not found, search it in the optional part
    msg_ref_match = remaining_bytes.match(/id:([^ ]*)/) if !msg_ref_match
    
    if msg_ref_match
      options[:msg_reference] = msg_ref_match[1]
    end
    
    # stat is the mandatory part
    stat_match = short_message.match(/stat:([^ ]*)/)
    # stat is not found, search it in the optional part
    stat_match = remaining_bytes.match(/stat:([^ ]*)/) if !stat_match
    
    if stat_match
      options[:stat] = stat_match[1]
    end
    
    Smpp::Base.logger.debug "DeliverSM with source_addr=#{source_addr}, destination_addr=#{destination_addr}, msg_reference=#{options[:msg_reference]}, stat=#{options[:stat]}"
  else
    Smpp::Base.logger.debug "DeliverSM with source_addr=#{source_addr}, destination_addr=#{destination_addr}"
  end

  #yield the data_coding and short_message to the encoder if one is set
  short_message = @@encoder.encode(options[:data_coding], short_message) if @@encoder.respond_to?(:encode)

  new(source_addr, destination_addr, short_message, options, seq) 
end
new(source_addr, destination_addr, short_message, options={}, seq=nil) click to toggle source
Calls superclass method Smpp::Pdu::Base::new
# File lib/smpp/pdu/deliver_sm.rb, line 14
def initialize(source_addr, destination_addr, short_message, options={}, seq=nil) 
  
  @udh = options[:udh]      
  @service_type            = options[:service_type]? options[:service_type] :''
  @source_addr_ton         = options[:source_addr_ton]?options[:source_addr_ton]:0 # network specific
  @source_addr_npi         = options[:source_addr_npi]?options[:source_addr_npi]:1 # unknown
  @source_addr             = source_addr
  @dest_addr_ton           = options[:dest_addr_ton]?options[:dest_addr_ton]:1 # international
  @dest_addr_npi           = options[:dest_addr_npi]?options[:dest_addr_npi]:1 # unknown
  @destination_addr        = destination_addr
  @esm_class               = options[:esm_class]?options[:esm_class]:0 # default smsc mode
  @protocol_id             = options[:protocol_id]?options[:protocol_id]:0
  @priority_flag           = options[:priority_flag]?options[:priority_flag]:0
  @schedule_delivery_time  = options[:schedule_delivery_time]?options[:schedule_delivery_time]:''
  @validity_period         = options[:validity_period]?options[:validity_period]:''
  @registered_delivery     = options[:registered_delivery]?options[:registered_delivery]:1 # we want delivery notifications
  @replace_if_present_flag = options[:replace_if_present_flag]?options[:replace_if_present_flag]:0
  @data_coding             = options[:data_coding]?options[:data_coding]:3 # iso-8859-1
  @sm_default_msg_id       = options[:sm_default_msg_id]?options[:sm_default_msg_id]:0
  @short_message           = short_message
  payload                  = @udh ? @udh.to_s + @short_message : @short_message 
  @sm_length               = payload.length

  #fields set for delivery report
  @stat                    = options[:stat]
  @msg_reference           = options[:msg_reference]
  @receipted_message_id    = options[:receipted_message_id]
  @message_state           = options[:message_state]
  @optional_parameters     = options[:optional_parameters]

  pdu_body = sprintf("%s\0%c%c%s\0%c%c%s\0%c%c%c%s\0%s\0%c%c%c%c%c%s", @service_type, @source_addr_ton, @source_addr_npi, @source_addr,
  @dest_addr_ton, @dest_addr_npi, @destination_addr, @esm_class, @protocol_id, @priority_flag, @schedule_delivery_time, @validity_period,
  @registered_delivery, @replace_if_present_flag, @data_coding, @sm_default_msg_id, @sm_length, payload)

  seq ||= next_sequence_number

  super(DELIVER_SM, 0, seq, pdu_body)
end

Public Instance Methods

message_id() click to toggle source
# File lib/smpp/pdu/deliver_sm.rb, line 61
def message_id
  @udh ? @udh[3] : 0
end
part() click to toggle source
# File lib/smpp/pdu/deliver_sm.rb, line 57
def part
  @udh ? @udh[5] : 0
end
total_parts() click to toggle source
# File lib/smpp/pdu/deliver_sm.rb, line 53
def total_parts
  @udh ? @udh[4] : 0
end