Class: Vines::Services::Connection

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/vines/services/connection.rb

Overview

Connects the component process to the chat server and provides the protocol used by the web user interface and the agents.

Constant Summary collapse

@@controllers =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/vines/services/connection.rb', line 15

def initialize(options)
  host, port, password, @vhost = *options.values_at(:host, :port, :password, :vhost)
  @stream = Blather::Client.setup(@vhost.name, password, host, port)
  @throttle = Throttle.new(@stream)
  @queues = {}

  @stream.register_handler(:stream_error) do |e|
    log.error(e.message)
    true # prevent EM.stop
  end

  @stream.register_handler(:disconnected) do
    log.info("Stream disconnected, reconnecting . . .")
    EM::Timer.new(10) do
      self.class.new(options).start
    end
    true # prevent EM.stop
  end

  @stream.register_handler(:ready) do
    log.info("Connected #{@stream.jid} component to #{host}:#{port}")
    Fiber.new do
      broadcast_presence
    end.resume
  end

  @stream.register_handler(:iq, '/iq[@type="get"]/ns:query', :ns => 'jabber:iq:version') do |node|
    if node.to == @stream.jid
      iq = Blather::Stanza::Iq::Query.new(:result)
      iq.id, iq.to = node.id, node.from
      iq.query.add_child("<name>Vines Services</name>")
      iq.query.add_child("<version>#{VERSION}</version>")
      @stream.write(iq)
    end
  end

  @@controllers.each do |args, klass|
    @stream.register_handler(*args) do |node|
      jid = node.from.to_s
      start = !@queues.key?(jid)
      queue = (@queues[jid] ||= EM::Queue.new)
      queue.push([node, klass])
      process_node_queue(jid) if start
    end
  end
end

Class Method Details

.register(*args, klass) ⇒ Object



11
12
13
# File 'lib/vines/services/connection.rb', line 11

def self.register(*args, klass)
  @@controllers << [args, klass]
end

Instance Method Details

#startObject



62
63
64
# File 'lib/vines/services/connection.rb', line 62

def start
  @stream.run
end