Class: PunchblockConsole::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/punchblock/console/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CLI

Returns a new instance of CLI.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/punchblock/console/cli.rb', line 14

def initialize(options)
  @options = options
  PunchblockConsole::Logging.start @options.delete(:log_file)
  logger.info "Starting up..."
  @prompt       = options.delete(:prompt)
  @connection   = options.delete(:connection_class).new options
  @client       = Client.new :connection => connection
  @call_queues  = {}

  [:INT, :TERM].each do |signal|
    trap signal do
      logger.info "Shutting down!"
      client.stop
    end
  end
end

Instance Attribute Details

#call_queuesObject (readonly)

Returns the value of attribute call_queues.



12
13
14
# File 'lib/punchblock/console/cli.rb', line 12

def call_queues
  @call_queues
end

#clientObject (readonly)

Returns the value of attribute client.



12
13
14
# File 'lib/punchblock/console/cli.rb', line 12

def client
  @client
end

#connectionObject (readonly)

Returns the value of attribute connection.



12
13
14
# File 'lib/punchblock/console/cli.rb', line 12

def connection
  @connection
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/punchblock/console/cli.rb', line 12

def options
  @options
end

Instance Method Details

#runObject



31
32
33
34
35
36
# File 'lib/punchblock/console/cli.rb', line 31

def run
  run_dispatcher
  client_thread = run_client
  pry if @prompt
  client_thread.join
end

#run_call(client, offer) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/punchblock/console/cli.rb', line 85

def run_call(client, offer)
  ### CALL THREAD
  # One thread is spun up to handle each call.
  Thread.new do
    raise "Unknown call #{offer.call_id}" unless call_queues.has_key?(offer.call_id)
    queue = call_queues[offer.call_id]
    call = queue.pop

    puts "Incoming offer to #{offer.to} from #{offer.from} #{offer}"

    PunchblockConsole::Commands.new(client, offer.call_id, queue).pry

    # Clean up the queue.
    call_queues[offer.call_id] = nil
  end
end

#run_clientObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/punchblock/console/cli.rb', line 38

def run_client
  Thread.new do
    begin
      client.run
    rescue Punchblock::ProtocolError => e
      case e.name
      when 'Blather::Stream::ConnectionFailed'
        puts "The connection to the XMPP server failed. This could be due to a network failure or the server may not be started or accepting connections."
        $option_parser.parse '--help'
      else
        logger.error "Exception in Punchblock client thread! #{e.message}"
        logger.error e.backtrace.join("\t\n")
      end
    end
  end
end

#run_dispatcherObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/punchblock/console/cli.rb', line 55

def run_dispatcher
  ### DISPATCHER THREAD
  # This thread multiplexes the event stream from the underlying connection
  # handler and routes them to the correct queue for each call.  It also starts
  # a call handler, the run_call method) after creating the queue.
  Thread.new do
    loop do
      event = client.event_queue.pop
      case event
      when Connection::Connected
        connection.ready!
        puts "Punchblock connected!"
      when Event::Offer
        raise "Duplicate call ID for #{event.call_id}" if call_queues.has_key?(event.call_id)
        call_queues[event.call_id] = Queue.new
        call_queues[event.call_id].push event
        run_call client, event
      when Event
        if event.call_id
          call_queues[event.call_id].push event
        else
          # puts "Ad-hoc event: #{event.inspect}"
        end
      else
        logger.warn "Unknown event: #{event.inspect}"
      end
    end
  end
end