Class: Babylon::Runner

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

Overview

Runner is in charge of running the application.

Class Method Summary collapse

Class Method Details

.add_connection_observer(observer) ⇒ Object

Adding a connection observer. These observer will receive on_connected and on_disconnected events.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/babylon/runner.rb', line 94

def self.add_connection_observer(observer)
  @@observers ||= Array.new 
  if observer.ancestors.include? Babylon::Base::Controller
    Babylon.logger.debug {
      "Added #{observer} to the list of Connection Observers"
    }
    @@observers.push(observer) unless @@observers.include? observer
  else
    Babylon.logger.error {
      "Observer can only be Babylon::Base::Controller"
    }
    false
  end
end

.connection_observersObject

Returns the list of connection observers



88
89
90
# File 'lib/babylon/runner.rb', line 88

def self.connection_observers()
  @@observers ||= Array.new
end

.on_connected(connection) ⇒ Object

Will be called by the connection class once it is connected to the server. It “plugs” the router and then calls on_connected on the various observers.



112
113
114
115
116
117
# File 'lib/babylon/runner.rb', line 112

def self.on_connected(connection)
  Babylon.router.connected(connection)
  connection_observers.each do |observer|
    Babylon.router.execute_route(observer, "on_connected")
  end
end

.on_disconnectedObject

Will be called by the connection class upon disconnection. It stops the event loop and then calls on_disconnected on the various observers.



122
123
124
125
126
127
128
# File 'lib/babylon/runner.rb', line 122

def self.on_disconnected()
  connection_observers.each do |conn_obs|
    observer = conn_obs.new
    observer.on_disconnected if observer.respond_to?("on_disconnected")
  end
  EventMachine.stop_event_loop
end

.on_stanza(stanza) ⇒ Object

Will be called by the connection class when it receives and parses a stanza.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/babylon/runner.rb', line 132

def self.on_stanza(stanza)
  begin
    Babylon.router.route(stanza)
  rescue Babylon::NotConnected
    Babylon.logger.fatal {
      "#{$!.class} => #{$!.inspect}\n#{$!.backtrace.join("\n")}"
    }
    EventMachine::stop_event_loop
  rescue
    Babylon.logger.error {
      "#{$!.class} => #{$!.inspect}\n#{$!.backtrace.join("\n")}"
    }
  end
end

.prepare(env) ⇒ Object

Prepares the Application to run.



9
10
11
12
13
14
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
# File 'lib/babylon/runner.rb', line 9

def self.prepare(env)
  
  config_file = nil
  
  if env.kind_of? Hash
    config_file = File.open( env[:config] ) if env[:config]
  end
  
  # Load the configuration
  config_file = File.open('config/config.yaml') if config_file.nil?
  Babylon.config = YAML.load(config_file)[Babylon.environment]
  
  # Add an outputter to the logger
  log_file = Log4r::RollingFileOutputter.new("#{Babylon.environment}", :filename => "log/#{Babylon.environment}.log", :trunc => false)
  case Babylon.environment
  when "production"
    log_file.formatter = Log4r::PatternFormatter.new(:pattern => "%d (#{Process.pid}) [%l] :: %m", :date_pattern => "%d/%m %H:%M")      
  when "development"
    log_file.formatter = Log4r::PatternFormatter.new(:pattern => "%d (#{Process.pid}) [%l] :: %m", :date_pattern => "%d/%m %H:%M")      
  else
    log_file.formatter = Log4r::PatternFormatter.new(:pattern => "%d (#{Process.pid}) [%l] :: %m", :date_pattern => "%d/%m %H:%M")      
  end
  Babylon.logger.add(log_file)
  
  # Requiring all models, stanza, controllers
  ['app/models/*.rb', 'app/stanzas/*.rb', 'app/controllers/*_controller.rb'].each do |dir|
    Runner.require_directory(dir)
  end
  
  # Create the router
  Babylon.router = Babylon::StanzaRouter.new
  
  # Evaluate routes defined with the new DSL router.
  require 'config/routes.rb'
        
  # Caching views
  Babylon.cache_views
  
end

.require_directory(path) ⇒ Object

Convenience method to require files in a given directory



51
52
53
# File 'lib/babylon/runner.rb', line 51

def self.require_directory(path)
  Dir.glob(path).each { |f| require f }
end

.run(env) ⇒ Object

When run is called, it loads the configuration, the routes and add them into the router It then loads the models. Finally it starts the EventMachine and connect the ComponentConnection You can pass an additional block that will be called upon launching, when the eventmachine has been started.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/babylon/runner.rb', line 60

def self.run(env)
  
  if env.kind_of? Hash
    Babylon.environment = env[:env]
  else
    Babylon.environment = env
  end
  
  # Starting the EventMachine
  EventMachine.epoll
  EventMachine.run do
    
    Runner.prepare(env)
    
    case Babylon.config["application_type"] 
    when "client"
      Babylon::ClientConnection.connect(Babylon.config, self) 
    else # By default, we assume it's a component
      Babylon::ComponentConnection.connect(Babylon.config, self) 
    end
    
    # And finally, let's allow the application to do all it wants to do after we started the EventMachine!
    yield(self) if block_given?
  end
end