Class: Ftpd::Server

Inherits:
Object
  • Object
show all
Includes:
Memoizer
Defined in:
lib/ftpd/server.rb

Direct Known Subclasses

TlsServer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



25
26
27
28
29
# File 'lib/ftpd/server.rb', line 25

def initialize
  @interface = '127.0.0.1'
  @port = 0
  @stopping = false
end

Instance Attribute Details

#interfaceString

The interface to bind to (e.g. “127.0.0.1”, “0.0.0.0”, “10.0.0.12”, “::1”, “::”, etc.). Defaults to “127.0.0.1”

Set this before calling #start.

Returns:

  • (String)


13
14
15
# File 'lib/ftpd/server.rb', line 13

def interface
  @interface
end

#portString

The port to bind to. Defaults to 0, which causes an ephemeral port to be used. When bound to an ephemeral port, use #bound_port to find out which port was actually bound to.

Set this before calling #start.

Returns:

  • (String)


23
24
25
# File 'lib/ftpd/server.rb', line 23

def port
  @port
end

Instance Method Details

#bound_portInteger

The port the server is bound to. Must not be called until after #start is called.

Returns:

  • (Integer)


36
37
38
# File 'lib/ftpd/server.rb', line 36

def bound_port
  @server_socket.addr[1]
end

#joinObject

The calling thread will suspend execution until the server is stopped.



43
44
45
46
# File 'lib/ftpd/server.rb', line 43

def join
  raise 'Server is not started!' if @server_thread.nil?
  @server_thread.join
end

#startObject

Start the server. This creates the server socket, and the thread to service it.



51
52
53
54
# File 'lib/ftpd/server.rb', line 51

def start
  @server_socket = make_server_socket
  @server_thread = make_server_thread
end

#stopObject

Stop the server. This closes the server socket, which in turn stops the thread.



59
60
61
62
63
64
65
66
# File 'lib/ftpd/server.rb', line 59

def stop
  @stopping = true
  begin
    @server_socket.shutdown
  rescue Errno::ENOTCONN
  end
  @server_socket.close
end