Class: FTPMVC::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/ftpmvc/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = '0.0.0.0', port) ⇒ Server

Returns a new instance of Server.



9
10
11
# File 'lib/ftpmvc/server.rb', line 9

def initialize(host='0.0.0.0', port)
  @host, @port = host, port
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/ftpmvc/server.rb', line 7

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/ftpmvc/server.rb', line 7

def port
  @port
end

Instance Method Details

#start(application) ⇒ Object



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

def start(application)
  EM.epoll
  EM::run do
    @signature = EM::start_server(@host, @port, EM::FTPD::Server, Driver, application)
    @port = Socket.unpack_sockaddr_in(EM.get_sockname(@signature)).first
    yield self if block_given?
  end
end

#start_in_new_thread(application) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ftpmvc/server.rb', line 13

def start_in_new_thread(application)
  queue = Queue.new
  server_thread = Thread.new do
    begin
      start(application) do |server|
        queue << server
      end
    rescue => e
      $strerr.puts "Server error: #{e.class}: #{e.message}"
    end
  end
  queue.pop
end

#stopObject



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

def stop
  EM.stop_server(@signature)
end