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.



27
28
29
30
31
32
# File 'lib/ftpd/server.rb', line 27

def initialize
  @interface = '127.0.0.1'
  @port = 0
  @stopping = false
  @server_thread = nil
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)


15
16
17
# File 'lib/ftpd/server.rb', line 15

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)


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

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)


39
40
41
# File 'lib/ftpd/server.rb', line 39

def bound_port
  @server_socket.addr[1]
end

#joinObject

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



46
47
48
49
# File 'lib/ftpd/server.rb', line 46

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.



54
55
56
57
# File 'lib/ftpd/server.rb', line 54

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.



62
63
64
65
66
67
68
69
# File 'lib/ftpd/server.rb', line 62

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