Class: Blur::Client
- Inherits:
-
Object
- Object
- Blur::Client
- Includes:
- Handling, Logging
- Defined in:
- library/blur/client.rb,
library/blur/handling.rb
Overview
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.
Defined Under Namespace
Modules: Handling
Instance Attribute Summary collapse
-
#networks ⇒ Array
A list of instantiated networks.
-
#options ⇒ Array
The options that is passed upon initialization.
-
#scripts ⇒ Array
A list of scripts that is loaded during runtime.
Instance Method Summary collapse
-
#connect ⇒ Object
Connect to each network available that is not already connected, then proceed to start the run-loop.
-
#got_command(network, command) ⇒ Object
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.
-
#initialize(options) ⇒ Client
constructor
Instantiates the client, stores the options, instantiates the networks and then loads available scripts.
-
#load_scripts ⇒ Object
Searches for scripts in working_directory/scripts and then loads them.
-
#network_connection_closed(network) ⇒ Object
Called when a network connection is either closed, or terminated.
-
#quit(signal = :SIGINT) ⇒ Object
Try to gracefully disconnect from each network, unload all scripts and exit properly.
-
#unload_scripts ⇒ Object
Unload all scripts gracefully that have been loaded into the client.
Methods included from Handling
#got_005, #got_channel_topic, #got_end_of_motd, #got_join, #got_kick, #got_mode, #got_name_reply, #got_nick, #got_part, #got_ping, #got_privmsg, #got_quit, #got_topic
Constructor Details
#initialize(options) ⇒ Client
Instantiates the client, stores the options, instantiates the networks and then loads available scripts.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'library/blur/client.rb', line 26 def initialize @options = @scripts = [] @networks = [] @callbacks = {} @networks = @options[:networks].map {|| Network.new } load_scripts trap 2, &method(:quit) end |
Instance Attribute Details
#networks ⇒ Array
Returns a list of instantiated networks.
18 19 20 |
# File 'library/blur/client.rb', line 18 def networks @networks end |
#options ⇒ Array
Returns the options that is passed upon initialization.
14 15 16 |
# File 'library/blur/client.rb', line 14 def @options end |
#scripts ⇒ Array
Returns a list of scripts that is loaded during runtime.
16 17 18 |
# File 'library/blur/client.rb', line 16 def scripts @scripts end |
Instance Method Details
#connect ⇒ Object
Connect to each network available that is not already connected, then proceed to start the run-loop.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'library/blur/client.rb', line 40 def connect networks = @networks.select {|network| not network.connected? } EventMachine.run do EventMachine.error_handler{|e| p e } networks.each do |network| network.delegate = self network.connect end end end |
#got_command(network, command) ⇒ Object
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.
59 60 61 62 63 64 65 66 |
# File 'library/blur/client.rb', line 59 def got_command network, command log "#{'←' ^ :green} #{command.name.to_s.ljust(8, ' ') ^ :light_gray} #{command.params.map(&:inspect).join ' '}" name = :"got_#{command.name.downcase}" if respond_to? name __send__ name, network, command end end |
#load_scripts ⇒ Object
Searches for scripts in working_directory/scripts and then loads them.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'library/blur/client.rb', line 69 def load_scripts # Load script extensions. Script.load_extensions! # Load the scripts. script_path = File.dirname $0 Dir.glob("#{script_path}/scripts/*.rb").each do |path| script = Script.new path script.__client = self @scripts << script end end |
#network_connection_closed(network) ⇒ Object
Called when a network connection is either closed, or terminated.
97 98 99 |
# File 'library/blur/client.rb', line 97 def network_connection_closed network emit :connection_close, network end |
#quit(signal = :SIGINT) ⇒ Object
Try to gracefully disconnect from each network, unload all scripts and exit properly.
105 106 107 108 109 110 111 112 113 114 |
# File 'library/blur/client.rb', line 105 def quit signal = :SIGINT unload_scripts @networks.each do |network| network.transmit :QUIT, "Got SIGINT?" network.disconnect end EventMachine.stop end |
#unload_scripts ⇒ Object
Unload all scripts gracefully that have been loaded into the client.
87 88 89 90 91 92 93 94 |
# File 'library/blur/client.rb', line 87 def unload_scripts # Unload script extensions. Script.unload_extensions! @scripts.each do |script| script.unload! end.clear end |