Class: Raval::PassiveSocket

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#portObject (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

#closeObject



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

Returns:

  • (Boolean)


50
51
52
# File 'lib/raval/passive_socket.rb', line 50

def connected?
  @socket != nil
end

#readObject



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

#runObject



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