ruby-smpp 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -17,6 +17,7 @@ tmtags
17
17
  coverage
18
18
  rdoc
19
19
  pkg
20
+ tags
20
21
 
21
22
  ## PROJECT::SPECIFIC
22
23
  *.log
data/Rakefile CHANGED
@@ -33,7 +33,7 @@ begin
33
33
  require 'rcov/rcovtask'
34
34
  Rcov::RcovTask.new do |test|
35
35
  test.libs << 'test'
36
- test.pattern = 'test/**/test_*.rb'
36
+ test.pattern = 'test/**/*_test.rb'
37
37
  test.verbose = true
38
38
  end
39
39
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.2.0
data/lib/smpp.rb CHANGED
@@ -10,6 +10,7 @@ require 'logger'
10
10
  $:.unshift(File.dirname(__FILE__))
11
11
  require 'smpp/base.rb'
12
12
  require 'smpp/transceiver.rb'
13
+ require 'smpp/optional_parameter'
13
14
  require 'smpp/pdu/base.rb'
14
15
  require 'smpp/pdu/bind_base.rb'
15
16
  require 'smpp/pdu/bind_resp_base.rb'
data/lib/smpp/base.rb CHANGED
@@ -165,7 +165,10 @@ module Smpp
165
165
  end
166
166
 
167
167
  def hex_debug(data, prefix = "")
168
- return unless @config[:hex_debug]
168
+ Base.hex_debug(data, prefix)
169
+ end
170
+
171
+ def Base.hex_debug(data, prefix = "")
169
172
  logger.debug do
170
173
  message = "Hex dump follows:\n"
171
174
  hexdump(data).each_line do |line|
@@ -175,7 +178,7 @@ module Smpp
175
178
  end
176
179
  end
177
180
 
178
- def hexdump(target)
181
+ def Base.hexdump(target)
179
182
  width=16
180
183
  group=2
181
184
 
@@ -0,0 +1,31 @@
1
+ class Smpp::OptionalParameter
2
+
3
+ attr_reader :tag, :value
4
+
5
+ def initialize(tag, value)
6
+ @tag = tag
7
+ @value = value
8
+ end
9
+
10
+ def [](symbol)
11
+ self.send symbol
12
+ end
13
+
14
+ #class methods
15
+ class << self
16
+ def from_wire_data(data)
17
+
18
+ return nil if data.nil?
19
+ tag, length, remaining_bytes = data.unpack('H4na*')
20
+ tag = tag.hex
21
+
22
+ raise "invalid data, cannot parse optional parameters" if tag == 0 or length.nil?
23
+
24
+ value = remaining_bytes.slice!(0...length)
25
+
26
+ return new(tag, value), remaining_bytes
27
+ end
28
+
29
+ end
30
+
31
+ end
data/lib/smpp/pdu/base.rb CHANGED
@@ -98,11 +98,32 @@ module Smpp::Pdu
98
98
  end
99
99
 
100
100
  # return int as binary string of 4 octets
101
- def fixed_int(value)
101
+ def Base.fixed_int(value)
102
102
  arr = [value >> 24, value >> 16, value >> 8, value & 0xff]
103
103
  arr.pack("cccc")
104
104
  end
105
105
 
106
+ def fixed_int(value)
107
+ Base.fixed_int(value)
108
+ end
109
+
110
+ #expects a hash like {tag => Smpp::OptionalParameter}
111
+ def Base.optional_parameters_to_buffer(optionals)
112
+ output = ""
113
+ optionals.each do |tag, optional_param|
114
+ length = optional_param.value.length
115
+ buffer = []
116
+ buffer += [tag >> 8, tag & 0xff]
117
+ buffer += [length >> 8, length & 0xff]
118
+ output << buffer.pack('cccc') << optional_param.value
119
+ end
120
+ output
121
+ end
122
+
123
+ def optional_parameters_to_buffer(optionals)
124
+ Base.optional_parameters_to_buffer(optionals)
125
+ end
126
+
106
127
  def next_sequence_number
107
128
  Base.next_sequence_number
108
129
  end
@@ -147,10 +168,8 @@ module Smpp::Pdu
147
168
  optionals = {}
148
169
  while not remaining_bytes.empty?
149
170
  optional = {}
150
- tag, optional[:length], remaining_bytes = remaining_bytes.unpack('H4na*')
151
- optional[:tag] = tag.hex
152
- optional[:value] = remaining_bytes.slice!(0...optional[:length])
153
- optionals[tag.hex] = optional
171
+ optional_parameter, remaining_bytes = Smpp::OptionalParameter.from_wire_data(remaining_bytes)
172
+ optionals[optional_parameter.tag] = optional_parameter
154
173
  end
155
174
 
156
175
  return optionals
@@ -4,7 +4,7 @@ class Smpp::Pdu::SubmitSm < Smpp::Pdu::Base
4
4
  attr_reader :service_type, :source_addr_ton, :source_addr_npi, :source_addr, :dest_addr_ton, :dest_addr_npi,
5
5
  :destination_addr, :esm_class, :protocol_id, :priority_flag, :schedule_delivery_time,
6
6
  :validity_period, :registered_delivery, :replace_if_present_flag, :data_coding,
7
- :sm_default_msg_id, :sm_length, :udh, :short_message
7
+ :sm_default_msg_id, :sm_length, :udh, :short_message, :optional_parameters
8
8
 
9
9
 
10
10
  # Note: short_message (the SMS body) must be in iso-8859-1 format
@@ -33,11 +33,17 @@ class Smpp::Pdu::SubmitSm < Smpp::Pdu::Base
33
33
  payload = @udh ? @udh + @short_message : @short_message
34
34
  @sm_length = payload.length
35
35
 
36
+ @optional_parameters = options[:optional_parameters]
37
+
36
38
  # craft the string/byte buffer
37
39
  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,
38
40
  @dest_addr_ton, @dest_addr_npi, @destination_addr, @esm_class, @protocol_id, @priority_flag, @schedule_delivery_time, @validity_period,
39
41
  @registered_delivery, @replace_if_present_flag, @data_coding, @sm_default_msg_id, @sm_length, payload)
40
42
 
43
+ if @optional_parameters
44
+ pdu_body << optional_parameters_to_buffer(@optional_parameters)
45
+ end
46
+
41
47
  seq ||= next_sequence_number
42
48
 
43
49
  super(SUBMIT_SM, 0, seq, pdu_body)
@@ -70,8 +76,14 @@ class Smpp::Pdu::SubmitSm < Smpp::Pdu::Base
70
76
  options[:data_coding],
71
77
  options[:sm_default_msg_id],
72
78
  options[:sm_length],
73
- short_message = body.unpack('Z*CCZ*CCZ*CCCZ*Z*CCCCCa*')
74
- Smpp::Base.logger.debug "DeliverSM with source_addr=#{source_addr}, destination_addr=#{destination_addr}"
79
+ remaining_bytes = body.unpack('Z*CCZ*CCZ*CCCZ*Z*CCCCCa*')
80
+
81
+ short_message = remaining_bytes.slice!(0...options[:sm_length])
82
+
83
+ #everything left in remaining_bytes is 3.4 optional parameters
84
+ options[:optional_parameters] = optional_parameters(remaining_bytes)
85
+
86
+ Smpp::Base.logger.debug "SubmitSM with source_addr=#{source_addr}, destination_addr=#{destination_addr}"
75
87
 
76
88
  new(source_addr, destination_addr, short_message, options, seq)
77
89
  end
data/ruby-smpp.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ruby-smpp}
8
- s.version = "0.1.3"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ray Krueger", "August Z. Flatby"]
12
- s.date = %q{2010-02-05}
12
+ s.date = %q{2010-02-11}
13
13
  s.description = %q{Ruby implementation of the SMPP protocol, based on EventMachine. SMPP is a protocol that allows ordinary people outside the mobile network to exchange SMS messages directly with mobile operators.}
14
14
  s.email = %q{raykrueger@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "examples/sample_smsc.rb",
32
32
  "lib/smpp.rb",
33
33
  "lib/smpp/base.rb",
34
+ "lib/smpp/optional_parameter.rb",
34
35
  "lib/smpp/pdu/base.rb",
35
36
  "lib/smpp/pdu/bind_base.rb",
36
37
  "lib/smpp/pdu/bind_resp_base.rb",
@@ -51,7 +52,9 @@ Gem::Specification.new do |s|
51
52
  "lib/smpp/transceiver.rb",
52
53
  "lib/sms.rb",
53
54
  "ruby-smpp.gemspec",
54
- "test/smpp_test.rb"
55
+ "test/optional_parameter_test.rb",
56
+ "test/smpp_test.rb",
57
+ "test/submit_sm_test.rb"
55
58
  ]
56
59
  s.homepage = %q{http://github.com/raykrueger/ruby-smpp}
57
60
  s.rdoc_options = ["--charset=UTF-8"]
@@ -60,7 +63,9 @@ Gem::Specification.new do |s|
60
63
  s.rubygems_version = %q{1.3.5}
61
64
  s.summary = %q{Ruby implementation of the SMPP protocol, based on EventMachine.}
62
65
  s.test_files = [
63
- "test/smpp_test.rb",
66
+ "test/optional_parameter_test.rb",
67
+ "test/smpp_test.rb",
68
+ "test/submit_sm_test.rb",
64
69
  "examples/sample_gateway.rb",
65
70
  "examples/sample_smsc.rb"
66
71
  ]
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'smpp'
4
+
5
+ class OptionalParameterTest < Test::Unit::TestCase
6
+ include Smpp
7
+
8
+ def test_symbol_accessor
9
+ op = OptionalParameter.new(0x2150, "abcd")
10
+ assert_equal "abcd", op[:value]
11
+ assert_equal 0x2150, op[:tag]
12
+ end
13
+
14
+ def test_bad_data_does_not_puke
15
+ assert_raise RuntimeError do
16
+ OptionalParameter.from_wire_data("")
17
+ end
18
+ end
19
+
20
+ def test_from_wire_data
21
+ data = "\041\120\000\002\001\177"
22
+ op, remaining_data = OptionalParameter.from_wire_data(data)
23
+ assert_not_nil op, "op should not be nil"
24
+ assert_equal 0x2150, op.tag
25
+ assert_equal "\001\177", op.value
26
+ assert_equal "", remaining_data
27
+ end
28
+
29
+ end
30
+
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'smpp'
4
+
5
+ class SubmitSmTest < Test::Unit::TestCase
6
+
7
+ def test_this_is_not_a_real_test
8
+ value = [383 >> 8, 383 & 0xff]
9
+ optionals = {0x2150 => Smpp::OptionalParameter.new(0x2150, value.pack('cc'))}
10
+ pdu = Smpp::Pdu::SubmitSm.new('12345', '54321', "Ba Ba Boosh", {:optional_parameters => optionals})
11
+ Smpp::Base.hex_debug(pdu.data)
12
+ puts "PDU DATA", pdu.data.inspect
13
+
14
+ end
15
+
16
+ def test_fixnum_optional_parameter
17
+ value = [383 >> 8, 383 & 0xff]
18
+ optionals = {0x2150 => Smpp::OptionalParameter.new(0x2150, value.pack('cc'))}
19
+
20
+ pdu = Smpp::Pdu::SubmitSm.new('12345', '54321', "Ba Ba Boosh", {:optional_parameters => optionals})
21
+ pdu_from_wire = Smpp::Pdu::Base.create(pdu.data)
22
+
23
+ assert optional = pdu_from_wire.optional_parameters[0x2150]
24
+
25
+ optional_value = optional[:value].unpack('n')[0]
26
+ assert_equal 383, optional_value
27
+ end
28
+
29
+ def test_string_optional_parameter
30
+ optionals = {0x2150 => Smpp::OptionalParameter.new(0x2150, "boosh")}
31
+
32
+ pdu = Smpp::Pdu::SubmitSm.new('12345', '54321', "Ba Ba Boosh", {:optional_parameters => optionals})
33
+ pdu_from_wire = Smpp::Pdu::Base.create(pdu.data)
34
+
35
+ assert optional = pdu_from_wire.optional_parameters[0x2150]
36
+
37
+ optional_value = optional[:value].unpack("A*")[0]
38
+ assert_equal 'boosh', optional_value
39
+ end
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-smpp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ray Krueger
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-02-05 00:00:00 -06:00
13
+ date: 2010-02-11 00:00:00 -06:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -47,6 +47,7 @@ files:
47
47
  - examples/sample_smsc.rb
48
48
  - lib/smpp.rb
49
49
  - lib/smpp/base.rb
50
+ - lib/smpp/optional_parameter.rb
50
51
  - lib/smpp/pdu/base.rb
51
52
  - lib/smpp/pdu/bind_base.rb
52
53
  - lib/smpp/pdu/bind_resp_base.rb
@@ -67,7 +68,9 @@ files:
67
68
  - lib/smpp/transceiver.rb
68
69
  - lib/sms.rb
69
70
  - ruby-smpp.gemspec
71
+ - test/optional_parameter_test.rb
70
72
  - test/smpp_test.rb
73
+ - test/submit_sm_test.rb
71
74
  has_rdoc: true
72
75
  homepage: http://github.com/raykrueger/ruby-smpp
73
76
  licenses: []
@@ -97,6 +100,8 @@ signing_key:
97
100
  specification_version: 3
98
101
  summary: Ruby implementation of the SMPP protocol, based on EventMachine.
99
102
  test_files:
103
+ - test/optional_parameter_test.rb
100
104
  - test/smpp_test.rb
105
+ - test/submit_sm_test.rb
101
106
  - examples/sample_gateway.rb
102
107
  - examples/sample_smsc.rb