Class: Net::TOC::Connection

Inherits:
Object
  • Object
show all
Includes:
Net::TOC
Defined in:
lib/aim/net_toc.rb

Overview

The Connection class handles low-level communication using the TOC protocol. You shouldn’t use it directly.

Constant Summary collapse

FrameType =
{
  :sign_on => 1,
  :data    => 2
}

Constants included from Net::TOC

Debug, ErrorCode

Instance Method Summary collapse

Methods included from Net::TOC

#format_message, #format_screen_name, new

Constructor Details

#initialize(screen_name) ⇒ Connection

Returns a new instance of Connection.



183
184
185
186
# File 'lib/aim/net_toc.rb', line 183

def initialize(screen_name)
  @user = format_screen_name screen_name
  @msgseq = rand(100000)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command, *args) ⇒ Object (private)

Any unknown methods are assumed to be messages for the server.



238
239
240
241
# File 'lib/aim/net_toc.rb', line 238

def method_missing(command, *args)
  puts ([command] + args).join(" ").inspect
  send(([command] + args).join(" "))
end

Instance Method Details

#closeObject



199
200
201
# File 'lib/aim/net_toc.rb', line 199

def close
  @sock.close unless @sock.nil?
end

#open(server = "toc.oscar.aol.com", port = 9898) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/aim/net_toc.rb', line 188

def open(server="toc.oscar.aol.com", port=9898)
  close
  @sock = TCPSocket.new(server, port)

  @sock.send "FLAPON\r\n\r\n", 0

  toc_version = *recv.unpack("N")

  send [1, 1, @user.length, @user].pack("Nnna*"), :sign_on
end

#recvObject

Raises:



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/aim/net_toc.rb', line 217

def recv
  header = @sock.recv 6
  raise CommunicationError, "Server didn't send full header." if header.length < 6

  asterisk, type, serverseq, length = header.unpack "aCnn"

  response = @sock.recv length
  puts "  recv: #{response}" if Debug
  unless type == FrameType[:sign_on]
    message, value = response.split(":", 2)
    unless message.nil? or value.nil?
      msg_sym = message.downcase.to_sym
      yield msg_sym, value if block_given?
    end
  end
  response
end

#send(message, type = :data) ⇒ Object



208
209
210
211
212
213
214
215
# File 'lib/aim/net_toc.rb', line 208

def send(message, type=:data)
  message << "\0"
  puts "  send: #{message}" if Debug
  @msgseq = @msgseq.next
  header = ['*', FrameType[type], @msgseq, message.length].pack("aCnn")
  packet = header + message
  @sock.send packet, 0
end