Class: Circus::Agents::Connection

Inherits:
Blather::Client
  • Object
show all
Defined in:
lib/circus/agents/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

Returns a new instance of Connection.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/circus/agents/connection.rb', line 10

def initialize
  super

  @ready_mon = Monitor.new
  @ready_cond = @ready_mon.new_cond
  @thread_handlers = {}
  register_handler :message do |m|
    process_chat_thread(m)
  end
  register_handler :disconnected do
    puts "Disconnected!"
  end
  register_handler :ready do
    @ready_mon.synchronize {
      @ready_cond.signal
    }
  end
end

Instance Attribute Details

#thread_handlersObject (readonly)

Returns the value of attribute thread_handlers.



8
9
10
# File 'lib/circus/agents/connection.rb', line 8

def thread_handlers
  @thread_handlers
end

Instance Method Details

#configure!(config) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/circus/agents/connection.rb', line 29

def configure!(config)
  args = [config.jid, config.password]
  if config.host
    args << config.host
    args << config.port if config.port
  end 

  setup *args
end

#configure_bg!(config) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/circus/agents/connection.rb', line 39

def configure_bg!(config)
  configure!(config)
  
  Thread.new {
    EM.run { 
      begin
        run
      rescue
        puts $!, $@
      end
    }
  }
  @ready_mon.synchronize {
    @ready_cond.wait(2)
  }
end

#register_thread_handler(thread_id, &block) ⇒ Object



56
57
58
# File 'lib/circus/agents/connection.rb', line 56

def register_thread_handler(thread_id, &block)
  thread_handlers[thread_id] = block
end

#remove_thread_handler(thread_id) ⇒ Object



60
61
62
# File 'lib/circus/agents/connection.rb', line 60

def remove_thread_handler(thread_id)
  thread_handlers.delete(thread_id)
end