Class: SmscManager::Broadcast

Inherits:
Object
  • Object
show all
Defined in:
lib/smsc_manager/broadcast.rb

Overview

this class helps to broadcast sms to list

Constant Summary collapse

@@MAX_THROUGHPUT =

in messages per second

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, lst, txt = nil) ⇒ Broadcast

initialize with list of destionations, texts eg different text for each destination initialize with common text and list of destinations

Raises:



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/smsc_manager/broadcast.rb', line 20

def initialize(src, lst, txt=nil)
  common_setup(src)
  self.list=lst
  self.text=txt
 # puts "lst.size is #{lst.size}"
  raise BadList.new("use send_sms for lists of size 1") if lst.class==String 
  @list_queue=Queue.new
  self.populate_queue(lst) 
  self.send
#  raise InvalidPrefix.new("invalid prefix: valid prefix area:" + Sms.valid ) if !prefix_test
end

Instance Attribute Details

#attemptsObject

old attr_accessor :text, :source, :list, :num_threads, :attempts, :sent



11
12
13
# File 'lib/smsc_manager/broadcast.rb', line 11

def attempts
  @attempts
end

#listObject

old attr_accessor :text, :source, :list, :num_threads, :attempts, :sent



11
12
13
# File 'lib/smsc_manager/broadcast.rb', line 11

def list
  @list
end

#num_threadsObject

old attr_accessor :text, :source, :list, :num_threads, :attempts, :sent



11
12
13
# File 'lib/smsc_manager/broadcast.rb', line 11

def num_threads
  @num_threads
end

#sentObject

old attr_accessor :text, :source, :list, :num_threads, :attempts, :sent



11
12
13
# File 'lib/smsc_manager/broadcast.rb', line 11

def sent
  @sent
end

#sourceObject

old attr_accessor :text, :source, :list, :num_threads, :attempts, :sent



11
12
13
# File 'lib/smsc_manager/broadcast.rb', line 11

def source
  @source
end

#textObject

old attr_accessor :text, :source, :list, :num_threads, :attempts, :sent



11
12
13
# File 'lib/smsc_manager/broadcast.rb', line 11

def text
  @text
end

Instance Method Details

#calculate_threadsObject

calculate number of threads



54
55
56
57
# File 'lib/smsc_manager/broadcast.rb', line 54

def calculate_threads
  self.num_threads = @@MAX_THROUGHPUT  +1  #sleep 1 after every message
  self.num_threads =1 if self.list.size<=@@MAX_THROUGHPUT 
end

#common_setup(src) ⇒ Object



12
13
14
15
16
17
# File 'lib/smsc_manager/broadcast.rb', line 12

def common_setup(src)
  @guard    = Mutex.new  
  self.attempts=0
  self.sent=0
  self.source=src
end

#populate_queue(lst) ⇒ Object

populate thread safe queue



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/smsc_manager/broadcast.rb', line 33

def populate_queue(lst)
   puts "list is #{lst}"
  lst.each {|key, val|   
              # txt=line[1] ||
              # if value set then send it else send default text value  
               txt2 =  val==nil ? self.text : val
               dst =  key
               puts "text is #{txt2} dest is #{dst}"
               begin
               sms=SmscManager::Sms.new(txt2,dst,self.source)
               @list_queue << sms
               rescue  Exception => e
                 puts "bad values dst: #{dst} txt: #{txt2} msg: #{e.message}"
               end
             
            # puts "  dst is #{dst}" 
             }
  puts "populate queue list size #{@list_queue.size}"
end

#sendObject

send using number of threads



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/smsc_manager/broadcast.rb', line 60

def send
 self.calculate_threads
 smsc=SmscManager::SmscConnection.factory
 c=0
 thread_mgr=[]
# puts "queue size is #{@list_queue.size} " 
# puts "queue empty is #{@list_queue.empty?}"
while c < self.num_threads 
   thread_mgr << Thread.new(c+=1)  { |ctmp|
       #puts " #{Time.now}Creating retry thread: #{ctmp}"
       begin 
          sms = @list_queue.pop
          if sms!=nil 
             puts "Thread #{ctmp}  destination: #{sms.destination}  text: #{sms.text}"
             self.send_sms(smsc,sms)
          end
         end until @list_queue.empty?
     }      
 end
  thread_mgr.each { |aThread|  aThread.join if aThread.alive? }  #wait till all end
end