Class: C3s::ComponentConnection

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(componentclass, options = {}) ⇒ ComponentConnection

Returns a new instance of ComponentConnection.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/component_connection.rb', line 3

def initialize(componentclass, options={})
  @no_db = options[:no_db]
  if !componentclass.superclass.eql?(C3s::Component) and !componentclass.superclass.eql?(C3s::Republisher)
    puts "ERROR: #{componentclass} is not a valid C3s::Component (seems to be a #{componentclass.superclass})"
    exit
  end
  
  reader = C3s::ConfigReader.new(options)
  puts "Reading configurations from yaml file..."
  @server_config = reader.section('server')
  @client_config = reader.section('client')
  
  # a jid parameter is usefull, so lets merge it on client configuration
  @client_config.merge!('jid' => @client_config['name']+"."+@server_config['url'])
  @db_config = reader.section('db') if !@no_db
  
  puts "Creating new component with jid '#{@client_config['jid']}'..."
  @component = componentclass.new(@server_config.merge(@client_config))
end

Class Method Details

.stopObject



71
72
73
# File 'lib/component_connection.rb', line 71

def self.stop
  puts "Stop aqui!!"
end

Instance Method Details

#connectObject



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
68
69
# File 'lib/component_connection.rb', line 39

def connect
  puts "Connecting to #{@server_config['url']} (port #{@server_config['port']})..."
        
  begin
    @component.connect
  rescue Jabber::ComponentAuthenticationFailure => e
    puts "Could not authenticate on #{@server_config['url']}: #{e}"
    exit
  rescue Errno::ECONNREFUSED => e
    puts "Connection refused on #{@server_config['url']} (port #{@server_config['port']})"
    exit
  rescue Exception => e
    puts "ERROR: Couldn't connect to server!"
    puts "#{e.inspect} #{e.backtrace.join("\n")}"
    $LOG.error "Couldn't connect to server!"
    exit
  end
  puts "External component #{@client_config['jid']} connected successfully."
  
  # TODO - use this to do something useful?
  @component.on_exception do |e, component, where|
    component.disconnect
    $LOG.error e.backtrace.join("\n") if e
    exit 1
  end
  
  puts "Starting messaging loop..."
  Thread.abort_on_exception = true
  @component.add_callbacks
  Thread.stop
end

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/component_connection.rb', line 23

def start
  if !@no_db
    puts "Connecting to database '#{@db_config['database']}'"
    begin
      # activerecord connection
      DatabaseAdapter.new(@db_config)
      # raises exception if not connected
      ActiveRecord::Base.retrieve_connection
    rescue Exception => e
      puts "Error: #{e}"
      exit 1
    end
  end
  connect
end