Class: Blur::Client

Inherits:
Object
  • Object
show all
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)

Instance Method Summary (collapse)

Methods included from Handling

#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

- (Client) initialize(options)

Instantiates the client, stores the options, instantiates the networks and then loads available scripts.

Parameters:

  • options (Hash)

    the options for the client.

Options Hash (options):

  • networks (Array)

    list of hashes that contain network options.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'library/blur/client.rb', line 26

def initialize options
  @options   = options
  @scripts   = []
  @networks  = []
  @callbacks = {}
  
  @networks = @options[:networks].map {|options| Network.new options }
  
  load_scripts
  trap 2, &method(:quit)

  EventMachine.threadpool_size = 1
end

Instance Attribute Details

- (Array) networks

Returns a list of instantiated networks.

Returns:

  • (Array)

    a list of instantiated networks.



18
19
20
# File 'library/blur/client.rb', line 18

def networks
  @networks
end

- (Array) options

Returns the options that is passed upon initialization.

Returns:

  • (Array)

    the options that is passed upon initialization.



14
15
16
# File 'library/blur/client.rb', line 14

def options
  @options
end

- (Array) scripts

Returns a list of scripts that is loaded during runtime.

Returns:

  • (Array)

    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

- (Object) connect

Connect to each network available that is not already connected, then proceed to start the run-loop.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'library/blur/client.rb', line 42

def connect
  networks = @networks.select {|network| not network.connected? }
  
  EventMachine.run do
    networks.each do |network|
      network.delegate = self
      network.connect
    end

    EventMachine.error_handler{|e| p e }
  end
end

- (Object) got_command(network, command)

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.

Parameters:

  • network (Network)

    the network that received the command.

  • command (Network::Command)

    the received command.



61
62
63
64
65
66
67
68
# File 'library/blur/client.rb', line 61

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

- (Object) load_scripts

Searches for scripts in working_directory/scripts and then loads them.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'library/blur/client.rb', line 71

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

- (Object) network_connection_closed(network)

Called when a network connection is either closed, or terminated.



99
100
101
# File 'library/blur/client.rb', line 99

def network_connection_closed network
  emit :connection_close, network
end

- (Object) quit(signal = :SIGINT)

Try to gracefully disconnect from each network, unload all scripts and exit properly.

Parameters:

  • signal (optional, Symbol) (defaults to: :SIGINT)

    The signal received by the system, if any.



107
108
109
110
111
112
113
114
115
116
# File 'library/blur/client.rb', line 107

def quit signal = :SIGINT
  @networks.each do |network|
    network.transmit :QUIT, "Got SIGINT?"
    network.disconnect
  end
  
  unload_scripts
  
  EventMachine.stop
end

- (Object) unload_scripts

Unload all scripts gracefully that have been loaded into the client.

See Also:



89
90
91
92
93
94
95
96
# File 'library/blur/client.rb', line 89

def unload_scripts
  # Unload script extensions.
  Script.unload_extensions!

  @scripts.each do |script|
    script.unload!
  end.clear
end