Class: Raval::Connection

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

Overview

wraps a raw TCP socket and simplifies working with FTP style data.

FTP commands and responses are always a single line ending in a line break, so make it easy to read and write single lines.

Constant Summary collapse

BUFFER_SIZE =
1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler, socket) ⇒ Connection

Returns a new instance of Connection.



15
16
17
18
19
20
21
# File 'lib/raval/connection.rb', line 15

def initialize(handler, socket)
  @socket, @handler = socket, handler
  _, @port, @host = socket.peeraddr
  _, @myport, @myhost = socket.addr
  handler.new_connection(self)
  puts "*** Received connection from #{host}:#{port}"
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



12
13
14
# File 'lib/raval/connection.rb', line 12

def host
  @host
end

#myhostObject (readonly)

Returns the value of attribute myhost.



13
14
15
# File 'lib/raval/connection.rb', line 13

def myhost
  @myhost
end

#myportObject (readonly)

Returns the value of attribute myport.



13
14
15
# File 'lib/raval/connection.rb', line 13

def myport
  @myport
end

#portObject (readonly)

Returns the value of attribute port.



12
13
14
# File 'lib/raval/connection.rb', line 12

def port
  @port
end

Instance Method Details

#closeObject

Close the connection



32
33
34
# File 'lib/raval/connection.rb', line 32

def close
  @socket.close
end

#read_commandsObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/raval/connection.rb', line 36

def read_commands
  input = ""
  while true
    input << @socket.readpartial(BUFFER_SIZE)
    match = input.match(/(^.+\r\n)/)
    if match
      line  = match[1]
      input = input[line.bytesize, line.bytesize]
      @handler.receive_line(line)
    end
  end
end

#send_data(str) ⇒ Object



23
24
25
# File 'lib/raval/connection.rb', line 23

def send_data(str)
  @socket.write(str)
end

#send_response(code, message) ⇒ Object



27
28
29
# File 'lib/raval/connection.rb', line 27

def send_response(code, message)
  @socket.write("#{code} #{message}#{Raval::LBRK}")
end