lib/marvin/irc/client.rb in Sutto-marvin-0.1.20081120 vs lib/marvin/irc/client.rb in Sutto-marvin-0.2.0

- old
+ new

@@ -44,10 +44,12 @@ # To add an object as a handler, you simply call # the class method, register_handler with the # handler as the only argument. class Client < Marvin::AbstractClient cattr_accessor :stopped + self.stopped = false + attr_accessor :em_connection class EMConnection < EventMachine::Protocols::LineAndTextProtocol attr_accessor :client, :server, :port @@ -82,14 +84,16 @@ ## Client specific details # Starts the EventMachine loop and hence starts up the actual # networking portion of the IRC Client. - def self.run + def self.run(force = false) + return if self.stopped && !force self.setup # So we have options etc settings = YAML.load_file(Marvin::Settings.root / "config/connections.yml") if settings.is_a?(Hash) + # Use epoll if available EventMachine.epoll EventMachine::run do settings.each do |name, options| settings = options.symbolize_keys! settings[:server] ||= name @@ -103,20 +107,27 @@ end def self.connect(opts = {}) logger.info "Connecting to #{opts[:server]}:#{opts[:port]} - Channels: #{opts[:channels].join(", ")}" EventMachine::connect(opts[:server], opts[:port], EMConnection, opts) - logger.info "Connection created for #{opts[:server]}:#{opts[:port]}" end def self.stop return if self.stopped logger.debug "Telling all connections to quit" self.connections.dup.each { |connection| connection.quit } logger.debug "Telling Event Machine to Stop" EventMachine::stop_event_loop logger.debug "Stopped." self.stopped = true + end + + def self.add_reconnect(opts = {}) + Marvin::Logger.warn "Adding entry to reconnect to #{opts[:server]}:#{opts[:port]} in 15 seconds" + EventMachine::add_timer(15) do + Marvin::Logger.warn "Attempting to reconnect to #{opts[:server]}:#{opts[:port]}" + Marvin::IRC::Client.connect(opts) + end end # Registers a callback handle that will be periodically run. def periodically(timing, event_callback) callback = proc { self.dispatch event_callback.to_sym } \ No newline at end of file