Sha256: 456d4c2f07cdc13b6e7449c308209befa0cfec3d79455382af8362012c4e240a

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

require 'socket'

module Marvin::IRC
  class SocketClient < Marvin::AbstractClient
    attr_accessor :socket
    
    def run
      @socket = TCPSocket.new(self.configuration.server, self.configuration.port)
      self.process_connect
      self.enter_loop
    end
    
    def send_line(*args)
      args.each { |l| @socket.write l } if !@socket.closed?
    end
    
    def disconnect_processed?
      @disconnect_processed
    end
    
    def enter_loop
      until @socket.closed?
        line = @socket.readline.strip
        receive_line line
      end
      self.process_disconnect unless self.disconnect_processed?
      @disconnect_processed = true
    rescue SystemExit
      self.process_disconnect unless self.disconnect_processed?
      @disconnect_processed = true
    rescue Exception => e
      Marvin::ExceptionTracker.log(e)
    end
    
    def quit(*args)
      super(*args)
    end
    
    ## Client specific details
    
    def self.run
      self.setup # So we have options etc
      logger.debug "Connecting to #{self.configuration.server}:#{self.configuration.port}"
      self.new.run
    end
    
    def self.stop
      logger.debug "Telling all connections to quit"
      self.connections.each do |connection|
        connection.quit
        logger.debug "Preparing to close socket"
        connection.socket.close
      end
      logger.debug "Stopped."
    end
    
    # Registers a callback handle that will be periodically run.
    def periodically(timing, event_callback)
      callback = proc { self.dispatch_event event_callback.to_sym }
    end
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
Sutto-marvin-0.1.0.20081014 lib/marvin/irc/socket_client.rb
Sutto-marvin-0.1.0.20081016 lib/marvin/irc/socket_client.rb