Class: Cereal::Connection
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- Cereal::Connection
- Defined in:
- lib/connection.rb
Overview
Synopsis
Cereal::Connection represents a connection to an IRC server. It is used internally by Cereal::Bot, and you never need to touch it to use Cereal. Each instance of Cereal::Bot has an attached Cereal::Connection instance that it communicates with the IRC server via.
See the documentation for EventMachine::Connection for more detail.
Author
Ross “Raws” Paffett
Copyright
Copyright © 2009 Ross Paffett. Licensed under the MIT license: www.opensource.org/licenses/mit-license.php
Instance Attribute Summary collapse
-
#channels ⇒ Object
readonly
Returns the value of attribute channels.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#temp_nick ⇒ Object
Returns the value of attribute temp_nick.
Instance Method Summary collapse
-
#initialize(args) ⇒ Connection
constructor
A new instance of Connection.
-
#post_init ⇒ Object
Called upon successfully establishing a socket connection to the IRC server.
-
#receive_data(data) ⇒ Object
Called upon receiving one or more lines of data from the IRC server.
-
#send_data(data) ⇒ Object
Send raw data to the IRC server immediately.
-
#send_raw(data) ⇒ Object
Send raw data to the IRC server through the output queue.
-
#unbind ⇒ Object
Called whenever the connection is closed, whether intentional or not.
Constructor Details
#initialize(args) ⇒ Connection
Returns a new instance of Connection.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/connection.rb', line 25 def initialize(args) # Our parent Cereal::Bot @bot = args[:bot] # Current state of this connection @state = Cereal::Connection::STATE_DISCONNECTED # Details about the server we last connected to @host = nil @password = args[:password] # Hash of channels that is used to remember which users are # in which channels @channels = {} # Hash to temporarily hold channel topics on RPL_TOPIC until # we receive more information on RPL_TOPICINFO @topics = {} # A thread which is responsible for sending messages to the IRC # server in a controlled manner. Its queue prevents a flood of # messages from causing the bot to be kicked from a channel or server. @output_queue = Queue.new @output_thread = Thread.new do while true sleep @bot. send_data(@output_queue.deq) end end # A string buffer to hold incomplete lines (lines not ending in \r\n) # that we receive from the server. @incomplete_line_buffer = '' end |
Instance Attribute Details
#channels ⇒ Object (readonly)
Returns the value of attribute channels.
22 23 24 |
# File 'lib/connection.rb', line 22 def channels @channels end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
22 23 24 |
# File 'lib/connection.rb', line 22 def state @state end |
#temp_nick ⇒ Object
Returns the value of attribute temp_nick.
23 24 25 |
# File 'lib/connection.rb', line 23 def temp_nick @temp_nick end |
Instance Method Details
#post_init ⇒ Object
Called upon successfully establishing a socket connection to the IRC server. Sends initial messages to the server in an attempt to register a valid client connection.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/connection.rb', line 95 def post_init # Attempt to register connection with server @state = Cereal::Connection::STATE_CONNECTING send_data("PASS #{@password}") if !@password.nil? bot_nick = @bot.nick @temp_nick = 'CerealBot' if bot_nick.is_a? Array @temp_nick = bot_nick.shift else @temp_nick = bot_nick end send_data("NICK #{@temp_nick}") send_data("USER #{@bot.login} 0 * :#{@bot.name}") end |
#receive_data(data) ⇒ Object
Called upon receiving one or more lines of data from the IRC server. handle_line is called for each received line of data.
- data
-
The received data.
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/connection.rb', line 79 def receive_data(data) data.each_line do |line| if line[-2,2] != "\r\n" @incomplete_line_buffer << line else line = @incomplete_line_buffer + line @bot.log(line) handle_line(line) @incomplete_line_buffer = '' end end end |
#send_data(data) ⇒ Object
Send raw data to the IRC server immediately. A trailing newline is added automatically.
- data
-
The data to send.
63 64 65 66 67 |
# File 'lib/connection.rb', line 63 def send_data(data) return if data.nil? || @state == Cereal::Connection::STATE_DISCONNECTED super(data + "\r\n") @bot.log(">>>#{data}") end |
#send_raw(data) ⇒ Object
Send raw data to the IRC server through the output queue. Messages are sent every n seconds, where n is @bot.message_delay
. To bypass the output queue, use send_data.
72 73 74 |
# File 'lib/connection.rb', line 72 def send_raw(data) @output_queue.enq(data) end |
#unbind ⇒ Object
Called whenever the connection is closed, whether intentional or not.
111 112 113 114 |
# File 'lib/connection.rb', line 111 def unbind @state = Cereal::Connection::STATE_DISCONNECTED @bot.on_disconnect end |