Class: Raval::Connection
- Inherits:
-
Object
- Object
- Raval::Connection
- 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
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#myhost ⇒ Object
readonly
Returns the value of attribute myhost.
-
#myport ⇒ Object
readonly
Returns the value of attribute myport.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#close ⇒ Object
Close the connection.
-
#initialize(handler, socket) ⇒ Connection
constructor
A new instance of Connection.
- #read_commands ⇒ Object
- #send_data(str) ⇒ Object
- #send_response(code, message) ⇒ Object
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
#host ⇒ Object (readonly)
Returns the value of attribute host.
12 13 14 |
# File 'lib/raval/connection.rb', line 12 def host @host end |
#myhost ⇒ Object (readonly)
Returns the value of attribute myhost.
13 14 15 |
# File 'lib/raval/connection.rb', line 13 def myhost @myhost end |
#myport ⇒ Object (readonly)
Returns the value of attribute myport.
13 14 15 |
# File 'lib/raval/connection.rb', line 13 def myport @myport end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
12 13 14 |
# File 'lib/raval/connection.rb', line 12 def port @port end |
Instance Method Details
#close ⇒ Object
Close the connection
32 33 34 |
# File 'lib/raval/connection.rb', line 32 def close @socket.close end |
#read_commands ⇒ Object
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 |