Class: Raval::PassiveSocket
- Inherits:
-
Object
- Object
- Raval::PassiveSocket
- Includes:
- Celluloid::IO
- Defined in:
- lib/raval/passive_socket.rb
Overview
In passive FTP mode, the server opens a listening data socket and the client will connect to it.
A different class is used when operating in active FTP mode. They both have a #read and #write method, so the quack like each other in the ways that matter.
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #close ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(host) ⇒ PassiveSocket
constructor
A new instance of PassiveSocket.
- #read ⇒ Object
- #run ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(host) ⇒ PassiveSocket
Returns a new instance of PassiveSocket.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/raval/passive_socket.rb', line 17 def initialize(host) @host = host @socket = nil puts "*** Starting passive socket on #{host}" # TODO ::TcpServer#addr exists, but Celluloid::IO::TcpServer#addr does # not. Once that's fixed, make this port random by replacing 30000 # with 0 #@server = TCPServer.new(@host, 0) #@port = @server.addr[1] @port = choose_port(@host) @server = TCPServer.new(@host, @port) #@port = @server.addr[1] run! end |
Instance Attribute Details
#port ⇒ Object (readonly)
Returns the value of attribute port.
15 16 17 |
# File 'lib/raval/passive_socket.rb', line 15 def port @port end |
Instance Method Details
#close ⇒ Object
54 55 56 57 58 59 |
# File 'lib/raval/passive_socket.rb', line 54 def close @socket.close if @socket @socket = nil @server.close if @server @server = nil end |
#connected? ⇒ Boolean
50 51 52 |
# File 'lib/raval/passive_socket.rb', line 50 def connected? @socket != nil end |
#read ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/raval/passive_socket.rb', line 37 def read raise "socket not connected" unless @socket @socket.readpartial(4096) rescue EOFError, Errno::ECONNRESET close nil end |
#run ⇒ Object
33 34 35 |
# File 'lib/raval/passive_socket.rb', line 33 def run handle_connection @server.accept end |
#write(data) ⇒ Object
45 46 47 48 |
# File 'lib/raval/passive_socket.rb', line 45 def write(data) raise "socket not connected" unless @socket @socket.write(data) end |