Class: Circus::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/irc/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(event_manager, config) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
17
# File 'lib/irc/connection.rb', line 13

def initialize(event_manager, config)
  @parser = Parser.new(event_manager)
  @config = config
  @queue = Queue.new
end

Instance Method Details

#connectObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/irc/connection.rb', line 19

def connect
  @socket = TCPSocket.open(@config[:server], @config[:port])
  
  @send_thread = Thread.new { dispatch }
  
  begin
    loop do
      line = nil
      Timeout::timeout(@config[:timeout]) do
        line = @socket.gets(@config[:eol])
      end
      raise "Socket Closed" unless line
      parse line.chomp
    end
  rescue Timeout::Error
    raise PingTimeout
  rescue Interrupt
    @queue.clear
    @queue << "QUIT :Circus-IRC out"
    sleep 1
  ensure
    @send_thread.exit
    @queue.clear
    @socket.close unless @socket.nil? || @socket.closed?
  end
end

#send(message) ⇒ Object



46
47
48
# File 'lib/irc/connection.rb', line 46

def send(message)
  @queue << message
end