smsc_manager 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/smsc_manager/broadcast.rb +12 -5
- data/lib/smsc_manager/version.rb +1 -1
- data/test/smsc_manager_test.rb +7 -3
- metadata +1 -1
@@ -2,16 +2,20 @@
|
|
2
2
|
#require 'monitor'
|
3
3
|
require 'thread'
|
4
4
|
module SmscManager
|
5
|
-
class
|
5
|
+
class BadList < RuntimeError
|
6
6
|
end
|
7
7
|
# this class helps to broadcast sms to list
|
8
8
|
class Broadcast
|
9
9
|
@@MAX_THROUGHPUT =5 # in messages per second
|
10
|
-
attr_accessor :text, :source, :list, :num_threads
|
10
|
+
attr_accessor :text, :source, :list, :num_threads, :attempts, :sent
|
11
11
|
def initialize(txt,lst,src='888')
|
12
|
+
@guard = Mutex.new
|
12
13
|
self.text=txt
|
13
14
|
self.list=lst
|
14
|
-
|
15
|
+
self.attempts=0
|
16
|
+
self.sent=0
|
17
|
+
# puts "lst.size is #{lst.size}"
|
18
|
+
raise BadList.new("use send_sms for lists of size 1") if lst.class==String
|
15
19
|
self.source=src
|
16
20
|
self.calculate_threads
|
17
21
|
@list_queue=Queue.new
|
@@ -39,8 +43,8 @@ class Broadcast
|
|
39
43
|
smsc=SmscManager::SmscConnection.factory
|
40
44
|
c=0
|
41
45
|
thread_mgr=[]
|
42
|
-
|
43
|
-
|
46
|
+
# puts "queue size is #{@list_queue.size} "
|
47
|
+
# puts "queue empty is #{@list_queue.empty?}"
|
44
48
|
while c < self.num_threads
|
45
49
|
thread_mgr << Thread.new(c+=1) { |ctmp|
|
46
50
|
#puts " #{Time.now}Creating retry thread: #{ctmp}"
|
@@ -57,7 +61,10 @@ class Broadcast
|
|
57
61
|
protected
|
58
62
|
def send_sms(smsc,sms)
|
59
63
|
begin
|
64
|
+
|
60
65
|
res= smsc.send(sms)
|
66
|
+
@guard.synchronize { self.attempts +=1
|
67
|
+
self.sent +=1 if res.kind_of? Net::HTTPAccepted }
|
61
68
|
rescue Exception => e
|
62
69
|
puts "Exception found #{e.message}"
|
63
70
|
ensure
|
data/lib/smsc_manager/version.rb
CHANGED
data/test/smsc_manager_test.rb
CHANGED
@@ -43,18 +43,22 @@ class SmscManagerTest < Test::Unit::TestCase
|
|
43
43
|
def test_broadcast_two
|
44
44
|
list = %w(9991 9992)
|
45
45
|
b=SmscManager::Broadcast.new("hello there",list)
|
46
|
+
assert b.attempts ==2, "did not send two sms"
|
46
47
|
end
|
47
48
|
def test_broadcast_five
|
48
|
-
list = %w(9991 9992 9993 9994 9995
|
49
|
+
list = %w(9991 9992 9993 9994 9995)
|
49
50
|
b=SmscManager::Broadcast.new("hello there",list)
|
51
|
+
assert b.attempts ==5, "did not send 5 sms"
|
50
52
|
end
|
51
|
-
def
|
53
|
+
def test_broadcast_12
|
52
54
|
list = %w(9991 9992 9993 9994 9995 9996 99910 99920 99930 99940 99950 99960)
|
53
55
|
b=SmscManager::Broadcast.new("hello there",list)
|
56
|
+
assert b.attempts ==12, "did not send 12 sms"
|
54
57
|
end
|
55
58
|
def test_broadcast_six
|
56
|
-
list = %w(9991 9992 9993 9994 9995 9996
|
59
|
+
list = %w(9991 9992 9993 9994 9995 9996)
|
57
60
|
b=SmscManager::Broadcast.new("second hello there",list)
|
61
|
+
assert b.attempts ==6, "did not send 6 sms"
|
58
62
|
end
|
59
63
|
def test_find_google_mod
|
60
64
|
@smsc.hostname='www.google.com'
|