class Blur::Client
The Client
class is the controller of the low-level access.
It stores networks, scripts and callbacks, and is also encharge of distributing the incoming commands to the right networks and scripts.
Constants
- DEFAULT_CONFIG
The default configuration.
- ENVIRONMENT
The default environment.
- Error
Client
error.
Attributes
@return [Hash] client configuration.
@return [String] the path to the currently used config file.
@return [Array] a list of instantiated networks.
@return [Hash] initialized scripts.
@return [Boolean] whether verbose logging is enabled.
Public Class Methods
Instantiates the client, stores the options, instantiates the networks and then loads available scripts.
@param [Hash] options the options for the client. @option options [String] :config_path path to a configuration file. @option options [String] :environment the client environment.
# File library/blur/client.rb, line 48 def initialize options = {} @scripts = {} @networks = [] @config_path = options[:config_path] @environment = options[:environment] @verbose = options[:verbose] == true unless @config_path raise ConfigError, 'missing config file path in :config_path option' end load_config! networks = @config['blur']['networks'] if networks&.any? networks.each do |network_options| @networks.<< Network.new network_options, self end end trap 2, &method(:quit) end
Public Instance Methods
Connect to each network available that is not already connected, then proceed to start the run-loop.
# File library/blur/client.rb, line 74 def connect networks = @networks.reject &:connected? EventMachine.run do load_scripts! networks.each &:connect EventMachine.error_handler do |exception| log.error "#{exception.message ^ :bold} on line #{exception.line.to_s ^ :bold}" puts exception.backtrace.join "\n" end end end
Is called when a command have been received and parsed, this distributes the command to the loader, which then further distributes it to events and scripts.
@param [Network] network the network that received the command. @param [Network::Command] command the received command.
# File library/blur/client.rb, line 94 def got_message network, message if @verbose log "#{'←' ^ :green} #{message.command.to_s.ljust(8, ' ') ^ :light_gray} #{message.parameters.map(&:inspect).join ' '}" end name = :"got_#{message.command.downcase}" if respond_to? name __send__ name, network, message end end
Instantiates each SuperScript
in the Blur.scripts
list by manually allocating an instance and calling initialize on it, then the instance is stored in +Client#scripts+.
@raise [Exception] any exception that might occur in any scripts'
#initialize method.
# File library/blur/client.rb, line 177 def initialize_superscripts scripts_config = @config['scripts'] scripts_cache_dir = File.expand_path @config['blur']['cache_dir'] Blur.scripts.each do |name, superscript| script = superscript.allocate script.cache = ScriptCache.load name, scripts_cache_dir script.config = scripts_config.fetch name, {} script._client_ref = self script.send :initialize @scripts[name] = script end end
Loads the given file_path
as a Ruby script, wrapping it in an anonymous module to protect our global namespace.
@param [String] file_path the path to the ruby script.
@raise [Exception] if there was any problems loading the file
# File library/blur/client.rb, line 162 def load_script_file file_path load file_path, true rescue Exception => exception warn "The script `#{file_path}' failed to load" warn "#{exception.class}: #{exception.message}" warn '' warn 'Backtrace:', '---', exception.backtrace end
Loads all scripts in the script directory.
# File library/blur/client.rb, line 135 def load_scripts! scripts_dir = File.expand_path @config['blur']['scripts_dir'] script_file_paths = Dir.glob File.join scripts_dir, '*.rb' # Sort the script file paths by file name so they load by alphabetical # order. # # This will make it possible to create a script called '10_database.rb' # which will be loaded before '20_settings.rb' and non-numeric prefixes # will be loaded after that. script_file_paths = script_file_paths.sort do |a, b| File.basename(a) <=> File.basename(b) end script_file_paths.each { |script_path| load_script_file script_path } initialize_superscripts emit :scripts_loaded end
Called when a network connection is either closed, or terminated.
# File library/blur/client.rb, line 106 def network_connection_closed network emit :connection_close, network end
Try to gracefully disconnect from each network, unload all scripts and exit properly.
@param [optional, Symbol] signal The signal received by the system, if any.
# File library/blur/client.rb, line 114 def quit signal = :SIGINT @networks.each do |network| network.transmit :QUIT, 'Got SIGINT?' network.disconnect end EventMachine.stop end
Reloads configuration file and scripts.
# File library/blur/client.rb, line 124 def reload! EM.schedule do unload_scripts! load_config! load_scripts! yield if block_given? end end
Unloads initialized scripts and superscripts.
This method will call unloaded on the instance of each loaded script to give it a chance to clean up any resources.
# File library/blur/client.rb, line 196 def unload_scripts! @scripts.each do |name, script| script.__send__ :unloaded if script.respond_to? :unloaded end.clear Blur.reset_scripts! end
Private Instance Methods
Load the user-specified configuration file.
@returns true on success, false otherwise.
# File library/blur/client.rb, line 209 def load_config! config = YAML.load_file @config_path if config.key? @environment @config = config[@environment] @config.deeper_merge! DEFAULT_CONFIG emit :config_load else raise Error, "No configuration found for specified environment `#{@environment}'" end end