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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/babylon/runner.rb', line 83

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



77
78
79
# File 'lib/babylon/runner.rb', line 77

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.



101
102
103
104
105
106
# File 'lib/babylon/runner.rb', line 101

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.



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

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.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/babylon/runner.rb', line 121

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

def self.prepare(env)
  # Load the configuration
  config_file = File.open('config/config.yaml')
  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



44
45
46
# File 'lib/babylon/runner.rb', line 44

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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/babylon/runner.rb', line 53

def self.run(env)
  
  Babylon.environment = env
  
  # 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