Class: Marvin::IRC::Client

Inherits:
AbstractClient show all
Defined in:
lib/marvin/irc/client.rb

Defined Under Namespace

Classes: EMConnection

Constant Summary collapse

@@stopped =
false

Instance Attribute Summary collapse

Attributes inherited from AbstractClient

#channels, #connection_config, #disconnect_expected, #nickname, #nicks, #pass, #port, #server

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractClient

#action, #command, configuration=, configure, #default_channels, #default_channels=, #dispatch, #handle_client_connected, #handle_incoming_error, #handle_incoming_join, #handle_incoming_numeric, #handle_incoming_ping, #handle_nick_taken, #handle_welcome, #host_with_port, #initialize, #join, #msg, #nick, #part, #pong, #process_connect, #process_development, #process_disconnect, #quit, #receive_line, setup, setup?, #setup_handlers

Constructor Details

This class inherits a constructor from Marvin::AbstractClient

Instance Attribute Details

#em_connectionObject

Returns the value of attribute em_connection.



8
9
10
# File 'lib/marvin/irc/client.rb', line 8

def em_connection
  @em_connection
end

Class Method Details

.add_reconnect(c) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/marvin/irc/client.rb', line 136

def add_reconnect(c)
  logger.warn "Adding timer to reconnect to #{c.server}:#{c.port} in 15 seconds"
  EventMachine.add_timer(15) do
    logger.warn "Preparing to reconnect to #{c.server}:#{c.port}"
    connect(c)
  end
end

.connect(c, &blk) ⇒ Object

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
# File 'lib/marvin/irc/client.rb', line 117

def connect(c, &blk)
  c = normalize_connection_options(c)
  raise ArgumentError, "Your connection options must specify a server" if !c.server?
  raise ArgumentError, "Your connection options must specify a port"   if !c.port?
  real_block = blk.present? ? proc { |c| blk.call(connection.client) } : nil
  logger.info "Connecting to #{c.server}:#{c.port} (using ssl: #{c.ssl?}) - Channels: #{c.channels.join(", ")}"
  EventMachine.connect(c.server, c.port, EMConnection, c, &real_block)
end

.run(opts = {}, force = false) ⇒ Object

Starts the EventMachine loop and hence starts up the actual networking portion of the IRC Client.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/marvin/irc/client.rb', line 95

def run(opts = {}, force = false)
  self.development = opts[:development]
  return if @stopped && !force
  self.setup # So we have options etc
  connections_file = Marvin::Settings.root / "config" / "connections.yml"
  connections = Marvin::Nash.load_file(connections_file) rescue nil
  if connections.present?
    # Use epoll if available
    EventMachine.kqueue
    EventMachine.epoll
    EventMachine.run do
      connections.each_pair do |server, configuration|
        connect(configuration.merge(:server => server.to_s))
      end
      Marvin::Distributed::Server.start if Marvin::Distributed::Handler.registered?
      @@stopped = false
    end
  else
    logger.fatal "config/connections.yml couldn't be loaded."
  end
end

.stopObject



126
127
128
129
130
131
132
133
134
# File 'lib/marvin/irc/client.rb', line 126

def stop
  return if @@stopped
  logger.debug "Telling all connections to quit"
  connections.dup.each { |connection| connection.quit }
  logger.debug "Telling Event Machine to Stop"
  EventMachine.stop_event_loop
  logger.debug "Stopped."
  @@stoped = true
end

Instance Method Details

#periodically(timing, event_callback) ⇒ Object

Registers a callback handle that will be periodically run.



162
163
164
# File 'lib/marvin/irc/client.rb', line 162

def periodically(timing, event_callback)
  EventMachine.add_periodic_timer(timing) { dispatch(event_callback.to_sym) }
end

#send_line(*args) ⇒ Object

Client specific details



87
88
89
# File 'lib/marvin/irc/client.rb', line 87

def send_line(*args)
  @em_connection.send_line(*args)
end