Class: Receive

Inherits:
Object
  • Object
show all
Defined in:
lib/smsruby/receive.rb

Overview

The Receive class represent the receive layer. Handles the reception of messages and the initialization of multiples threads to allow multiples connections to receive messages simultaniously

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, time) ⇒ Receive

Obtains and instance of the Connection Administrator to control all existing connections. Due to the use of a singleton in the administrator, the same created instance will be obtain, or will be created if an instance doesn’t exist. The type of receive and the period of time that will be used are passed to initialize method. An Exception is thrown if the instance of Admconnection fail



29
30
31
32
33
34
35
# File 'lib/smsruby/receive.rb', line 29

def initialize(type,time)
    begin
      @receivetype=type
      @time =time
      @adm = AdmConnection.instance
    end
end

Instance Attribute Details

#admObject (readonly)

Reference an instance for the Connection Administrator



13
14
15
# File 'lib/smsruby/receive.rb', line 13

def adm
  @adm
end

#listObject (readonly)

Reference the list of messages



15
16
17
# File 'lib/smsruby/receive.rb', line 15

def list
  @list
end

#receivetypeObject

Reference the type of receive that will be used (0: receive for a defined period of time, 1: receive messages ones )



17
18
19
# File 'lib/smsruby/receive.rb', line 17

def receivetype
  @receivetype
end

#timeObject

Represent the time in seconds that a particular connection will be receiving messages (for receivetype==0)



19
20
21
# File 'lib/smsruby/receive.rb', line 19

def time
  @time
end

Instance Method Details

#receive(imeis) ⇒ Object

Represent the receive method of the Receive class. Will create the reception threads acording with the imeis values passed as an argument. if imeis passed are nil will be created as many threads as connections configured to receive are found



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/smsruby/receive.rb', line 42

def receive(imeis)
  begin
    if @adm.avlconn
      if imeis
        imeis.each do |i|
          if @adm.get_connections.inject(false){|res,act| (act[1].phone_imei==i.to_s and act[1].status=='available' and (act[1].typec=='r' or act[1].typec=='sr')) || res}
            t=Thread.new{receive_internal(i.to_s){|x,y| yield x,y if block_given? }}
            t[:type]='r'
          else
            @adm.log.warn "Can't receive message in connection with imei: #{i}." unless @adm.log.nil?
          end
        end
      else
        @adm.get_connections.select{ |n,act| (act.typec=='r' or act.typec=='sr') and act.status=='available'}.each do |i|
          t=Thread.new{receive_internal(i[1].phone_imei){|x,y| yield x,y if block_given? }}
          t[:type]='r'
        end
      end
    else
      raise "There are no active connections"
    end
  rescue Exception => e
    @adm.log.error "Error receiving messages #{e.message}. Detail: #{e.message}" unless @adm.log.nil?
    raise e
  end
end

#receive_internal(imei) ⇒ Object

Represent the method associated to the receive threads. Will handle the BD writting of the received messages and will make the call of recieve method in the Connection Administrator



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/smsruby/receive.rb', line 74

def receive_internal(imei)
  begin
    db = SQLite3::Database.new('prueba.s3db')
    @adm.receive(to_hash(imei)){ |x,y|
      yield x,y if block_given?
      db.execute("INSERT INTO inbox (smsdate,phone_number,text) VALUES " + "('#{x.date}', '#{x.source_number}','#{x.text}');")
    }
  rescue Exception => e
    puts e.message
  end
end

#to_hash(imei) ⇒ Object

Combine all option values into a hash to relate them



89
90
91
92
93
94
95
# File 'lib/smsruby/receive.rb', line 89

def to_hash(imei)
    { :type => 'receive',
      :imei => imei.gsub(/[\s\D]/, ""),
      :receivetype => self.receivetype,
      :time => self.time
      }
end