Class: RSpecRayo::RayoDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-rayo/rayo/driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RayoDriver

Returns a new instance of RayoDriver.



6
7
8
9
10
11
12
13
14
# File 'lib/rspec-rayo/rayo/driver.rb', line 6

def initialize(options)
  @calls            = {}
  @call_queue       = Queue.new
  @queue_timeout    = options[:queue_timeout] || 5
  @write_timeout    = options[:write_timeout] || 5
  @threads          = []

  initialize_rayo options
end

Instance Attribute Details

#call_queueObject (readonly)

Returns the value of attribute call_queue.



3
4
5
# File 'lib/rspec-rayo/rayo/driver.rb', line 3

def call_queue
  @call_queue
end

#callsObject

Returns the value of attribute calls.



4
5
6
# File 'lib/rspec-rayo/rayo/driver.rb', line 4

def calls
  @calls
end

#event_queueObject (readonly)

Returns the value of attribute event_queue.



3
4
5
# File 'lib/rspec-rayo/rayo/driver.rb', line 3

def event_queue
  @event_queue
end

#threadsObject (readonly)

Returns the value of attribute threads.



3
4
5
# File 'lib/rspec-rayo/rayo/driver.rb', line 3

def threads
  @threads
end

Instance Method Details

#cleanup_callsObject



20
21
22
23
24
25
# File 'lib/rspec-rayo/rayo/driver.rb', line 20

def cleanup_calls
  @calls.each_pair do |call_id, call|
    call.hangup unless call.status == :finished
  end
  @calls = {}
end

#dial(options) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/rspec-rayo/rayo/driver.rb', line 27

def dial(options)
  Call.new(:client => @rayo, :queue => Queue.new, :read_timeout => @queue_timeout, :write_timeout => @write_timeout).tap do |call|
    dial = call.dial options
    call.call_id = dial.call_id
    @calls.merge! call.call_id => call
  end
end

#get_callObject



16
17
18
# File 'lib/rspec-rayo/rayo/driver.rb', line 16

def get_call
  read_queue @call_queue
end

#initialize_logging(options) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/rspec-rayo/rayo/driver.rb', line 91

def initialize_logging(options)
  @wire_logger = options[:wire_logger]
  @wire_logger.level = options[:log_level]
  #@wire_logger.info "Starting up..." if @wire_logger

  @transport_logger = options[:transport_logger]
  @transport_logger.level = options[:log_level]
  #@transport_logger.info "Starting up..." if @transport_logger
end

#initialize_rayo(options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rspec-rayo/rayo/driver.rb', line 73

def initialize_rayo(options)
  initialize_logging options

  # Setup our Rayo environment
  connection = Punchblock::Connection::XMPP.new :username         => options[:username],
                                                :password         => options[:password],
                                                :host             => options[:host],
                                                :port             => options[:port],
                                                :wire_logger      => @wire_logger,
                                                :transport_logger => @transport_logger,
                                                :auto_reconnect   => false
  @rayo = Punchblock::Client.new  :connection => connection,
                                  :write_timeout => options[:write_timeout]
  @event_queue = @rayo.event_queue

  start_rayo
end

#read_queue(queue) ⇒ Object



69
70
71
# File 'lib/rspec-rayo/rayo/driver.rb', line 69

def read_queue(queue)
  Timeout::timeout(@queue_timeout) { queue.pop }
end

#start_event_dispatcherObject



35
36
37
38
39
40
41
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/rspec-rayo/rayo/driver.rb', line 35

def start_event_dispatcher
  @threads << Thread.new do
    event = nil

    until event == 'STOP' do
      event = @event_queue.pop
      case event
      when Punchblock::Event::Offer
        call = Call.new :call_event     => event,
                        :client         => @rayo,
                        :queue          => Queue.new,
                        :read_timeout   => @queue_timeout,
                        :write_timeout  => @write_timeout
        @calls.merge! event.call_id => call
        @call_queue.push call
      when Punchblock::Event::Ringing
        call = @calls[event.call_id]
        call.ring_event = event if call
      else
        # Temp based on this nil returned on conference: https://github.com/tropo/punchblock/issues/27
        begin
          if event.is_a?(Punchblock::Event::End)
            @calls[event.call_id].status = :finished
          end
          @calls[event.call_id].queue.push event unless event.nil?
        rescue => error
          # Event nil issue to be addressed here: https://github.com/tropo/rspec-rayo/issues/2
        end
      end
    end

  end
end

#start_rayoObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rspec-rayo/rayo/driver.rb', line 101

def start_rayo
  # Launch the Rayo thread
  @threads << Thread.new do
    begin
      @rayo.run
    rescue => e
      puts "Exception in XMPP thread! #{e.message}"
      puts e.backtrace.join("\t\n")
    end
  end
end